-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
75 lines (61 loc) · 2.26 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "FNAFilePreprocess.h"
#include "RabinKarpAlgorithm.h"
#include "BruteForce.h"
#include "KMPAlgorithm.h"
#include <stdio.h>
#include <time.h>
#include <string.h>
#define NTHASH_COMMAND "rk-nt"
#define RABINKARP_COMMAND "rk"
#define BRUTEFROCE_COMMAND "b"
#define KMP_COMMAND "kmp"
int main(int argc, char *argv[]) {
char *geneticSequence;
int occurrences[MAX_OCCURRENCES];
char patternSequence[MAX_INPUT];
int (*func_ptr)(char*, char*, int*);
// parse the command line arguments
if (argc >= 2) {
if (strcmp(argv[1],NTHASH_COMMAND) == 0) {
func_ptr = &RabinKarpAlgorithm_ntHash;
initializeValueMap();
} else if (strcmp(argv[1],RABINKARP_COMMAND) == 0) {
func_ptr = &RabinKarpAlgorithmNaive;
} else if (strcmp(argv[1],BRUTEFROCE_COMMAND) == 0) {
func_ptr = &BruteForce;
} else if (strcmp(argv[1],KMP_COMMAND) == 0) {
func_ptr = &KMPSearch;
}
if (argc == 3) {
geneticSequence = readFile(argv[2]);
} else {
geneticSequence = readFile("example.fna");
}
} else {
func_ptr = &BruteForce; // default algorithm
geneticSequence = readFile("example.fna");
}
printf("Type the target pattern to search (type -1 to exit the program): ");
scanf("%s", patternSequence); // '\0' is appended
while (strcmp(patternSequence,"-1") != 0) {
// start searching
clock_t startTime = clock();
int occurrencesNum = (*func_ptr)(geneticSequence, patternSequence, occurrences);
clock_t endTime = clock();
// print the result
if (occurrencesNum != 0) {
printf("==========%d occurrences found in total==========\n", occurrencesNum);
for (int i = 0; i < occurrencesNum; i++) {
printf("%d ", occurrences[i]);
}
printf("\n");
} else {
printf("==========Not Found==========\n");
}
double elapsed = (double) (endTime - startTime) * 1000.0 / CLOCKS_PER_SEC;
printf("==========Execution time = %f==========\n", elapsed);
printf("Type the target pattern to search (type -1 to exit the program): ");
scanf("%s", patternSequence);
}
return 0;
}