-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrSBS.c
94 lines (70 loc) · 1.98 KB
/
strSBS.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#define MAXWORD 220000
#define MAXLENGTH 32
void sort(char dictionary[][MAXLENGTH], int size)
{
//Adapt the integer sorting algorithm here
}
int binarySearch(char dictionary[][MAXLENGTH], int size, char word[])
{
//Adapt the integer binary search here
return -1;
}
int readDictionary(char fname[], char dictionary[][MAXLENGTH], int max)
{
int count = 0;
FILE* infile;
if(!( infile = fopen( fname, "r"))){
return 0;
}
//Try to read up to max, but will stop if the file runs out
while (count < max && fscanf(infile,"%s", dictionary[count++]) == 1);
return count;
}
int queryWordList(char fname[], int* nQuery,
char dictionary[][MAXLENGTH], int size)
{
FILE* infile;
int count = 0, found = 0;
char word[MAXLENGTH];
if(!( infile = fopen( fname, "r"))){
return 0;
}
while (count < *nQuery && fscanf(infile,"%s", word) == 1) {
count++;
if (binarySearch(dictionary, size, word) != -1){
found++;
}
}
//Update the number query if we cannot perform all
if (count != *nQuery){
*nQuery = count;
}
return found;
}
int main()
{
char dictionary[MAXWORD][MAXLENGTH];
char word[MAXLENGTH];
int dSize = 0, success, nQuery = 10000, N;
clock_t start, end;
double seconds;
//Read the "data size" as N
printf("Data Size = ");
scanf("%d", &N);
//Initialize the dictionary up to N words
// dSize is the actual size after initialization
dSize = readDictionary("words_random.txt", dictionary, N);
printf("Read %d words\n", dSize);
//Sort dictionary first
sort(dictionary, dSize);
//Perform nQuery[10,000] searches
success = queryWordList("words_query.txt", &nQuery, dictionary, dSize);
//Print the statistic
printf("Found %d out of %d queries in dictionary of %d words\n",
success, nQuery, dSize);
return 0;
}