1
- #include < iostream>
2
- #include < fstream>
3
- #include < string>
4
- #include < sstream>
5
- #include < iomanip>
6
- #include < chrono>
7
-
8
- using namespace std ;
9
- using namespace std ::chrono;
10
-
11
- string readFile (std::ifstream& in);
12
- int brute_force (string textFileName, string patternFileName);
13
- int knuth_morris_pratt (string textFileName, string patternFileName);
14
- int boyer_moore (std::string text, std::string pattern);
15
- int rabin_karp (std::string text, std::string pattern);
16
-
17
- int main (int argc, char *argv[]) {
18
- if (argc < 3 ) {
19
- fprintf (stderr, " usage: ./out <textFileName patternFileName>\n " );
20
- return -1 ;
21
- }
22
-
23
- ifstream myTextFile (argv[1 ]);
24
- string text = readFile (myTextFile);
25
-
26
- ifstream myPatternFile (argv[2 ]);
27
- string pattern = readFile (myPatternFile);
28
-
29
- int textLength = text.size ();
30
- int patternLength = pattern.size ();
31
-
32
- auto start = high_resolution_clock::now ();
33
- int bruteForceRes = brute_force (text, pattern);
34
- auto stop = high_resolution_clock::now ();
35
- auto duration = duration_cast<microseconds>(stop - start);
36
- cout << " Brute force execution time: " << duration.count () << endl;
37
- cout << " Brute force found pattern in index: " << bruteForceRes << endl;
38
- cout << endl;
39
-
40
- start = high_resolution_clock::now ();
41
- int KMPRes = knuth_morris_pratt (text, pattern);
42
- stop = high_resolution_clock::now ();
43
- duration = duration_cast<microseconds>(stop - start);
44
- cout << " Knuth-Morris-Pratt execution time: " << duration.count () << endl;
45
- cout << " Knuth-Morris-Pratt found pattern in index: " << KMPRes << endl;
46
- cout << endl;
47
-
48
- start = high_resolution_clock::now ();
49
- int BoyerMooreRes = boyer_moore (text, pattern);
50
- stop = high_resolution_clock::now ();
51
- duration = duration_cast<microseconds>(stop - start);
52
- cout << " Boyer-Moore execution time: " << duration.count () << endl;
53
- cout << " Boyer-Moore found pattern in index " << BoyerMooreRes << endl;
54
- cout << endl;
55
-
56
- start = high_resolution_clock::now ();
57
- int rabinKarpRes = rabin_karp (text, pattern);
58
- stop = high_resolution_clock::now ();
59
- duration = duration_cast<microseconds>(stop - start);
60
- cout << " Rabin-Karp execution time: " << duration.count () << endl;
61
- cout << " Rabin-Karp found pattern in index: " << rabinKarpRes << endl;
1
+ #include < iostream>
2
+ #include < fstream>
3
+ #include < string>
4
+ #include < sstream>
5
+ #include < iomanip>
6
+ #include < chrono>
7
+
8
+ using namespace std ;
9
+ using namespace std ::chrono;
10
+
11
+ string readFile (std::ifstream& in);
12
+ int brute_force (string textFileName, string patternFileName);
13
+ int knuth_morris_pratt (string textFileName, string patternFileName);
14
+ int boyer_moore (std::string text, std::string pattern);
15
+ int rabin_karp (std::string text, std::string pattern);
16
+
17
+ int main (int argc, char *argv[]) {
18
+ if (argc < 3 ) {
19
+ fprintf (stderr, " usage: ./out <textFileName patternFileName>\n " );
20
+ return -1 ;
21
+ }
22
+
23
+ ifstream file1 (argv[1 ]);
24
+ ifstream file2 (argv[2 ]);
25
+
26
+ string text, pattern, text1 = readFile (file1), text2 = readFile (file2);
27
+ text = text1.at (1 ) == ' t' ? text1.substr (6 ) : text2.substr (6 );
28
+ pattern = text2.at (1 ) == ' p' ? text2.substr (9 ) : text1.substr (9 );
29
+
30
+ int textLength = text.size ();
31
+ int patternLength = pattern.size ();
32
+
33
+ auto start = high_resolution_clock::now ();
34
+ int bruteForceRes = brute_force (text, pattern);
35
+ auto stop = high_resolution_clock::now ();
36
+ auto duration = duration_cast<microseconds>(stop - start);
37
+ cout << " Brute force execution time: " << duration.count () << endl;
38
+ cout << " Brute force found pattern in index: " << bruteForceRes << endl;
39
+ cout << endl;
40
+
41
+ start = high_resolution_clock::now ();
42
+ int KMPRes = knuth_morris_pratt (text, pattern);
43
+ stop = high_resolution_clock::now ();
44
+ duration = duration_cast<microseconds>(stop - start);
45
+ cout << " Knuth-Morris-Pratt execution time: " << duration.count () << endl;
46
+ cout << " Knuth-Morris-Pratt found pattern in index: " << KMPRes << endl;
47
+ cout << endl;
48
+
49
+ start = high_resolution_clock::now ();
50
+ int BoyerMooreRes = boyer_moore (text, pattern);
51
+ stop = high_resolution_clock::now ();
52
+ duration = duration_cast<microseconds>(stop - start);
53
+ cout << " Boyer-Moore execution time: " << duration.count () << endl;
54
+ cout << " Boyer-Moore found pattern in index " << BoyerMooreRes << endl;
55
+ cout << endl;
56
+
57
+ start = high_resolution_clock::now ();
58
+ int rabinKarpRes = rabin_karp (text, pattern);
59
+ stop = high_resolution_clock::now ();
60
+ duration = duration_cast<microseconds>(stop - start);
61
+ cout << " Rabin-Karp execution time: " << duration.count () << endl;
62
+ cout << " Rabin-Karp found pattern in index: " << rabinKarpRes << endl;
62
63
}
0 commit comments