-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
77 lines (59 loc) · 1.97 KB
/
main.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
using std::string;
/*
Author : Blaz Pocrnja
Student ID : 5035473
Assignment #3
Purpose: Reads a text file, and builds a dictionary of words found in the file. Keeps track of the frequency of words.
*/
const int MAX = 100;
typedef string STRING;
typedef bool BOOL;
typedef string WORD;
/*
structure describing a word entry in the dictionary
*/
typedef struct entry {
int count; /* frequency count for a particular word */
WORD w; /* the word itself */
struct entry *nextWord; /* pointer to next entry */
} ENTRY;
/*
structure describing the dictionary
*/
typedef struct dict {
int maxEntries; /* maximum number of entries allowed; this is an artificial limit */
/* link lists can be as big as you want. This limit ensures that */
/* this code tries to behave like the previous ones */
int numWords; /* number of words in the dictionary */
ENTRY *Words; /* pointer to the entries in the dictionary */
} DICT;
ENTRY *LocateWord(DICT& , WORD);
BOOL FullDictionary(DICT&);
BOOL InsertWord(DICT&,WORD);
WORD GetNextWord(void);
void DumpDictionary(DICT&);
/*
note that these are global variables so that they are already initialized to 0
*/
DICT dictionary={MAX,0,0}; /* your dictionary */
WORD word; /* */
int main (void) {
ENTRY *pos;
while (1) {
word = GetNextWord();
if ( word.empty() ) {
DumpDictionary(dictionary);
break;
}
if ((pos = LocateWord(dictionary,word)) > 0 )
pos->count++;
else
if (!InsertWord(dictionary,word)) cout << "dictionary full" << word << "cannot be added\n";
}
return 0;
}