-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
122 lines (109 loc) · 2.88 KB
/
main.c
File metadata and controls
122 lines (109 loc) · 2.88 KB
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
/*
* CS 261 Data Structures
* Assignment 5
* Name: Jesse McKenna
* Date: 2018-3-10
*/
#include "hashMap.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
/**
* Allocates a string for the next word in the file and returns it. This string
* is null terminated. Returns NULL after reaching the end of the file.
* @param file
* @return Allocated string or NULL.
*/
char* nextWord(FILE* file)
{
int maxLength = 16;
int length = 0;
char* word = malloc(sizeof(char) * maxLength);
//Fix me: Do the necessary change to make the implementation //case-insensitive
while (1)
{
char c = fgetc(file);
if ((c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') ||
c == '\'')
{
if (length + 1 >= maxLength)
{
maxLength *= 2;
word = realloc(word, maxLength);
}
// .............
word[length] = c;
length++;
}
else if (length > 0 || c == EOF)
{
break;
}
}
if (length == 0)
{
free(word);
return NULL;
}
word[length] = '\0';
return word;
}
/**
* Prints the concordance of the given file and performance information. Uses
* the file input1.txt by default or a file name specified as a command line
* argument.
* @param argc
* @param argv
* @return
*/
int main(int argc, const char** argv)
{
// FIXED
const char* fileName = "input1.txt";
if (argc > 1)
{
fileName = argv[1];
}
printf("Opening file: %s\n", fileName);
clock_t timer = clock();
HashMap* map = hashMapNew(10);
// --- Concordance code begins here ---
FILE* file = fopen(fileName, "r");
char* word = nextWord(file);
int wordCount;
while (word != NULL) // for each word in file
{
for (int i = 0, n = strlen(word); i < n; i++)
{
word[i] = tolower(word[i]); // convert all characters to lower case
}
if (hashMapContainsKey(map, word)) // word is already in the map
{
wordCount = *(hashMapGet(map, word));
hashMapPut(map, word, wordCount + 1); // increment its count
}
else
{
hashMapPut(map, word, 1); // add word to map with initial count of 1
}
// Be sure to free the word after you are done with it here.
free(word);
word = nextWord(file); // get next word
}
fclose(file);
// --- Concordance code ends here ---
hashMapPrint(map);
timer = clock() - timer;
printf("\nRan in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC);
printf("Empty buckets: %d\n", hashMapEmptyBuckets(map));
printf("Number of links: %d\n", hashMapSize(map));
printf("Number of buckets: %d\n", hashMapCapacity(map));
printf("Table load: %f\n", hashMapTableLoad(map));
hashMapDelete(map);
return 0;
}