-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.cpp
More file actions
275 lines (210 loc) · 5.17 KB
/
Code.cpp
File metadata and controls
275 lines (210 loc) · 5.17 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tnode{
char ch;
bool word;
char definition[1001];
struct tnode *next[26];
};
void release(struct tnode **root){
char newWord[101];
char newDef[1001];
bool valid = 0;
while(!valid){
printf("Input a new slang word [Must be more than 1 characters and contains no space]: ");
scanf("%[^\n]", &newWord);
getchar();
if(strlen(newWord) <= 1) continue;
bool space = 0;
for(int i = 0; i < strlen(newWord); i++){
if(newWord[i] == ' '){
space = 1;
break;
}
}
if(space) continue;
valid = 1;
}
valid = 0;
while(!valid){
printf("Input a new slang word description [Must be more than 2 words]: ");
scanf("%[^\n]", &newDef);
getchar();
int nspace = 0;
for(int i = 0; i < strlen(newDef); i++){
if(newDef[i] == ' ') nspace++;
}
if(nspace < 2) continue;
valid = 1;
}
struct tnode *curr = *root;
bool exist = 1;
for(int i = 0; i < strlen(newWord); i++){
if(curr->next[newWord[i] - 97] != NULL){
curr = curr->next[newWord[i] - 97];
}
else{
struct tnode *temp = (struct tnode*) malloc(sizeof(struct tnode));
for(int i = 0; i < 26; i++) temp->next[i] = NULL;
temp->ch = newWord[i];
temp->word = 0;
curr->next[newWord[i] - 97] = temp;
if(i == strlen(newWord) - 1){
temp->word = 1;
strcpy(temp->definition, newDef);
}
curr = temp;
exist = 0;
}
}
if(exist){
strcpy(curr->definition, newDef);
printf("\nSuccessfully updated a slang word.\n");
}
else{
printf("\nSuccessfully released new slang word.\n");
}
printf("\n");
}
void search(struct tnode **root){
char wordSearch[101];
bool valid = 0;
while(!valid){
printf("Input a slang word to be searched [Must be more than 1 characters and contains no space]: ");
scanf("%[^\n]", &wordSearch);
getchar();
if(strlen(wordSearch) <= 1) continue;
bool space = 0;
for(int i = 0; i < strlen(wordSearch); i++){
if(wordSearch[i] == ' '){
space = 1;
break;
}
}
if(space) continue;
valid = 1;
}
bool exist = 1;
struct tnode *curr = *root;
for(int i = 0; i < strlen(wordSearch); i++){
curr = curr->next[wordSearch[i] - 97];
if(curr == NULL){
exist = 0;
break;
}
if(i == strlen(wordSearch) - 1){
if(curr->word == 0) exist = 0;
break;
}
}
if(!exist) printf("There is no word \"%s\" in the dictionary.\n\n", wordSearch);
else{
printf("Slang word: %s\n", wordSearch);
printf("Description: %s\n\n", curr->definition);
}
}
int num = 1;
void display(struct tnode **root, char words[], int level){
struct tnode *curr = *root;
if(curr->word){
words[level] = '\0';
printf("%d. %s\n", num, words);
num++;
}
for(int i = 0; i < 26; i++){
if(curr->next[i] != NULL){
words[level] = i + 'a';
display(&curr->next[i], words, level + 1);
}
}
}
void displayPrefix(struct tnode **root, char prefixSearch[], char words[], int level){
struct tnode *curr = *root;
if(curr->word){
words[level] = '\0';
printf("%d. %s%s\n", num, prefixSearch, words);
num++;
}
for(int i = 0; i < 26; i++){
if(curr->next[i] != NULL){
words[level] = i + 'a';
displayPrefix(&curr->next[i], prefixSearch, words, level + 1);
}
}
}
void viewprefix(struct tnode **root){
char prefixSearch[101];
printf("Input a prefix to be searched: ");
scanf("%[^\n]", &prefixSearch);
getchar();
bool exist = 1;
struct tnode *curr = *root;
for(int i = 0; i < strlen(prefixSearch); i++){
curr = curr->next[prefixSearch[i] - 97];
if(curr == NULL){
exist = 0;
break;
}
}
if(!exist) printf("There is no prefix \"%s\" in the dictionary.\n\n", prefixSearch);
else{
int level = 0;
char words[101] = {};
displayPrefix(&curr, prefixSearch, words, level);
}
num = 1;
printf("\n");
}
void viewall(struct tnode **root){
struct tnode *curr = *root;
bool empty = 1;
for(int i = 0; i < 26; i++){
if(curr->next[i] != NULL){
empty = 0;
break;
}
}
if(empty){
printf("There is no slang word yet in the dictionary.\n\n");
return;
}
int level = 0;
char words[101] = {};
display(&curr, words, level);
num = 1;
printf("\n");
}
int main(){
struct tnode *root = (struct tnode*) malloc(sizeof(struct tnode));
root->word = 0;
for(int i = 0; i < 26; i++) root->next[i] = NULL;
int nchoice = 0;
while(nchoice != 5){
printf("Choose one of the option\n");
printf("1. Release a new slang word\n");
printf("2. Search a slang word\n");
printf("3. View all slang words starting with a certain prefix word\n");
printf("4. View all slang words\n");
printf("5. Exit\n");
printf("Input your choice: ");
scanf("%d", &nchoice);
getchar();
switch(nchoice){
case(1):
release(&root);
break;
case(2):
search(&root);
break;
case(3):
viewprefix(&root);
break;
case(4):
viewall(&root);
break;
}
}
printf("Thank you... Have a nice day :)\n");
return 0;
}