#include <cstdio> #include <iostream> #include <cstring> using namespace std; const double EPS = 1e-5; //精确度 const int maxn = 1024; int cnt = 0; struct stu { string SID, name; int CID; int score[5]; bool removed; }all[maxn];//数组最多可以装maxn个学生 void print_menu() { cout << "Welcome to Student Performance Management System (SPMS)."; cout << endl << endl; cout << "1 - Add" << endl; cout << "2 - Remove" << endl; cout << "3 - Query" << endl; cout << "4 - Show ranking" << endl; cout << "5 - Show Statistics" << endl; cout << "0 - Exit" << endl; cout << endl; } int Rank(const int x)//按总分排名 { int t = all[x].score[0]; int high = 0; for(int i = 0; i < cnt; i++) if(!all[i].removed && all[i].score[0] > t) high++; //找学生中比这个人高的,统计人数 return high + 1; //排名为统计人数的下一个 } void add() { while(true) { loop: cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish." << endl; stu in; cin >> in.SID;//学生编号 if(in.SID == "0") break; cin >> in.CID >> in.name >> in.score[1] >> in.score[2] >> in.score[3] >> in.score[4]; for(int i = 0; i < cnt; i++) if((all[i].removed==0) && in.SID == all[i].SID) //若某个学生没有被删除,并且与 所注册的学生编号有重复 { cout << "Duplicated SID." << endl; goto loop; //loop: goto loop ------无条件转移语句 }/*最常见的用法是终止程序在某些深度嵌套 的结构中的处理过程, 例如一次跳出两层或多层循环 goto只能在函数体内跳转,不能跳到函数体外的函数。 即goto有局部作用域,需要在同一个栈内*/ in.score[0] = in.score[1] + in.score[2] + in.score[3] + in.score[4];//总分 in.removed = false; all[cnt++] = in; //cnt为学生总数 } } void DQ(const int isq) { while(true) { cout << "Please enter SID or name. Enter 0 to finish." << endl; string str; cin >> str; if(str == "0") break; int rem_cnt = 0; // 删除的总数 for(int i = 0; i < cnt; i++) { if(!all[i].removed && (str == all[i].SID || str == all[i].name)) { if(isq) //isq为 1表示查询,为 0表示删除 { //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. printf("%d %s %d %s %d %d %d %d %d %.2f\n", Rank(i), all[i].SID.c_str(), all[i].CID, all[i].name.c_str(), 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 = true; //标记删除的人数 rem_cnt++; //统计删除的人数 } } } if(!isq) printf("%d student(s) removed.\n", rem_cnt); } } //形参为班级编号,0表示为全年级 和成绩类型 ,输出某年级某门课程的信息函数 void output(const int ID, const int type) { int sum = 0, cnt1 = 0, cnt2 = 0; for(int i = 0; i < cnt; i++) { if(!all[i].removed && (ID==0 || all[i].CID==ID)) { sum += all[i].score[type]; //统计每门课程的总人数(或每个年级)总成绩 if(all[i].score[type] >= 60) cnt1++; //统计及格的人数 else cnt2++; //统计不及格的人数 } } printf("Average Score: %.2f\n", cnt1+cnt2 == 0 ? 0 : sum*1.0/(cnt1+cnt2)+EPS); printf("Number of passed students: %d\n", cnt1); printf("Number of failed students: %d\n", cnt2); printf("\n"); } void stat() { puts("Please enter class ID, 0 for the whole statistics."); int ID; cin >> ID; cout << "Chinese" << endl; output(ID, 1); cout << "Mathematics" << endl; output(ID, 2); cout << "English" << endl; output(ID, 3); cout << "Programming" << endl; output(ID, 4); cout << "Overall:" << endl; //统计每个人所通过的课程数 int okcnt[5]; memset(okcnt, 0, sizeof(okcnt)); for(int i = 0; i < cnt; i++) { if(!all[i].removed && (ID==0 || all[i].CID==ID)) { int ok = 0; for(int j = 1; j <= 4; j++) if(all[i].score[j] >= 60) ok++; okcnt[ok]++; } } printf("Number of students who passed all subjects: %d\n", okcnt[4]); printf("Number of students who passed 3 or more subjects: %d\n", okcnt[3]+okcnt[4]); printf("Number of students who passed 2 or more subjects: %d\n", okcnt[2]+okcnt[3]+okcnt[4]); printf("Number of students who passed 1 or more subjects: %d\n", okcnt[1]+okcnt[2]+okcnt[3]+okcnt[4]); printf("Number of students who failed all subjects: %d\n", okcnt[0]); printf("\n"); } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); while(true) { print_menu(); int choice; cin >> choice; if(choice == 0) break; if(choice == 1) add(); if(choice == 2) DQ(0);//删除 if(choice == 3) DQ(1);//查询 if(choice == 4) { cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."; cout<<endl; } if(choice == 5) stat(); } return 0; }
师兄帮帮忙(A Typical Homework,ACM)
最新推荐文章于 2022-02-02 23:00:35 发布