-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q1_PreSort.c
133 lines (126 loc) · 3.22 KB
/
Q1_PreSort.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//Amit Hampal
//ID: 0964514
//Assignment 3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
//function strips input of new line and replaces with null
char *removeNewLine(char *file) {
for(int i = 0; i < strlen(file); i++) {
if(*(file + i) == '\n') {
*(file + i) = '\0';
}
}
return file;
}
//struct definition
typedef struct arrElem {
char *origString;
char *sortString;
}arrElem;
//compare function for strings
int cmpFunc(const void *a, const void *b) {
return strcmp((char *)a, (char *)b);
}
//compare function for array of structs
int compFunc(const void *a, const void *b) {
return strcmp( ((arrElem *)a)->sortString, ((arrElem *)b)->sortString );
}
//recursive binary search function
int binarySearch(arrElem *arr, int l, int r, char *x)
{
if (r >= l)
{
int mid = l + (r - l)/2;
if (strcmp(arr[mid].sortString, x) == 0) {
return mid;
}
if (strcmp(arr[mid].sortString, x) > 0) {
return binarySearch(arr, l, mid-1, x);
}
return binarySearch(arr, mid+1, r, x);
}
return -1;
}
//main function
int main(int argc, char **argv) {
arrElem A[30000];
int n;
int j;
char *line = malloc(sizeof(char) * 100);
char *strToFind = malloc(sizeof(char) * 100);
char *tok = malloc(sizeof(char) *10);
char *currStr = malloc(sizeof(char) *100);
FILE *fp;
char *sortedPattern = malloc(sizeof(char) * 100);
int searchCount = 0;
int index;
int i;
//open file for reading
printf("File is: %s\n", argv[argc - 1]);
if(argc > 1)
{
fp = fopen( argv[argc - 1], "r" );
}
else
{
return 0;
}
if(fp == NULL)
{
printf("Cannot find file. Ensure your syntax is correct. Exiting program.\n");
return 0;
}
n = 0;
//read contents of file into memory, sort strings using qsort
while(!feof(fp))
{
fgets(line, 100, fp);
line = removeNewLine(line);
tok = strtok(line, " ");
while(tok != NULL) {
A[n].origString = malloc(sizeof(char) * 100);
A[n].sortString = malloc(sizeof(char) * 100);
strcpy(A[n].origString, tok);
strcpy(A[n].sortString, tok);
qsort(A[n].sortString, strlen(A[n].sortString), sizeof(char), cmpFunc);
n++;
tok = strtok(NULL, " ");
}
}
//sort array using qsort and then get user input
qsort(A, 30000, sizeof(arrElem), compFunc);
printf("Anagram to find: ");
fgets(strToFind, 100, stdin);
strToFind = removeNewLine(strToFind);
strcpy(sortedPattern, strToFind);
qsort(sortedPattern, strlen(strToFind), sizeof(char), cmpFunc);
clock_t b = clock();
index = binarySearch(A, 0, 30000, sortedPattern);
i = index;
//check elements before returned index to see if they are also anagrams
if(index != -1) {
while(strcmp(A[i].sortString, sortedPattern) == 0) {
printf("%s is an anagram\n", A[i].origString);
i++;
searchCount++;
}
//check elements after returned index to see if they are also anagrams
i = index - 1;
while(strcmp(A[i].sortString, sortedPattern) == 0) {
printf("%s is an anagram\n", A[i].origString);
i--;
searchCount++;
}
}
clock_t a = clock();
//report results
double time = (double)(a-b) / CLOCKS_PER_SEC;
printf("Number of anagrams: %d\n", searchCount);
printf("Execution time for bruteforce was %lf seconds\n", time);
free(line);
free(strToFind);
fclose(fp);
return 0;
}