UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)
生活随笔
收集整理的這篇文章主要介紹了
UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這個問題,使得人們仿佛又回到了字符界面的時代。
問題鏈接:UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)。
題意簡述:學生成績有關數(shù)據(jù)的增刪統(tǒng)計等(具體內(nèi)容參見原問題)。
問題分析:使用一個結構類型數(shù)組存儲數(shù)據(jù),對其中數(shù)據(jù)進行操作。字符時代的軟件似乎就是這樣,只是少了用文件來存儲最終的結果。
程序說明:(略)。
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-5struct 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).\n\n");printf("1 - Add\n");printf("2 - Remove\n");printf("3 - Query\n");printf("4 - Show ranking\n");printf("5 - Show Statistics\n");printf("0 - Exit\n\n"); }void add() {int dflag, i;for(;;) {printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");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.\n");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.\n");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\n", 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.\n", 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++;elsecount2++;}}printf("Average Score: %.2f\n", count1+count2 == 0 ? 0 : sum*1.0/(count1+count2)+EPS);printf("Number of passed students: %d\n", count1);printf("Number of failed students: %d\n", count2);printf("\n"); }void statistics() {int id, i, j;printf("Please enter class ID, 0 for the whole statistics.\n");scanf("%d", &id);printf("Chinese\n");output_score(id, 1);printf("Mathematics\n");output_score(id, 2);printf("English\n");output_score(id, 3);printf("Programming\n");output_score(id, 4);printf("Overall:\n");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\n", okcount[4]);printf("Number of students who passed 3 or more subjects: %d\n", okcount[3]+okcount[4]);printf("Number of students who passed 2 or more subjects: %d\n", okcount[2]+okcount[3]+okcount[4]);printf("Number of students who passed 1 or more subjects: %d\n", okcount[1]+okcount[2]+okcount[3]+okcount[4]);printf("Number of students who failed all subjects: %d\n", okcount[0]);printf("\n"); }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.\n");else if(choice == 5)statistics();else if(choice == 0)break;}return 0; }
轉載于:https://www.cnblogs.com/tigerisland/p/7564305.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結
以上是生活随笔為你收集整理的UVA12412 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贴权和填权的区别
- 下一篇: php动态网页是什么意思?