-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
72 lines (69 loc) · 1.75 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
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include "Trie.h"
#include "TrieNode.h"
#include <string>
#include <iostream>
#include <ctype.h>
#define MAX_WORD_LEN 65
#define CHARACTER 'a'
int main()
{
Trie tr;
string str;
char line[256];
char *word;
int length, occurences;
//preventing resize temp every time
str.reserve(MAX_WORD_LEN);
cout << "Please enter your input, enter '.' to end input" << endl;
//input loop
while (true) {
cin.getline(line, 256);
//checks for end of input
if (line[0] == '.' && line[1] == '\0')
break;
//returns a word from the line, space is replaced by a '\0'
word = strtok(line, " ");
while (word) {
str = word;
str = tr.checkAndUpdateWord(str);
length = str.length();
//make sure that the word is not too long
if (length > MAX_WORD_LEN)
str.erase(MAX_WORD_LEN, length);
tr.insert(str);
//getting next word
word = strtok(NULL, " ");
}
}
//second level loop
while (true) {
cin.getline(line, 256);
//checks for end of input
if (line[0] == '.' && line[1] == '\0')
break;
//returns a word from the line, space is replaced by a '\0'
word = strtok(line, " ");
while (word) {
str = word;
length = str.length();
//make sure that the word is not too long
if (length > MAX_WORD_LEN)
str.erase(MAX_WORD_LEN, length);
occurences = tr.find(str);
//the word appears in the dictionary
if (occurences > 0)
{
cout << occurences << "\n";
//delete the word
tr.deleteWord(str);
}
//the word is not appear in the dictionary
else
cout << "Did you mean " << tr.approxFind(str) << "?\n";
//getting next word
word = strtok(NULL, " ");
}
}
}