-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
461 lines (446 loc) · 13.6 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
#include "student.h"
int readName(char *name){
char str[100];
int c =0;
do{
c= 0;
fgets(str,100,stdin);
if(str[0] == '\n') continue;
for(int j =0 ; str[j]; j++){
if(str[j] >= '0' && str[j] <= '9')
return 1;
if(isspace(str[j]))
c++;
}
if(c == strlen(str)) continue;
//remove leading
int i =0;
while (str[i] == ' '|| str[i] == '\t'|| str[i] == '\n'){
i++;
}
for (int k =0, j = i; str[j] != '\0'; j++){
if(i == 0){
strcpy(name , str);
break;
}
name[k] = str[j];
k++;
}
name[strlen(name) -1] = '\0';
// manage letter case
for(i = 0; i<strlen(name); i++) {
if (i == 0 && name[i] != ' ' || name[i] != ' ' && name[i-1] == ' ') {
if(name[i] >= 'a' && name[i]<='z') {
name[i] = (char)(('A'-'a') + name[i] );
}
} else if (name[i] >= 'A' && name[i] <= 'Z') {
name[i] = (char)(name[i] + ('a' - 'A'));
}
}
}while(str[0] == '\n' || c == strlen(str));
return 0;
}
void readClass(int *classNum)
{
//Only allows digits between 1 and 9
char c;
while (1)
{
c = getche();
puts("");
fflush(stdin);
if (c >= '1' && c <= '9')
{
*classNum = c - '0';
return;
}
}
return;
}
int readID(int *ID)
{
char text[7];
int output = 0;
do {
fgets(text, sizeof(text), stdin);
} while (text[0] == '\n');
int i = 0;
for(i = 0; i<strlen(text); i++) {
if (text[i] >= '0' && text[i] <= '9')
output = (output * 10) + (text[i]-'0');
else if (text[i] != '\0' && text[i] != '\n')
{
return 0;
}
}
*ID = output;
return 1;
}
int validMenuChoice(int maxChoice)
{
fflush(stdin);
char c;
while (1)
{
c = getch();
if (c >= '1' && c <= '9')
{
if (c-'0' >= 1 && c-'0' <= maxChoice)
{
return c-'0';
}
else
return 0;
} else return 0;
}
return -1;
}
void delayMessage(char msg[])
{
//Adds a delay by asking to click any button before clearing the screen
//Used to display an error message or a 'click any button to continue'
printf("%s\n", msg);
int c = getchar();
fflush(stdin);
}
void DeleteStudentMenu(StudentList *ls)
{
unsigned int ID;
char name[100];
while(1)
{
system("cls || clear");
printf("\tDelete Student\n");
printf("1- Delete by name\n");
printf("2- Delete by ID\n");
printf("3- Back\n\n");
printf("Enter your selection: ");
switch(validMenuChoice(3))
{
case 1:
system("cls || clear");
printf("\tDelete Student by name\n");
printf("Name: ");
readName(name);
int flag = DeletebyName(name, ls, printStudent);
if (flag == 0)
delayMessage("Student successfully deleted!");
else if (flag == 1)
{
if (!readID(&ID))
{
delayMessage("\nError: Enter valid ID");
continue;
}
if (DeletebyID(ID, ls)) {
delayMessage("Student successfully deleted!");
return;
}
else
delayMessage("\nError: No student with this ID exists");
}
else
delayMessage("\nError: No student with this name exists");
break;
case 2:
while(1)
{
system("cls || clear");
printf("\tDelete Student by ID\n");
printf("ID: ");
if (!readID(&ID))
{
delayMessage("\nError: Enter valid ID");
continue;
}
if (DeletebyID(ID, ls)){
delayMessage("Student successfully deleted!");
return;
}
else
delayMessage("\nError: No student with this ID exists");
break;
}
break;
case 3:
return;
break;
default:
delayMessage("\nError: enter a valid input");
}
}
}
void EditStudentMenu(StudentList *ls)
{
unsigned int ID, classID;
char name[100];
while(1)
{
system("cls || clear");
printf("\tEdit Student\n");
printf("ID: ");
if (!readID(&ID))
{
delayMessage("\nError: Enter valid ID");
continue;
}
if (idExists(ls, ID))
{
while (1)
{
system("cls || clear");
printf("\tEdit Student\n");
printf("1- Edit name\n");
printf("2- Change Class\n");
printf("3- Back\n\n");
printf("Enter your selection: ");
switch(validMenuChoice(3))
{
case 1:
system("cls || clear");
printf("\tEdit Student's name\n");
printf("Name: ");
if(readName(name)){
delayMessage("\nError: Enter a valid name");
break;
}
EditStudentName(ls, ID, name);
delayMessage("Name successfully changed!");
return;
break;
case 2:
system("cls || clear");
printf("\tChange Student's class\n");
printf("Class: ");
readClass(&classID);
EditStudentClass(ls, ID, classID);
if (classID == ID/1000)
delayMessage("Same class, nothing was changed");
else
delayMessage("Class successfully changed!");
return;
break;
case 3:
return;
break;
default:
delayMessage("\nError: enter a valid input");
}
}
}
else {
delayMessage("\nError: No student with this ID exists");
return;
}
}
}
void SearchStudentMenu(StudentList *ls)
{
unsigned int ID;
char name[100];
while(1)
{
system("cls || clear");
printf("\tSearch Student\n");
printf("1- Search by name\n");
printf("2- Search by ID\n");
printf("3- Back\n\n");
printf("Enter your selection: ");
switch(validMenuChoice(3))
{
case 1:
system("cls || clear");
printf("\tSearch Student(s) by name\n");
printf("Name: ");
readName(name);
if (SearchByName(name, ls, printStudent))
delayMessage("Press any key to return");
else
delayMessage("\nError: No student with this name exists");
break;
case 2:
while(1)
{
system("cls || clear");
printf("\tSearch Student by ID\n");
printf("ID: ");
if (!readID(&ID))
{
delayMessage("\nError: Enter valid ID");
continue;
}
if (SearchByID(ID, ls, printStudent))
delayMessage("Press any key to return");
else
delayMessage("\nError: No student with this ID exists");
break;
}
break;
case 3:
return;
break;
default:
delayMessage("\nError: enter a valid input");
}
}
}
void ShowStudentsMenu(StudentList *ls)
{
unsigned int classID;
while(1)
{
system("cls || clear");
printf("\tShow all Students in a class\n");
printf("1- Show sequentially\n");
printf("2- Show in reverse order\n");
printf("3- Back\n\n");
printf("Enter your selection: ");
switch(validMenuChoice(3))
{
case 1:
system("cls || clear");
printf("\tShow all Students in a class (Sequentially)\n");
printf("Class: ");
readClass(&classID);
if (showStudentsInClass(ls, classID, printStudent))
delayMessage("Press any key to return");
else
delayMessage("\nError: Class doesn't exist");
break;
case 2:
system("cls || clear");
printf("\tShow all Students in a class (Reversed)\n");
printf("Class: ");
readClass(&classID);
if (showStudentsInClassRev(ls, classID, printStudent))
delayMessage("Press any key to return");
else
delayMessage("\nError: Class doesn't exist");
break;
case 3:
return;
break;
default:
delayMessage("\nError: enter a valid input");
}
}
}
int main()
{
StudentList ls;
student s;
createList(&ls);
char name[100];
unsigned int classID, ID, flag;
while(1)
{
system("cls || clear");
printf("\tStudent Academic Records System\n");
printf("1- Add Student\n");
printf("2- Delete Student (by ID or Name)\n");
printf("3- Edit Student\n");
printf("4- Search Student (by ID or Name)\n");
printf("5- Show all Students in the system\n");
printf("6- Show all Students in a class (Sequentially or in Reverse)\n");
printf("7- Delete all Students in a class\n");
printf("8- Exit\n\n");
printf("Enter your selection: ");
switch(validMenuChoice(8))
{
case 1: //Add student
system("cls || clear");
printf("\tAdd Student\n");
printf("Name: ");
if(readName(name)){
delayMessage("\nError: Enter a valid name");
break;
}
printf("Class: ");
readClass(&classID);
strcpy(s.name,name);
s.classID=classID;
if (append(&ls,s) == 0)
delayMessage("Student successfully added!");
else
delayMessage("\nError: An error occurred");
break;
case 2: //Delete Student
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
DeleteStudentMenu(&ls);
break;
case 3: //Edit Student
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
EditStudentMenu(&ls);
break;
case 4: //Search Student
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
SearchStudentMenu(&ls);
break;
case 5:
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
system("cls || clear");
printf("\tShow all Students:\n");
TraverseList(&ls, printStudent);
delayMessage("Press any key to continue");
break;
case 6: //Show all Students in a class
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
ShowStudentsMenu(&ls);
break;
case 7: //Delete all Students in a class
if (listEmpty(&ls))
{
delayMessage("\nError: List is empty");
break;
}
do
{
system("cls || clear");
printf("\tDelete class\n");
printf("Class: ");
readClass(&classID);
flag = DeleteFromClass(&ls, classID);
if (flag)
delayMessage("Class successfully deleted!");
else
{
delayMessage("\nError: Class doesn't exist");
break;
}
} while (!flag);
break;
case 8:
exit(0);
break;
default:
delayMessage("\nError: enter a valid input");
}
}
return 0;
}