-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
1485 lines (1485 loc) · 54 KB
/
function.cpp
File metadata and controls
1485 lines (1485 loc) · 54 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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include"header.h"
//main linked list
user*User = NULL;
course*Course = NULL;
classes*Classes = NULL;
//support function
void checkcinfail() {
if (cin.fail()) {
cin.clear();
cin.ignore(INT_MAX, '\n');
}
}
int lengthNum(int a) {
return a == 0 ? 0 : lengthNum(a / 10) + 1;
}
char lowercase(char a) {
return (a > 64 && a < 91) ? a + 32 : a;
}
char changecase(char a) {
return (a > 64 && a < 91) ? a + 32 : a - 32;
}
bool compareWithOutCase(char*a, char*b) {
if (strlen(a) > strlen(b) || strlen(b) > strlen(a)) return false;
for (int i = 0; i < strlen(a); ++i)
if (a[i] != b[i] && a[i] != changecase(b[i])) return false;
return true;
}
void makesortname(char*name, char*username) {
int i = 0, j = 0, k; //i vs name, j vs username, k vs end.
char end[10];
while (i < strlen(name)) {
while (i < strlen(name) && name[i] == ' ') ++i;
if (i < strlen(name)) username[j++] = lowercase(name[i++]);
for (k = 0; i < strlen(name) && name[i] != ' '; ++i, ++k) end[k] = lowercase(name[i]);
end[k] = '\0';
}
for (int k = 0; k < strlen(end); ++k, ++j) username[j] = end[k];
username[j] = '\0';
}
//form function
void inputForm(char*title, char*var, int varLimit, int titleRange) {
cout << '|' << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl;
cout << '|' << setw(titleRange / 2 + strlen(title) / 2) << title << setw(titleRange / 2 + titleRange % 2 - strlen(title) / 2) << '|' << setw((wholeRange - titleRange) / 2 - varLimit / 2) << " ";
cin.getline(var, varLimit); checkcinfail();
cout << '|' << setfill('_') << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl << setfill(' ');
}
void inputForm(char*title, float &var, int titleRange) {
cout << '|' << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl;
cout << '|' << setw(titleRange / 2 + strlen(title) / 2) << title << setw(titleRange / 2 + titleRange % 2 - strlen(title) / 2) << '|' << " ";
cin >> var; cin.clear(); cin.ignore(INT_MAX, '\n');
cout << '|' << setfill('_') << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl << setfill(' ');
}
void optionForm(char*title, char &var, int titleRange) {
cout << '|' << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl;
cout << '|' << setw(titleRange / 2 + strlen(title) / 2) << title << setw(titleRange / 2 + titleRange % 2 - strlen(title) / 2) << '|' << " ";
cin >> var; cin.ignore(INT_MAX, '\n'); var -= 48;
cout << '|' << setfill('_') << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl << setfill(' ');
}
void showForm(char*title, char*var, int titleRange) {
cout << '|' << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl;
cout << '|' << setw(titleRange / 2 + strlen(title) / 2) << title << setw(titleRange / 2 + titleRange % 2 - strlen(title) / 2) << '|' << setw((wholeRange - titleRange) / 2 + strlen(var) / 2) << var << setw((wholeRange - titleRange) / 2 + (wholeRange - titleRange) % 2 - strlen(var) / 2) << '|' << endl;
cout << '|' << setfill('_') << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl << setfill(' ');
}
void showForm(char*title, float var, int titleRange) {
cout << '|' << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl;
cout << '|' << setw(titleRange / 2 + strlen(title) / 2) << title << setw(titleRange / 2 + titleRange % 2 - strlen(title) / 2) << '|' << setw((wholeRange - titleRange) / 2 + 1) << var << setw((wholeRange - titleRange) / 2 + (wholeRange - titleRange) % 2 - 1) << '|' << endl;
cout << '|' << setfill('_') << setw(titleRange) << '|' << setw(wholeRange - titleRange) << '|' << endl << setfill(' ');
}
void showForm(char*title) {
cout << '|' << setw(wholeRange) << '|' << endl;
cout << '|' << setw(wholeRange / 2 + strlen(title) / 2) << title << setw(wholeRange / 2 + wholeRange % 2 - strlen(title) / 2) << '|' << endl;
cout << '|' << setfill('_') << setw(wholeRange) << '|' << endl << setfill(' ');
}
void menuForm(int num, char*title) {
cout << '|' << setw(numRange) << '|' << setw(wholeRange - numRange) << '|' << endl;
cout << '|' << setw(numRange / 2 + lengthNum(num) / 2) << num << setw(numRange / 2 - lengthNum(num) / 2) << '|' << setw((wholeRange - numRange) / 2 + strlen(title) / 2) << title << setw((wholeRange - numRange) / 2 - strlen(title) / 2) << '|' << endl;
cout << '|' << setfill('_') << setw(numRange) << '|' << setw(wholeRange - numRange) << '|' << endl << setfill(' ');
}
void notifyForm(char*title) {
cout << R"(| /\ /\ )" << setw(wholeRange - 10) << '|' << endl;
cout << R"(| \/ /\ \/ )" << setw(wholeRange / 2 + strlen(title) / 2 - 10) << title << setw(wholeRange / 2 - strlen(title) / 2) << '|' << endl;
cout << R"(|____\/____)" << setfill('_') << setw(wholeRange - 10) << '|' << endl << setfill(' ');
}
void start() {
system("cls");
cout << ' ' << setfill('_') << setw(wholeRange) << ' ' << endl << setfill(' ');
}
void start(user admin) {
system("cls");
cout << "Welcome " << admin.fullname << "!\n";
cout << ' ' << setfill('_') << setw(wholeRange) << ' ' << endl << setfill(' ');
}
void done() {
notifyForm("Your procedure has been done!");
cin.ignore(INT_MAX, '\n');
}
//check function
bool isExistUser(char*username) {
for (user*cur = User; cur != NULL; cur = cur->next)
if (!strcmp(username, cur->username)) return true;
return false;
}
bool isExistUserLecturer(char*fullname) {
for (user*cur = User; cur != NULL; cur = cur->next)
if (compareWithOutCase(fullname, cur->fullname) && cur->type == user::lecturer) return true;
return false;
}
bool isExistStudentInCourse(course*cur, char*ID) {
for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next)
if (!strcmp(curstu->ID, ID)) return true;
return false;
}
bool isExistStudentInCourse(course*cur, char*ID, course::student *&match) {
for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next)
if (!strcmp(curstu->ID, ID)) {
match = curstu;
return true;
}
return false;
}
bool isExistStudentInClass(classes*cur, char*ID) {
for (classes::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next)
if (!strcmp(curstu->ID, ID)) return true;
return false;
}
bool isExistStudent(char*ID) {
for (classes*cur = Classes; cur != NULL; cur = cur->next)
for (classes::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next)
if (!strcmp(curstu->ID, ID)) return true;
return false;
}
bool isExistCourse(char*courseCode, char*year) {
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) return true;
return false;
}
bool isExistClass(char*classCode, classes*&match) {
for (classes*curcla = Classes; curcla != NULL; curcla = curcla->next)
if (compareWithOutCase(curcla->Class, classCode)) {
match = curcla;
return true;
}
return false;
}
//file and linked list function
void initAndShowUser() {
cout << "FILE " << fileUser << endl;
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileUser);
ifstream fin(file, ios_base::binary);
for (user tmp, *cur; fin.read((char*)(&tmp), sizeof(user) - sizeof(user*));) {
if (User == NULL) {
cout << setw(14) << "Catagory" << ' ';
cout << setw(usernameLimit) << "Username" << ' ';
cout << setw(fullnameLimit) << "Fullname" << ' ';
cout << setw(emailLimit) << "Email" << ' ';
cout << setw(mobileLimit) << "Mobile" << ' ';
cout << setw(passwordLimit) << "Password" << ' ';
cout << setw(ClassLimit) << "Class" << endl;
cur = User = new user;
}
else cur = cur->next = new user;
*cur = tmp;
switch (cur->type) {
case 0:cout << setw(14) << "Student" << ' '; break;
case 1:cout << setw(14) << "Academic Staff" << ' '; break;
case 2:cout << setw(14) << "Lecturer" << ' '; break;
}
cout << setw(usernameLimit) << cur->username << ' ';
cout << setw(fullnameLimit) << cur->fullname << ' ';
cout << setw(emailLimit) << cur->email << ' ';
cout << setw(mobileLimit) << cur->mobile << ' ';
cout << setw(passwordLimit) << cur->password << ' ';
if (cur->type == 0) cout << setw(ClassLimit) << cur->Class;
cout << endl;
}
fin.close();
}
void initAndShowCourse() {
cout << "FILE " << fileCourse << endl;
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileCourse);
ifstream fin(file, ios_base::binary);
for (course tmp, *cur; fin.read((char*)(&tmp), sizeof(course) - sizeof(course::student*) - sizeof(course*));) {
if (Course == NULL) {
cout << setw(courseCodeLimit) << "Coursecode" << ' ';
cout << setw(yearLimit) << "Year" << ' ';
cout << setw(8) << "Semester" << ' ';
cout << setw(courseNameLimit) << "Course name" << ' ';
cout << setw(16) << "LecturerUsername" << ' ';
cout << setw(dayLimit) << "Start at" << ' ';
cout << setw(dayLimit) << "End at" << ' ' << endl;
cur = Course = new course;
}
else cur = cur->next = new course;
*cur = tmp;
cout << setw(courseCodeLimit) << cur->courseCode << ' ';
cout << setw(yearLimit) << cur->year << ' ';
cout << setw(8) << cur->semester << ' ';
cout << setw(courseNameLimit) << cur->courseName << ' ';
cout << setw(16) << cur->lecturerUsername << ' ';
cout << setw(dayLimit) << cur->startAt << ' ';
cout << setw(dayLimit) << cur->endAt << ' ' << endl;
for (int i = 0; i < 6; ++i)
if (strcmp(cur->day[i].from, "") && strcmp(cur->day[i].to, ""))
cout << "Thu " << i + 2 << ": " << cur->day[i].from << ' ' << cur->day[i].to << endl;
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathCourse);
strcat(file, "/"); strcat(file, cur->courseCode); strcat(file, "-"); strcat(file, cur->year);
ifstream finstu(file, ios_base::binary);
for (course::student tmpstu, *curstu; finstu.read((char*)(&tmpstu), sizeof(course::student) - sizeof(course::student*));) {
if (cur->stuhead == NULL) curstu = cur->stuhead = new course::student;
else curstu = curstu->next = new course::student;
*curstu = tmpstu;
cout << curstu->ID << ' ' << curstu->mid << ' ' << curstu->lab << ' ' << curstu->final << ' ' << curstu->timecheck << endl;
for (int i = 0; i < curstu->timecheck; ++i) {
char*time=ctime(&curstu->checktime[i]);
cout << "Time " << i + 1 << ": " << time;
}
}
finstu.close();
}
fin.close();
}
void initAndShowClass() {
cout << "FILE " << fileClass << endl;
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileClass);
ifstream fin(file);
classes *cur;
for (char Class[ClassLimit]; fin.getline(Class, ClassLimit);) {
if (Classes == NULL) cur = Classes = new classes;
else cur = cur->next = new classes;
strcpy(cur->Class, Class);
cout << cur->Class << ' ';
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathClass);
strcat(file, "/"); strcat(file, cur->Class);
ifstream finstu(file);
classes::student *curstu;
int num = 0;
for (char ID[usernameLimit]; finstu.getline(ID, usernameLimit); ++num) {
if (cur->stuhead == NULL) curstu = cur->stuhead = new classes::student;
else curstu = curstu->next = new classes::student;
strcpy(curstu->ID, ID);
}
cout << "The number of students: " << num << endl;
finstu.close();
}
fin.close();
}
void delAndSaveUser() {
if (User != NULL) {
_mkdir(pathMain);
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileUser);
ofstream fout(file, ios_base::binary);
while (User != NULL) {
fout.write((char*)User, sizeof (user) - sizeof(user*));
user *tmp = User;
User = User->next;
delete tmp;
}
fout.close();
}
}
void delAndSaveCourse() {
if (Course != NULL) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileCourse);
ofstream fout(file, ios_base::binary);
while (Course != NULL) {
fout.write((char*)Course, sizeof(course) - sizeof(course::student*) - sizeof(course*));
if (Course->stuhead != NULL) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathCourse);
_mkdir(file);
strcat(file, "/"); strcat(file, Course->courseCode); strcat(file, "-"); strcat(file, Course->year);
ofstream foutstu(file, ios_base::binary);
while (Course->stuhead != NULL) {
foutstu.write((char*)Course->stuhead, sizeof(course::student) - sizeof(course::student*));
course::student *tmp = Course->stuhead;
Course->stuhead = Course->stuhead->next;
delete tmp;
}
foutstu.close();
}
course *tmp = Course;
Course = Course->next;
delete tmp;
}
fout.close();
}
}
void delAndSaveClass() {
if (Classes != NULL) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, fileClass);
ofstream fout(file);
while (Classes != NULL) {
fout << Classes->Class << endl;
if (Classes->stuhead != NULL) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathClass);
_mkdir(file);
strcat(file, "/"); strcat(file, Classes->Class);
ofstream foutstu(file);
while (Classes->stuhead != NULL) {
foutstu << Classes->stuhead->ID << endl;
classes::student *tmp = Classes->stuhead;
Classes->stuhead = Classes->stuhead->next;
delete tmp;
}
foutstu.close();
}
classes *tmp = Classes;
Classes = Classes->next;
delete tmp;
}
fout.close();
}
}
//add to linked list function
void creatAcc(user::category type, char*username, char*fullname, char*Class) {
if (!isExistUser(username)) {
user*cur = User;
while (cur->next != NULL) cur = cur->next;
cur->next = new user;
cur->next->type = type;
strcpy(cur->next->username, username);
strcpy(cur->next->password, "123123");
strcpy(cur->next->fullname, fullname);
strcpy(cur->next->Class, Class);
}
}
void addCourse(course tmp, char*fullname) {
course*cur = Course;
if (Course == NULL) cur = Course = new course, *Course = tmp;
else {
while (cur->next != NULL) cur = cur->next;
cur = cur->next = new course;
*cur = tmp;
}
if (!isExistUserLecturer(fullname)) {
if (isExistUser(cur->lecturerUsername)) {
char username[usernameLimit], n[]{ "0" };
do {
++n[0];
strcpy(username, cur->lecturerUsername);
strcat(username, n);
} while (isExistUser(username));
strcpy(cur->lecturerUsername, username);
}
creatAcc(user::lecturer, cur->lecturerUsername, fullname, "");
}
}
void addStudentToClass(classes*cur, char*ID) {
if (cur->stuhead == NULL) cur->stuhead = new classes::student, strcpy(cur->stuhead->ID, ID);
else {
classes::student*curstu = cur->stuhead;
while (curstu->next != NULL) curstu = curstu->next;
curstu->next = new classes::student;
strcpy(curstu->next->ID, ID);
}
creatAcc(user::student, ID, "", cur->Class);
}
void addStudentToCourse(course*cur, course::student tmp) {
if (cur->stuhead == NULL) cur->stuhead = new course::student, *cur->stuhead = tmp;
else {
course::student*curstu = cur->stuhead;
while (curstu->next != NULL) curstu = curstu->next;
curstu->next = new course::student;
*curstu->next = tmp;
}
}
//import course
bool addNewCourse() {
start();
notifyForm("Add a new course");
course tmp;
inputForm("Course code", tmp.courseCode, courseCodeLimit, 16);
inputForm("Academic year", tmp.year, yearLimit, 16);
if (isExistCourse(tmp.courseCode, tmp.year)) {
notifyForm("This course is existed in database!");
char isContinue;
optionForm("Do you want to continue import course(0 is no, 1 is yes)", isContinue, 60);
if (!isContinue) return true;
return false;
}
inputForm("Semester", tmp.semester, semesterLimit, 16);
inputForm("Course name", tmp.courseName, courseNameLimit, 16);
char fullname[fullnameLimit];
inputForm("Lecturer name", fullname, fullnameLimit, 16);
makesortname(fullname, tmp.lecturerUsername);
inputForm("This course start at (yyyy-mm-dd)", tmp.startAt, dayLimit, 35);
inputForm("This course finish at (yyyy-mm-dd)", tmp.endAt, dayLimit, 35);
notifyForm("Import Course Successful!");
addCourse(tmp, fullname);
return true;
}
bool importCourseCsv() {
start();
notifyForm("Import course from csv");
char path[200];
inputForm("Direct path(Each line is courseCode, year, semester, courseName, lecturerUsername, startAt, endAt)", path, 200, 60);
ifstream finIm(path);
if (finIm.is_open()) {
for (course tmp; finIm.getline(tmp.courseCode, courseCodeLimit, ',') && strcmp(tmp.courseCode, "");) {
finIm.getline(tmp.year, yearLimit, ',');
if (isExistCourse(tmp.courseCode, tmp.year)) {
finIm.ignore(INT_MAX, '\n');
char notify[100]{ "Course " };
strcat(notify, tmp.courseCode); strcat(notify, "-"); strcat(notify, tmp.year);
strcat(notify, " has been existed!");
notifyForm(notify);
}
else {
finIm.getline(tmp.semester, semesterLimit, ',');
finIm.getline(tmp.courseName, courseNameLimit, ',');
char fullname[fullnameLimit];
finIm.getline(fullname, fullnameLimit, ',');
makesortname(fullname, tmp.lecturerUsername);
finIm.getline(tmp.startAt, dayLimit, ',');
finIm.getline(tmp.endAt, dayLimit);
addCourse(tmp, fullname);
char notify[100]{};
strcat(notify, tmp.courseCode); strcat(notify, "-"); strcat(notify, tmp.year);
strcat(notify, " has been added!");
notifyForm(notify);
}
}
finIm.close();
return true;
}
else notifyForm("Can't open this file!");
finIm.close();
char isContinue;
optionForm("Do you want to continue import course(0 is no, 1 is yes)", isContinue, 60);
if (!isContinue) return true;
return false;
}
void viewCourse() {
start();
notifyForm("View list of course");
if (Course == NULL) {
notifyForm("No course has been imported yet!");
}
else {
ostringstream str;
str << setw(courseCodeLimit) << "Coursecode" << " | ";
str << setw(yearLimit) << "Year" << " | ";
str << setw(8) << "Semester" << " | ";
str << setw(35) << "Course name" << " | ";
str << setw(16) << "LecturerUsername" << " | ";
str << setw(dayLimit) << "Start at" << " | ";
str << setw(dayLimit) << "End at";
char notify[200]{};
strcpy(notify, str.str().c_str());
showForm(notify);
for (course*cur = Course; cur != NULL; cur = cur->next) {
ostringstream str;
str << setw(courseCodeLimit) << cur->courseCode << " | ";
str << setw(yearLimit) << cur->year << " | ";
str << setw(8) << cur->semester << " | ";
str << setw(35) << cur->courseName << " | ";
str << setw(16) << cur->lecturerUsername << " | ";
str << setw(dayLimit) << cur->startAt << " | ";
str << setw(dayLimit) << cur->endAt;
strcpy(notify, str.str().c_str());
showForm(notify);
}
}
}
bool importCourse() {
start();
notifyForm("Import Course");
menuForm(1, "Add a new course");
menuForm(2, "Import course from csv");
menuForm(3, "View list of course");
menuForm(4, "Back");
char inputStyle;
optionForm("Your choice", inputStyle, 20);
switch (inputStyle) {
case 1:while (!addNewCourse()); break;
case 2:while (!importCourseCsv()); break;
case 3:viewCourse(); break;
case 4:return true;
default:return false;
}
done();
return false;
}
//import student to a class and view
bool importListClass() {
start();
notifyForm("Import list of students in a class");
char Class[ClassLimit];
inputForm("Class which you want to import students", Class, ClassLimit, 55);
char path[200];
inputForm("Direct path (Each line is student ID)", path, 200, 40);
ifstream fin(path);
if (fin.is_open()) {
classes*curcla;
if (!isExistClass(Class, curcla)) {
if (Classes == NULL) curcla = Classes = new classes;
else {
classes*cur = Classes;
while (cur->next != NULL) cur = cur->next;
curcla = cur->next = new classes;
}
strcpy(curcla->Class, Class);
}
for (char ID[usernameLimit]; fin.getline(ID, usernameLimit);) {
if (isExistStudentInClass(curcla, ID)) {
char notify[100]{ "Student " };
strcat(notify, ID); strcat(notify, " has been existed in this class!");
notifyForm(notify);
}
else if (isExistStudent(ID)) {
char notify[100]{ "Student " };
strcat(notify, ID); strcat(notify, " has been existed in other class!");
notifyForm(notify);
}
else {
addStudentToClass(curcla, ID);
char notify[100]{ "Imported " };
strcat(notify, ID); strcat(notify, " to "); strcat(notify, Class);
strcat(notify, " successful!");
notifyForm(notify);
}
}
fin.close();
return true;
}
else notifyForm("This file cannot open!");
fin.close();
char isContinue;
optionForm("Do you want to continue import students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool viewStudentsCourse() {
start();
notifyForm("View students in a course");
char courseCode[courseCodeLimit];
inputForm("Course code which you want to view students", courseCode, courseCodeLimit, 55);
char year[yearLimit];
inputForm("Year of your course", year, yearLimit, 55);
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) {
if (cur->stuhead == NULL) notifyForm("This course doesn't have any students!");
else for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next) notifyForm(curstu->ID);
return true;
}
notifyForm("Cannot found your course!");
char isContinue;
optionForm("Do you want to continue view students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool viewStudentsClass() {
start();
notifyForm("View students in a class");
char Class[ClassLimit];
inputForm("Class which you want to view students", Class, courseCodeLimit, 55);
classes*cur;
if (isExistClass(Class, cur)) {
if (cur->stuhead == NULL) notifyForm("This class doesn't have any students!");
else for (classes::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next) notifyForm(curstu->ID);
return true;
}
notifyForm("Cannot found your class!");
char isContinue;
optionForm("Do you want to continue view students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
//import student to a course
bool assignWholeClass(course*admin) {
start();
char notify[100]{ "Assign students to " };
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
notifyForm(notify);
notifyForm("Choose an existing class");
char Class[ClassLimit];
inputForm("Class which you want to assign students", Class, courseCodeLimit, 55);
classes*cur;
if (isExistClass(Class, cur)) {
if (cur->stuhead == NULL) notifyForm("This class doesn't have any students!");
else for (classes::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next) {
if (isExistStudentInCourse(admin, curstu->ID)) {
char notify[100]{ "Student " };
strcat(notify, curstu->ID);
strcat(notify, " has been existed in this course!");
notifyForm(notify);
}
else {
course::student tmp;
strcpy(tmp.ID, curstu->ID);
addStudentToCourse(admin, tmp);
char notify[100]{ "Assigned " };
strcat(notify, curstu->ID); strcat(notify, " to ");
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
notifyForm(notify);
}
}
return true;
}
notifyForm("Cannot found your class!");
char isContinue;
optionForm("Do you want to continue view students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool addnewImportCsv(course*admin) {
start();
char notify[100]{ "Assign students to " };
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
notifyForm(notify);
notifyForm("Add a new class name and import student from csv");
char Class[ClassLimit];
inputForm("Class which you want to import students", Class, ClassLimit, 55);
classes*curcla;
if (!isExistClass(Class, curcla)) {
char path[200];
inputForm("Direct path (Each line is student ID)", path, 200, 40);
ifstream fin(path);
if (fin.is_open()) {
if (Classes == NULL) curcla = Classes = new classes;
else {
classes*cur = Classes;
while (cur->next != NULL) cur = cur->next;
curcla = cur->next = new classes;
}
strcpy(curcla->Class, Class);
char ID[usernameLimit];
while (fin.getline(ID, usernameLimit)) {
if (isExistStudent(ID)) {
char notify[100]{ "Student " };
strcat(notify, ID); strcat(notify, " has been existed in other class!");
notifyForm(notify);
}
else {
addStudentToClass(curcla, ID);
course::student tmp;
strcpy(tmp.ID, ID);
addStudentToCourse(admin, tmp);
char notify[100]{ "Imported " };
strcat(notify, ID); strcat(notify, " to "); strcat(notify, Class);
strcat(notify, " & "); strcat(notify, admin->courseCode);
strcat(notify, "-"); strcat(notify, admin->year);
strcat(notify, " successful!");
notifyForm(notify);
}
}
fin.close();
return true;
}
else notifyForm("This file cannot open!");
fin.close();
}
else notifyForm("This class has been existed!");
char isContinue;
optionForm("Do you want to continue import students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool manuallyAddExist(course*admin) {
start();
char notify[100]{ "Assign students to " };
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
notifyForm(notify);
notifyForm("Manually search and add an existing student");
char ID[usernameLimit];
inputForm("Student ID", ID, usernameLimit, 55);
for (classes*curcla = Classes; curcla != NULL; curcla = curcla->next)
if (isExistStudentInClass(curcla, ID)) {
if (isExistStudentInCourse(admin, ID)) {
char notify[100]{ "Student " };
strcat(notify, ID); strcat(notify, " has been existed in this course!");
notifyForm(notify);
}
else {
course::student tmp;
strcpy(tmp.ID, ID);
addStudentToCourse(admin, tmp);
char notify[100]{ "Imported " };
strcat(notify, ID); strcat(notify, "-"); strcat(notify, curcla->Class);
strcat(notify, " to ");
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
strcat(notify, " successful!");
notifyForm(notify);
}
return true;
}
notifyForm("Cannot found this student from any classes!");
char isContinue;
optionForm("Do you want to continue import students(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool assignStudentsMenu(course*admin) {
start();
char notify[100]{ "Assign students to " };
strcat(notify, admin->courseCode); strcat(notify, "-"); strcat(notify, admin->year);
notifyForm(notify);
menuForm(1, "Choose an existing class");
menuForm(2, "Add a new class name and import student from csv");
menuForm(3, "Manually search and add an existing student");
menuForm(4, "Back");
char inputStyle;
optionForm("Your choice", inputStyle, 20);
switch (inputStyle) {
case 1:while (!assignWholeClass(admin)); break;
case 2:while (!addnewImportCsv(admin)); break;
case 3:while (!manuallyAddExist(admin)); break;
case 4:return true;
default:return false;
}
done();
return false;
}
bool assignStudentsToCourse() {
start();
notifyForm("Assign students to a course");
char courseCode[courseCodeLimit];
inputForm("Course code which you want to assign students", courseCode, courseCodeLimit, 55);
char year[yearLimit];
inputForm("Year of your course", year, yearLimit, 55);
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) {
while (!assignStudentsMenu(cur));
return true;
}
notifyForm("Cannot found your course!");
char isContinue;
optionForm("Do you want to continue assign students to a course(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool importStudents() {
start();
notifyForm("Import Students");
menuForm(1, "Import list of students in a class");
menuForm(2, "Assign students to a course");
menuForm(3, "View students in a course");
menuForm(4, "View students in a class");
menuForm(5, "Back");
char inputStyle;
optionForm("Your choice", inputStyle, 20);
switch (inputStyle) {
case 1:while (!importListClass()); break;
case 2:while (!assignStudentsToCourse()); break;
case 3:while (!viewStudentsCourse()); break;
case 4:while (!viewStudentsClass()); break;
case 5:return true;
default:return false;
}
done();
return false;
}
//import schedules - input "" and "" not overwrite old schedules
bool importSchedules() {
start();
notifyForm("Import Schedules");
char courseCode[courseCodeLimit];
inputForm("Course code which you want to import schedules", courseCode, courseCodeLimit, 55);
char year[yearLimit];
inputForm("Year of your course", year, yearLimit, 55);
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) {
for (int i = 0; i < 6; i++) {
switch (i) {
case 0:notifyForm("Monday"); break;
case 1:notifyForm("Tuesday"); break;
case 2:notifyForm("Wednesday"); break;
case 3:notifyForm("Thursday"); break;
case 4:notifyForm("Friday"); break;
case 5:notifyForm("Saturday"); break;
}
char tmp[timeLimit];
inputForm("Starting time (HH:MM)", tmp, timeLimit, 30);
if (strcmp(tmp, "")) strcpy(cur->day[i].from, tmp);
inputForm("Ending time (HH:MM)", tmp, timeLimit, 30);
if (strcmp(tmp, "")) strcpy(cur->day[i].to, tmp);
}
notifyForm("Import Schedules Successful!");
return true;
}
notifyForm("Cannot found your course!");
char isContinue;
optionForm("Do you want to continue import schedules(0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
//export score
bool exportScoreCourse() {
start();
notifyForm("Export score of a course");
char courseCode[courseCodeLimit];
inputForm("Course code which you want to show score", courseCode, courseCodeLimit, 55);
char year[yearLimit];
inputForm("Year of your course", year, yearLimit, 55);
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) {
if (cur->stuhead == NULL) notifyForm("This course hasn't been imported students yet!");
else {
char n;
optionForm("Which score do you want to export?(0 is midterm score, 1 is laboratory score, 2 is final score, 3 is all score)", n, 60);
if (n != 0 && n != 1 && n != 2 && n != 3) {
notifyForm("This option is not available! - Press Enter to come back!");
return true;
}
char exportForm;
optionForm("Export to screen(0) or file(1)", exportForm, 55);
if (exportForm == 0) {
switch (n) {
case 0:showForm("Student ID | Midterm score"); break;
case 1:showForm("Student ID | Laboratory score"); break;
case 2:showForm("Student ID | Final score"); break;
case 3:showForm("Student ID | Midterm score | Laboratory score | Final score"); break;
}
for (course::student*tmp = cur->stuhead; tmp != NULL; tmp = tmp->next) {
ostringstream str;
str << setw(10) << tmp->ID << " | ";
switch (n) {
case 0:str << setw(13) << tmp->mid; break;
case 1:str << setw(16) << tmp->lab; break;
case 2:str << setw(11) << tmp->final; break;
case 3:str << setw(13) << tmp->mid << " | ";
str << setw(16) << tmp->lab << " | ";
str << setw(11) << tmp->final;
break;
}
char notify[100]{};
strcpy(notify, str.str().c_str());
showForm(notify);
}
}
else if (exportForm == 1) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathScore);
_mkdir(file);
strcat(file, "/"); strcat(file, courseCode); strcat(file, "-"); strcat(file, year);
switch (n) {
case 0:strcat(file, "(mid)"); break;
case 1:strcat(file, "(lab)"); break;
case 2:strcat(file, "(final)"); break;
case 3:strcat(file, "(all)"); break;
}
strcat(file, ".csv");
ofstream fout(file);
switch (n) {
case 0:fout << "Student ID,Mid score\n"; break;
case 1:fout << "Student ID,Lab score\n"; break;
case 2:fout << "Student ID,Final score\n"; break;
case 3:fout << "Student ID,Mid score,Lab score,Final score\n"; break;
}
for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next)
switch (n) {
case 0:fout << curstu->ID << ',' << curstu->mid << endl; break;
case 1:fout << curstu->ID << ',' << curstu->lab << endl; break;
case 2:fout << curstu->ID << ',' << curstu->final << endl; break;
case 3:fout << curstu->ID << ',' << curstu->mid << ',' << curstu->lab << ',' << curstu->final << endl; break;
}
fout.close();
char notify[100]{ "File has been saved in " };
strcat(notify, file);
notifyForm(notify);
}
else notifyForm("This option is not available! - Press Enter to come back!");
}
return true;
}
notifyForm("This course is not existed in database!");
char isContinue;
optionForm("Do you want to try export score again (0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool exportScoreStudent() {
start();
notifyForm("Export score of a student");
char ID[usernameLimit];
inputForm("Student ID which you want to export score", ID, usernameLimit, 55);
bool anyCourse = false;
char exportForm;
for (course*cur = Course; cur != NULL; cur = cur->next) {
course::student*curstu;
if (isExistStudentInCourse(cur, ID, curstu)) {
if (!anyCourse) optionForm("Export to screen(0) or file(1)", exportForm, 55);
if (exportForm == 0) {
if (!anyCourse) showForm("Course code | Course year | Midterm score | Laboratory score | Final score");
ostringstream str;
str << setw(11) << cur->courseCode << " | ";
str << setw(11) << cur->year << " | ";
str << setw(13) << curstu->mid << " | ";
str << setw(16) << curstu->lab << " | ";
str << setw(11) << curstu->final;
char notify[100]{};
strcpy(notify, str.str().c_str());
showForm(notify);
}
else if (exportForm == 1) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathScore);
_mkdir(file);
strcat(file, "/"); strcat(file, ID); strcat(file, ".csv");
ofstream fout;
if (!anyCourse) fout.open(file);
else fout.open(file, ios_base::app);
if (!anyCourse) fout << "Course code,Course year,Mid score,Lab score,Final score\n";
fout << cur->courseCode << ',' << cur->year << ',' << curstu->mid << ',' << curstu->lab << ',' << curstu->final << endl;
fout.close();
}
else {
notifyForm("This option is not available! - Press Enter to come back!");
return true;
}
anyCourse = true;
}
}
if (anyCourse) {
if (exportForm == 1) {
char notify[100]{ "File has been saved in data/score/" };
strcat(notify, ID); strcat(notify, ".csv");
notifyForm(notify);
}
return true;
}
notifyForm("No course has this student!");
char isContinue;
optionForm("Do you want to try export score again (0 is no, 1 is yes)", isContinue, 60);
if (isContinue) return false;
return true;
}
bool exportScore() {
start();
notifyForm("Export score");
menuForm(1, "Score of a course");
menuForm(2, "Score of a student");
menuForm(3, "Back");
char inputStyle;
optionForm("Your choice", inputStyle, 20);
switch (inputStyle) {
case 1:while (!exportScoreCourse()); break;
case 2:while (!exportScoreStudent()); break;
case 3:return true;
default:return false;
}
done();
return false;
}
//export presence
bool exportPresence() {
start();
notifyForm("Export lists of student who was present or absent in a course");
char courseCode[courseCodeLimit];
inputForm("Course code which you want to show score", courseCode, courseCodeLimit, 55);
char year[yearLimit];
inputForm("Year of your course", year, yearLimit, 55);
for (course*cur = Course; cur != NULL; cur = cur->next)
if (compareWithOutCase(cur->courseCode, courseCode) && compareWithOutCase(cur->year, year)) {
if (cur->stuhead == NULL) notifyForm("This course hasn't been imported students yet!");
else {
char exportForm;
optionForm("Export to screen(0) or file(1)", exportForm, 55);
if (exportForm == 0) {
showForm("Student ID | Time check | Last check");
for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next) {
ostringstream str;
str << setw(10) << curstu->ID << " | ";
str << setw(10) << curstu->timecheck << " | ";
if (curstu->timecheck != 0) {
tm*past=localtime(&curstu->checktime[curstu->timecheck - 1]);
char thatday[dayLimit];
strftime(thatday, dayLimit, "%Y-%m-%d", past);
str << setw(dayLimit) << thatday;
}
else str << setw(dayLimit) << ' ';
char notify[100]{};
strcpy(notify, str.str().c_str());
showForm(notify);
}
}
else if (exportForm == 1) {
char file[100]{}; strcat(file, pathMain); strcat(file, "/"); strcat(file, pathPresence);
_mkdir(file);
strcat(file, "/"); strcat(file, courseCode); strcat(file, "-"); strcat(file, year);
strcat(file, ".csv");
ofstream fout(file);
fout << "Student ID,Time check,Last check\n";
for (course::student*curstu = cur->stuhead; curstu != NULL; curstu = curstu->next) {
fout << curstu->ID << ',' << curstu->timecheck << ',';
if (curstu->timecheck != 0) {
tm*past=localtime(&curstu->checktime[curstu->timecheck - 1]);
char thatday[dayLimit];
strftime(thatday, dayLimit, "%Y-%m-%d", past);
fout << thatday;
}
fout << endl;
}
fout.close();
char notify[100]{ "File has been saved in " };
strcat(notify, file);
notifyForm(notify);
}
else notifyForm("This option is not available! - Press Enter to come back!");
}
return true;
}
notifyForm("This course is not existed in database!");