这个问题,使得人们仿佛又回到了字符界面的时代。
问题链接:UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)。
题意简述:学生成绩有关数据的增删统计等(具体内容参见原问题)。
问题分析:使用一个结构类型数组存储数据,对其中数据进行操作。字符时代的软件似乎就是这样,只是少了用文件来存储最终的结果。
程序说明:(略)。
AC的C语言程序如下:
/* UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang) */ #include <stdio.h> #include <string.h> #include <memory.h> #define MAXN 100 #define MAXLEN1 10 #define MAXLEN2 20 #define EPS 1e-5 struct student { char sid[MAXLEN1+1]; int cid; char name[MAXLEN2+1]; int score[5]; int removed; } all[MAXN+1]; int scount; void output_menu() { printf("Welcome to Student Performance Management System (SPMS). "); printf("1 - Add "); printf("2 - Remove "); printf("3 - Query "); printf("4 - Show ranking "); printf("5 - Show Statistics "); printf("0 - Exit "); } void add() { int dflag, i; for(;;) { printf("Please enter the SID, CID, name and four scores. Enter 0 to finish. "); struct student in; scanf("%s", in.sid); if(strcmp(in.sid, "0") == 0) break; scanf("%d", &in.cid); scanf("%s", in.name); scanf("%d", &in.score[1]); scanf("%d", &in.score[2]); scanf("%d", &in.score[3]); scanf("%d", &in.score[4]); dflag = 0; for(i=0; i<scount; i++) if(!all[i].removed && strcmp(in.sid, all[i].sid)==0) { printf("Duplicated SID. "); dflag = 1; continue; } if(!dflag) { in.score[0] = in.score[1] + in.score[2] + in.score[3] + in.score[4]; in.removed = 0; all[scount++] = in; } } } int rank(int x) { int t = all[x].score[0]; int high=0, i; for(i=0; i<scount; i++) if(!all[i].removed && all[i].score[0] > t) high++; return high + 1; } void queryremove(int flag) { char s[MAXLEN1+1]; int i; for(;;) { printf("Please enter SID or name. Enter 0 to finish. "); scanf("%s", s); if(strcmp(s, "0") == 0) break; int rcount = 0; for(i=0; i<scount; i++) { if(!all[i].removed && (strcmp(s, all[i].sid) == 0 || strcmp(s, all[i].name) == 0)) { if(flag){ printf("%d %s %d %s %d %d %d %d %d %.2f ", rank(i), all[i].sid, all[i].cid, all[i].name, all[i].score[1], all[i].score[2], all[i].score[3], all[i].score[4], all[i].score[0], all[i].score[0]/4.0+EPS); } else{ all[i].removed = 1; rcount++; } } } if(!flag) printf("%d student(s) removed. ", rcount); } } void output_score(int id, int type) { int sum=0, count1=0, count2=0, i; for(i=0; i<scount; i++) { if(!all[i].removed && (id == 0 || all[i].cid == id)) { sum += all[i].score[type]; if(all[i].score[type] >= 60) count1++; else count2++; } } printf("Average Score: %.2f ", count1+count2 == 0 ? 0 : sum*1.0/(count1+count2)+EPS); printf("Number of passed students: %d ", count1); printf("Number of failed students: %d ", count2); printf(" "); } void statistics() { int id, i, j; printf("Please enter class ID, 0 for the whole statistics. "); scanf("%d", &id); printf("Chinese "); output_score(id, 1); printf("Mathematics "); output_score(id, 2); printf("English "); output_score(id, 3); printf("Programming "); output_score(id, 4); printf("Overall: "); int okcount[5]; memset(okcount, 0, sizeof(okcount)); for(i=0; i<scount; i++) { if(!all[i].removed && (id == 0 || all[i].cid == id)) { int ok = 0; for(j=1; j<=4; j++) if(all[i].score[j] >= 60) ok++; okcount[ok]++; } } printf("Number of students who passed all subjects: %d ", okcount[4]); printf("Number of students who passed 3 or more subjects: %d ", okcount[3]+okcount[4]); printf("Number of students who passed 2 or more subjects: %d ", okcount[2]+okcount[3]+okcount[4]); printf("Number of students who passed 1 or more subjects: %d ", okcount[1]+okcount[2]+okcount[3]+okcount[4]); printf("Number of students who failed all subjects: %d ", okcount[0]); printf(" "); } int main(void) { int choice; scount = 0; for(;;) { output_menu(); scanf("%d", &choice); if(choice == 1) add(); else if(choice == 2) queryremove(0); else if(choice == 3) queryremove(1); else if(choice == 4) printf("Showing the ranklist hurts students' self-esteem. Don't do that. "); else if(choice == 5) statistics(); else if(choice == 0) break; } return 0; }