/
main.c
326 lines (285 loc) · 6.91 KB
/
main.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Main creates the menu and calls the functions from other files.
Consolidates all the other files. This also writes to file
Written By: Henry Wang
Debugged by: Mark Darmadi, Jon Wu, Louis Lesman & Alfred Koshan
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cType.h>
#include "Header.h"
#define FLUSH while( getchar() != '\n' )
#ifdef _MSC_VER
#include <crtdbg.h>
#endif
int main(){
HASH *hash;
D_LIST *list;
int size = 30;
int i = 0;
BST_TREE *tree = BST_Create(cmpID);
list = createDoublyList();
hash = createHashTable(size);
readFile(hash, list, tree);
process(hash, list, tree);
hash = freeData(hash);
freeLinkedList(list);
tree = BST_Destroy(tree);
printf("\n\t\tEnd of the program!\n" );
#ifdef _MSC_VER
printf( _CrtDumpMemoryLeaks() ? "No Memory Leak\n" : "Memory Leak\n");
#endif
getch();
}
/* ===== process =====
This function presents the user with the menu.
Pre hash, list, tree
Post nothing
*/
void process (HASH *hash, D_LIST *list, BST_TREE *tree)
{
// Local Definitions
char choice;
CAR car;
// Statements
do
{
choice = getChoice ();
switch (choice)
{
case 'P': printHash(hash);
printLinkedList(list);
printManager(tree);
break;
case 'R': printIndentedBST(tree);
break;
case 'E': printData(hash);
break;
case 'S': searchByHash(hash, list);
break;
case 'T': searchByDList(list);
break;
case 'I': car = addNewNode();
insert(hash,car);
insertDNode(list, car);
insertTree(tree,car);
break;
case 'D': deleteCar(list, hash, tree);
break;
case 'W': writeToFile(list);
break;
case 'Q': break;
} // switch
} while (choice != 'Q');
return;
} // process
/* ===== getChoice =====
This function prints the menu and gets the users option
Pre nothing
Post choice is returned
*/
char getChoice (void)
{
// Local Definitions
char choice;
int valid;
// Statements
printf("\n======== MENU ======= \n"
"Here are your choices:\n"
" S: Search by Hash Key\n"
" T: Search by Self-Adjusting LL\n"
" I: Insert a new Car\n"
" P: Print all \n"
" R: Print Indented Tree \n"
" E: Print Efficiency \n"
" D: Delete \n"
" W: Write to file \n"
" Q: Quit \n\n"
"Enter your choice: ");
do
{
scanf(" %c", &choice);
FLUSH;
choice = toupper(choice);
switch (choice)
{
case 'S':
case 'T':
case 'P':
case 'R':
case 'E':
case 'I':
case 'D':
case 'W':
case 'Q': valid = 1;
break;
default: valid = 0;
printf("\aInvalid choice\n"
"Please try again: ");
break;
} // switch
} while (!valid);
return choice;
}// getChoice
/* ===== readFile =====
This function inputs the data from the file
Pre hash, list, cars
Post nothing
*/
void readFile(HASH *hash, D_LIST *list, BST_TREE *cars){
CAR car;
FILE *fp;
CAR *fill;
D_NODE *pPre = list->tail;
char input[250];
fp = fopen("Cars.txt","r");
if(!fp){
printf("Error. Cannot Read File");
exit(100);
}
while((fgets(input,sizeof(input), fp))){
car = stringToCar(input);
insert(hash,car);
insertDNode(list, car);
pPre = pPre->back;
if(!BST_Retrieve(cars, &car))
{
fill = (CAR*)malloc(sizeof(CAR));
if(!fill)
{
printf("Memory FULL\n"),
exit(101);
}
fill->name = allocateString(car.name);
fill->brand = allocateString(car.brand);
fill->cost = car.cost;
strcpy(fill->plate, car.plate);
BST_Insert(cars, fill);
cars->count++;
}
}
}// readFile
/* ===== stringToCar =====
This function converts the string and inputs the data
Pre input
Post CAR list
*/
CAR stringToCar(char *input){
CAR car;
char tempBrand[50];
char tempName[50];
sscanf(input,"%s %s %s %d", car.plate, tempBrand,tempName, &(car.cost));
car.brand = (char *)calloc(strlen(tempBrand)+1,sizeof(char));
car.name = (char *)calloc(strlen(tempName)+1,sizeof(char));
strcpy(car.name,tempName);
strcpy(car.brand,tempBrand);
return car;
}// stringToCar
/*Linked List */
/* ===== addNewNode =====
This function adds a new node to the list
Pre nothing
Post CAR list
*/
CAR addNewNode(void){
CAR car;
char plate[50];
char tempString[50];
do{
printf("Enter your plate number: ");
gets(plate);
if(strlen(plate)!=6){
printf("Invalid input. Try Again\n");
}
}while(strlen(plate)!= 6);
strcpy(car.plate, plate);
printf("Enter brand: ");
gets(tempString);
car.brand = (char *)calloc(strlen(tempString)+1, sizeof(char));
strcpy(car.brand, tempString);
printf("Enter name: ");
gets(tempString);
car.name = (char *)calloc(strlen(tempString)+1, sizeof(char));
strcpy(car.name, tempString);
printf("Enter estimated cost: ");
scanf("%d", &(car.cost));
return car;
}// addNewNode
/* ===== cmpID =====
This function compares the plate numbers
Pre pID1, pID2
Post returns result
*/
int cmpID (void* pID1, void* pID2)
{
// Local Definitions
int result;
char id1[7];
char id2[7];
// Statements
strcpy(id1,((CAR *)pID1)->plate);
strcpy(id2,((CAR *)pID2)->plate);
if (strcmp(id1, id2) < 0)
result = -1;
else if (strcmp(id1, id2) > 0)
result = +1;
else{
result = 0;
}
return result;
}// cmpID
/* ===== deleteCar =====
This function deletes the car entry
Pre list, hash, tree
Post nothing
*/
void deleteCar(D_LIST *list, HASH *hash, BST_TREE *tree){
CAR car;
char plate[50];
char tempString[50];
do{
printf("Enter your plate number: ");
gets(plate);
if(strlen(plate)!=6){
printf("Invalid input. Try Again\n");
}
}while(strlen(plate)!= 6);
strcpy(car.plate, plate);
deleteNode(list, plate);
deleteHash(hash, plate);
deleteTree(tree, plate);
}// deleteCar
/* ===== wroteToFile =====
This function writes to file
Pre list,
Post nothing
*/
void writeToFile(D_LIST *list){
FILE *fp;
int i = 0;
D_NODE *pCur;
fp = fopen("serial.txt","w");
if(!fp){
printf("Error. Cannot Read File");
exit(100);
}
pCur = list->head->forward;
for(i = 0; i< list->counter; i++){
fprintf(fp,"%s; %s; %s; %d\n", pCur->car.plate, pCur->car.name, pCur->car.brand, pCur->car.cost);
pCur = pCur->forward;
}
printf("Succesfully output to serial.txt\n");
fclose(fp);
}// writeToFile
/* ===== printIndentedBST =====
Pre cars
Post nothing
*/
void printIndentedBST(BST_TREE *cars)
{
printf("=======================\n"
"LIST OF CARS\n"
"=======================\n");
BST_Print(cars, printCars);
return;
}// printIndentedBST