-
Notifications
You must be signed in to change notification settings - Fork 0
/
parity.c
73 lines (54 loc) · 1.59 KB
/
parity.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
/*
* File: parity.c
*
* Description: This file contains the main function for testing a set
* abstract data type for strings.
*
* The program takes a single file as a command line argument.
* A set is used to maintain a collection of words that occur
* an odd number of times. The counts of total words and
* words appearing an odd number of times are printed.
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <stdbool.h>
# include "set.h"
/* This is sufficient for the test cases in /scratch/coen12. */
# define MAX_SIZE 18000
/*
* Function: main
*
* Description: Driver function for the test application.
*/
int main(int argc, char *argv[])
{
FILE *fp;
char buffer[BUFSIZ];
SET *odd;
int words;
/* Check usage and open the file. */
if (argc != 2) {
fprintf(stderr, "usage: %s file1\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "%s: cannot open %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
/* Insert or delete words to compute their parity. */
words = 0;
odd = createSet(MAX_SIZE);
while (fscanf(fp, "%s", buffer) == 1) {
words ++;
if (hasElement(odd, buffer))
removeElement(odd, buffer);
else
addElement(odd, buffer);
}
printf("%d total words\n", words);
printf("%d words occur an odd number of times\n", numElements(odd));
fclose(fp);
destroySet(odd);
exit(EXIT_SUCCESS);
}