-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
138 lines (95 loc) · 3.24 KB
/
main.c
File metadata and controls
138 lines (95 loc) · 3.24 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
#include <stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>
//Alphabet's size
const int DICTIONARY_SIZE = 26;
//TrieNode structure
struct TrieNode {
struct TrieNode *letter[26];
bool isTheEndOfKey;
bool isUsed;
};
//Methods
struct TrieNode *createTrieNode();
void insertKeyToTree(struct TrieNode *root, char *key);
bool searchTrieForKey(struct TrieNode *root, const char *keyToFind);
int main() {
//Def attributes
FILE *INPUT_FILE;
char *tmp;
char *letter[26], *coupon, *word;
struct TrieNode *root = createTrieNode();
tmp = (char *) malloc(sizeof(char));
coupon = (char *) malloc(sizeof(char));
word = (char *) malloc(sizeof(char));
INPUT_FILE = fopen("../input.txt", "r");
if (INPUT_FILE == NULL) {
printf("File input.txt not in path! Exiting...");
exit(-1);
}
//If file is not empty put key into trees
while (fscanf(INPUT_FILE, "%s", &letter) != EOF) {
tmp = &letter[0];
insertKeyToTree(root, tmp);
}
printf("Please enter your coupon to checks its validity!\n");
scanf("%s", coupon);
word = &coupon;
while (word != "END") {
for (int k = 0; k < strlen(coupon); ++k) {
//Check if coupon is uppercase and 10 letters, modify to our need
if (!isupper(coupon[k]) || strlen(coupon) != 10) {
printf("Code not valid. Only Uppercase allowed for coupons and should be 10 letters long");
exit(1);
}
}
if (searchTrieForKey(root, coupon)) {
printf("\n %s --- Code is Valid\n", coupon);
} else {
printf("\n %s --- Code is NOT Valid\n", coupon);
}
scanf("%s", coupon);
word = &coupon;
}
return 0;
}
//Create a new node
struct TrieNode *createTrieNode(void) {
struct TrieNode *pNode = (struct TrieNode *) malloc(sizeof(struct TrieNode));
pNode->isTheEndOfKey = false;
for (int i = 0; i < DICTIONARY_SIZE; ++i) {
pNode->letter[i] = NULL;
}
return pNode;
}
bool searchTrieForKey(struct TrieNode *root, const char *keyToFind) {
struct TrieNode *pCrawl = root;
for (int i = 0; i < strlen(keyToFind); ++i) {
int indexForSearch = keyToFind[i] - 'A';
//If doesnt exist in trie return false
if (!pCrawl->letter[indexForSearch])
return false;
pCrawl = pCrawl->letter[indexForSearch];
}
if (pCrawl->isTheEndOfKey && !(pCrawl->isUsed) && pCrawl != NULL) { // if exists in trie and not used return true
//Find coupon and mark it as used
pCrawl->isUsed = true;
return true;
} else if (pCrawl->isTheEndOfKey && (pCrawl->isUsed) && pCrawl != NULL) { // if exists in trie and used return false
printf("\n Coupon already in use!");
return false;
}
}
//Creates nodes for given keys. The last element is consider as leaf
void insertKeyToTree(struct TrieNode *root, char *key) {
struct TrieNode *pCrawl = root;
for (int i = 0; i < strlen(key); i++) {
int index = key[i] - 'A';
if (!pCrawl->letter[index])
pCrawl->letter[index] = createTrieNode();
pCrawl = pCrawl->letter[index];
}
pCrawl->isTheEndOfKey = true;
}