#include #include #include #include"Array.h" void print_blanks(int n) { for(int i = 0; i < n; i ++) putchar(' '); } int make_sure(){ char s[200]; scanf("%s", s); return strcmp(s, "yes") == 0; } struct StuInfo { int id; char *clas; char *name; double ma, ph, eg, sum; }; struct Array info; #define psf PAT(struct StuInfo, p) void Info_Initialize(){ Array_create(&info, sizeof(struct StuInfo)); } void Info_release_(void *p){ free(((struct StuInfo*)p)->clas); free(((struct StuInfo*)p)->name); } void Info_Release(){ Array_release(&info, Info_release_); } void Info_add(int id,const char *const clas, const char *const name, double ma, double ph, double eg){ struct StuInfo stu = {id, NULL, NULL, ma, ph, eg, ma+ph+eg}; stu.clas = malloc_s_strcpy(clas); stu.name = malloc_s_strcpy(name); Array_insert(&info, &stu, info.size); } void Info_Add() { printf("Info Adding Guide:\nPlease type in Student's \n[id, class, name, math score, physics score, english score] in orders\n"); char tmp[200]; struct StuInfo stu; do{ printf("Id:");scanf("%d", &stu.id); printf("Class:");scanf("%s", tmp); stu.clas = malloc_s_strcpy(tmp); printf("Name:");scanf("%s", tmp); stu.name = malloc_s_strcpy(tmp); printf("Math Score:");scanf("%lf", &stu.ma); printf("Physics Score:");scanf("%lf", &stu.ph); printf("English Score:");scanf("%lf", &stu.eg); stu.sum += stu.ma + stu.ph + stu.eg; Array_insert(&info, &stu, info.size); printf("Continue to Add?[yes/no]\n"); }while(make_sure()); printf("Info Adding Done.\n"); } void info_format_print(struct Array_ *p){ printf("%d %s %s %2.1lf %2.1lf %2.1lf %2.1lf\n",psf->id,psf->clas,psf->name,psf->ma,psf->ph,psf->eg,psf->sum); } void Info_Remove() { printf("Remove Guide:\nType in [id] of the students you want to remove\n"); int num, ord = 0, stk[200], tp = 0; scanf("%d", &num); Range(info){ ord++; if(psf->id == num) stk[tp ++] = ord; } if(tp){ printf("Found:%d\n", tp); Range(info){ ord++; if(psf->id == num) info_format_print(p); } if(tp != 1) scanf("%d", &num); else num = 1; if(num > 0 && num < tp){ printf("Make Sure To Remove?[yes/no]"); if(make_sure()){ Array_delete(&info, stk[num-1]); printf("Remove Done\n"); }else printf("Canceling the option\n"); }else printf("illegal input of the Index:\n"); }else printf("Student [id:%d] Not Found\n", num); } void Info_Show() { printf("Here lists all infomation stored in the system:\n"); Range(info) info_format_print(p); printf("[End]\n"); } void Info_Quiry() { printf("Qury Guide:\nType in [id] of the students you are looking for\n"); int num, ord = 0, stk[200], tp = 0; scanf("%d", &num); Range(info){ ord++; if(psf->id == num) stk[tp ++] = ord; } if(tp){ printf("Found:%d\n", tp); Range(info){ ord++; if(psf->id == num) info_format_print(p); } }else printf("Student [id:%d] Not Found\n", num); } int Info_sortById_a(void *a, void *b){ return ((struct StuInfo*)a)->id - ((struct StuInfo*)b)->id; } int Info_sortByClass_a(void *a, void *b){ return strcmp(((struct StuInfo*)a)->clas, ((struct StuInfo*)b)->clas); } int Info_sortByName_a(void *a, void *b){ return strcmp(((struct StuInfo*)a)->name, ((struct StuInfo*)b)->name); } int Info_sortBySumScore_a(void *a, void *b){ return ((struct StuInfo*)a)->sum - ((struct StuInfo*)b)->sum; } void Info_SortByKeys() { int keys, order = 0; printf("Sort Guide:\nType in Keys:\n"); printf("1.id\n2.class\n3.name\n4.total score\n"); scanf("%d", &keys); printf("Choose the sort order:\n1.ascend\n2.descend\n"); scanf("%d", &order); int (*func)(void *, void *); switch(keys){ case 1:func = Info_sortById_a;break; case 2:func = Info_sortByClass_a;break; case 3:func = Info_sortByName_a;break; case 4:func = Info_sortBySumScore_a;break; default: printf("Illegal Input\n"); return; } Array_sort(&info, func, order >= 2); printf("Sort Finished\n"); Info_Show(); } struct Opt{ char *opt; void (*func)(); }; struct Array Cmds; void Opts_Append(const char *const opt, void (*func)()){ struct Opt optt = {NULL, func}; optt.opt = malloc_s_strcpy(opt); Array_insert(&Cmds, &optt, Cmds.size); } void Opts_Initialize(){ Array_create(&Cmds, sizeof(struct Opt)); Opts_Append("Quiry", Info_Quiry); Opts_Append("Add", Info_Add); Opts_Append("Remove", Info_Remove); Opts_Append("Show", Info_Show); Opts_Append("SortByKeys", Info_SortByKeys); } void Opts_release_(void *p){ free(((struct Opt *)p)->opt); } void Opts_Release(){ Array_release(&Cmds, Opts_release_); } int Opts_Select(){ int i=0; Range(Cmds) printf("%d.%s\n", ++i, PAT(struct Opt, p)->opt); printf("%d.Quit\n", ++i); int selectNum = 0; scanf("%d", &selectNum); if(selectNum > i || selectNum < 1){ printf("Error Input:%d\n", selectNum); return 1; }else if(selectNum == i){ printf("Quit From System...\n"); return 0; }else PAT(struct Opt, Array(&Cmds, selectNum-1))->func(); return 1; } int main(){ Opts_Initialize();Info_Initialize(); Info_add(1, "CLASS_TEST_1", "TEST_2", 100, 100, 100); Info_add(2, "CLASS_TEST_2", "TEST_4", 20, 0, 60); Info_add(3, "CLASS_TEST_3", "TEST_1", 5, 53, 100); while(Opts_Select()); Info_Release();Opts_Release(); printf("Done\n"); }