A Typical Homework(a.k.a Shi Xiong Bang Bang Mang)
Task
Write a Student Performance Management System (SPMS).Concepts
In the SPMS, there will be at most 100 students, each has an SID, a CID, a name and four scores (Chinese, Mathematics, English and Programming).- SID (student ID) is a 10-digit number
- CID (class ID) is a positive integer not greater than 20.
- Name is a string of no more than 10 letters and digits, beginning with a capital letter. Note that a name cannot contain space characters inside.
- Each score is a non-negative integer not greater than 100.
Main Menu
When you enter the SPMS, the main menu should be shown like this:Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit
Adding a Student
If you choose 1 from the main menu, the following message should be printed on the screen:Please enter the SID, CID, name and four scores. Enter 0 to finish.Then your program should wait for user input. The input lines are always valid (no invalid SID, CID, or name, exactly four scores etc), but the SID may already exist. In that case, simply ignore this line and print the following:
Duplicated SID.On the other hand, multiple students can have the same name. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Removing a Student
If you choose 2 from the main menu, the following message should be printed on the screen:Please enter SID or name. Enter 0 to finish.Then your program should wait for user input, and remove all the students matching the SID or name in the database, and print the following message (it's possible xx=0):
xx student(s) removed.You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Querying Students
If you choose 3 from the main menu, the following message should be printed on the screen:Please enter SID or name. Enter 0 to finish.Then your program should wait for user input. If no student matches the SID or name, simply do nothing, otherwise print out all the matching students, in the same order they're added to the database. The format is similar to the input format for "adding a student", but 3 more columns are added: rank (1st column), total score and average score (last two columns). The student with highest total score (considering all classes) received rank-1, and if there are two rank-2 students, the next one would be rank-4. You should keep printing the message above until the user inputs a single zero. After that the main menu is printed again.
Showing the Ranklist
If you choose 4 from the main menu, the following message should be printed on the screen:Showing the ranklist hurts students' self-esteem. Don't do that.Then the main menu is printed again.
Showing Statistics
If you choose 5 from the main menu, show the statistics, in the following format:Please enter class ID, 0 for the whole statistics.When a class ID is entered, print the following statistics. Note that "passed" means to have a score of at least 60.
Chinese Average Score: xx.xx Number of passed students: xx Number of failed students: xx Mathematics Average Score: xx.xx Number of passed students: xx Number of failed students: xx English Average Score: xx.xx Number of passed students: xx Number of failed students: xx Programming Average Score: xx.xx Number of passed students: xx Number of failed students: xx Overall: Number of students who passed all subjects: xx Number of students who passed 3 or more subjects: xx Number of students who passed 2 or more subjects: xx Number of students who passed 1 or more subjects: xx Number of students who failed all subjects: xxThen, the main menu is printed again.
Exiting SPMS
If you choose 0 from the main menu, the program should terminate. Note that course scores and total score should be formatted as integers, but average scores should be formatted as a real number with exactly two digits after the decimal point.Input
There will be a single test case, ending with a zero entered in the main menu screen. The entire input will be valid. The size of input does not exceed 10KB.
Output
Print out everything as stated in the problem description. You should be able to play around this little program in your machine, with a keyboard and a screen. However, both the input and output may look silly when they're not mixed, as in the keyboard-screen case.
Sample Input
1 0011223344 1 John 79 98 91 100 0022334455 1 Tom 59 72 60 81 0011223344 2 Alice 100 100 100 100 2423475629 2 John 60 80 30 99 0 3 0022334455 John 0 5 1 2 0011223344 0 5 0 4 0
Output for the Sample Input
Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Please enter the SID, CID, name and four scores. Enter 0 to finish. Please enter the SID, CID, name and four scores. Enter 0 to finish. Please enter the SID, CID, name and four scores. Enter 0 to finish. Duplicated SID. Please enter the SID, CID, name and four scores. Enter 0 to finish. Please enter the SID, CID, name and four scores. Enter 0 to finish. Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Please enter SID or name. Enter 0 to finish. 2 0022334455 1 Tom 59 72 60 81 272 68.00 Please enter SID or name. Enter 0 to finish. 1 0011223344 1 John 79 98 91 100 368 92.00 3 2423475629 2 John 60 80 30 99 269 67.25 Please enter SID or name. Enter 0 to finish. Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Please enter class ID, 0 for the whole statistics. Chinese Average Score: 69.00 Number of passed students: 1 Number of failed students: 1 Mathematics Average Score: 85.00 Number of passed students: 2 Number of failed students: 0 English Average Score: 75.50 Number of passed students: 2 Number of failed students: 0 Programming Average Score: 90.50 Number of passed students: 2 Number of failed students: 0 Overall: Number of students who passed all subjects: 1 Number of students who passed 3 or more subjects: 2 Number of students who passed 2 or more subjects: 2 Number of students who passed 1 or more subjects: 2 Number of students who failed all subjects: 0 Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Please enter SID or name. Enter 0 to finish. 1 student(s) removed. Please enter SID or name. Enter 0 to finish. Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Please enter class ID, 0 for the whole statistics. Chinese Average Score: 59.50 Number of passed students: 1 Number of failed students: 1 Mathematics Average Score: 76.00 Number of passed students: 2 Number of failed students: 0 English Average Score: 45.00 Number of passed students: 1 Number of failed students: 1 Programming Average Score: 90.00 Number of passed students: 2 Number of failed students: 0 Overall: Number of students who passed all subjects: 0 Number of students who passed 3 or more subjects: 2 Number of students who passed 2 or more subjects: 2 Number of students who passed 1 or more subjects: 2 Number of students who failed all subjects: 0 Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit Showing the ranklist hurts students' self-esteem. Don't do that. Welcome to Student Performance Management System (SPMS). 1 - Add 2 - Remove 3 - Query 4 - Show ranking 5 - Show Statistics 0 - Exit
Rujia Liu's Present 5: Developing Simplified Softwares
Special Thanks: Youzhi Bao, Zhuohua Chen
测试可以不知道为什么交上去WA
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define EPS 0.00001
using namespace std;
int flag[2000]={}; //标记该stu[]是否为空或者已删除
struct student
{
char SID[15];
int CID;
char name[15];
int chinese;
int math;
int english;
int program;
int sum;
double ave;
}stu[2000];
void menu() //显示菜单
{
cout<<"Welcome to Student Performance Management System (SPMS)."<<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<<endl;
}
void add() //增加学生信息
{
char temp[15];
int flag2=0,i;
while(1)
{
cout<<"Please enter the SID, CID, name and four scores. Enter 0 to finish."<<endl;
cin>>temp;
if(strcmp(temp,"0")==0)
break;
else
{
for(i=0;i<2000;i++)
{
if(flag[i]==0)
{
cin>>stu[i].CID>>stu[i].name>>stu[i].chinese>>stu[i].math>>stu[i].english>>stu[i].program;
stu[i].sum=stu[i].chinese+stu[i].math+stu[i].english+stu[i].program;
stu[i].ave=stu[i].sum/4.0+EPS;
break;
}
}
for(int j=0;j<2000;j++)
{
if(strcmp(stu[j].SID,temp)==0&&flag[j]==1)
{
flag2=1;
break;
}
}
if(flag2==1)
{
cout<<"Duplicated SID."<<endl;
flag2=0;
}
else
{
strcpy(stu[i].SID,temp);
flag[i]=1;
}
}
}
}
void rem() //删除学生信息
{
int count=0,i;
char temp[15]={};
while(1)
{
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
cin>>temp;
if(strcmp(temp,"0")==0)
break;
for(i=0;i<2000;i++)
{
if((strcmp(temp,stu[i].SID)==0||strcmp(temp,stu[i].name)==0)&&flag[i]!=0)
{
for(int j=i;j<2000;j++)
{
flag[j]=flag[j+1];
strcpy(stu[j].SID,stu[j+1].SID);
stu[j].CID=stu[j+1].CID;
strcpy(stu[j].name,stu[j+1].name);
stu[j].chinese=stu[j+1].chinese;
stu[j].math=stu[j+1].math;
stu[j].english=stu[j+1].english;
stu[j].program=stu[j+1].program;
stu[j].sum=stu[j+1].sum;
stu[j].ave=stu[j+1].ave;
}
i--; //往前移了一位所以从前一位继续遍历
count++;
}
}
cout<<count<<' '<<"student(s) removed."<<endl;
count=0;
}
}
void query() //查询学生信息
{
char temp[15]={};
while(1)
{
int rank=0;
cout<<"Please enter SID or name. Enter 0 to finish."<<endl;
cin>>temp;
if(strcmp(temp,"0")==0)
break;
else
{
for(int i=0;i<2000;i++)
{
if((strcmp(temp,stu[i].SID)==0||strcmp(temp,stu[i].name)==0)&&flag[i]!=0)
{
for(int j=0;j<2000;j++)
{
if((stu[j].sum>stu[i].sum)&&flag[j]!=0)
rank++;
}
cout<<rank+1<<' '<<stu[i].SID<<' '<<stu[i].CID<<' '<<stu[i].name<<' '<<stu[i].chinese<<' '<<stu[i].math<<' '<<stu[i].english<<' '<<stu[i].program<<' '<<stu[i].sum<<' ';
printf("%.2llf\n",stu[i].ave);
rank=0;
}
}
}
}
}
void statistics() //统计信息
{
int ID,i,allfail=0,one=0,two=0,three=0,four=0,pass[4]={},fail[4]={},sum[4]={},people=0,count1=0;
double ave=0;
cout<<"Please enter class ID, 0 for the whole statistics."<<endl;
cin>>ID;
for(i=0;i<2000;i++)
{
if((flag[i]==1&&stu[i].CID==ID)||(flag[i]==1&&ID==0))
{
sum[0]+=stu[i].chinese;
sum[1]+=stu[i].math;
sum[2]+=stu[i].english;
sum[3]+=stu[i].program;
if(stu[i].chinese>=60) {pass[0]++;count1++;}
if(stu[i].math>=60) {pass[1]++;count1++;}
if(stu[i].english>=60) {pass[2]++;count1++;}
if(stu[i].program>=60) {pass[3]++;count1++;}
if(count1==4) {four++;count1=0;}
else if(count1==3) {three++;count1=0;}
else if(count1==2) {two++;count1=0;}
else if(count1==1) {one++;count1=0;}
else if(count1==0) {allfail++;count1=0;}
people++;
}
}
cout<<"Chinese"<<endl;
if(people!=0)
ave=(sum[0]*1.0)/people+EPS;
else
ave=0+EPS;
printf("Average Score: %.2llf\n",ave);
cout<<"Number of passed students: "<<pass[0]<<endl;
cout<<"Number of failed students: "<<people-pass[0]<<endl<<endl;
cout<<"Mathematics"<<endl;
if(people!=0)
ave=(sum[1]*1.0)/people+EPS;
else
ave=0+EPS;
printf("Average Score: %.2llf\n",ave);
cout<<"Number of passed students: "<<pass[1]<<endl;
cout<<"Number of failed students: "<<people-pass[1]<<endl<<endl;
cout<<"English"<<endl;
if(people!=0)
ave=(sum[2]*1.0)/people+EPS;
else
ave=EPS;
printf("Average Score: %.2llf\n",ave);
cout<<"Number of passed students: "<<pass[2]<<endl;
cout<<"Number of failed students: "<<people-pass[2]<<endl<<endl;
cout<<"Programming"<<endl;
if(people!=0)
ave=(sum[3]*1.0)/people+EPS;
else
ave=0+EPS;
printf("Average Score: %.2llf\n",ave);
cout<<"Number of passed students: "<<pass[3]<<endl;
cout<<"Number of failed students: "<<people-pass[3]<<endl<<endl;
cout<<"Overall:"<<endl;
cout<<"Number of students who passed all subjects: "<<four<<endl;
cout<<"Number of students who passed 3 or more subjects: "<<four+three<<endl;
cout<<"Number of students who passed 2 or more subjects: "<<four+three+two<<endl;
cout<<"Number of students who passed 1 or more subjects: "<<four+three+two+one<<endl;
cout<<"Number of students who failed all subjects: "<<allfail<<endl<<endl;
}
int main()
{
//freopen("c:\\t.txt","r",stdin);
//freopen("c:\\hu.txt","w",stdout);
int choice;
while(1)
{
menu();
cin>>choice;
if(choice==0) break;
if(choice==1) add();
if(choice==2) rem();
if(choice==3) query();
if(choice==4) cout<<"Showing the ranklist hurts students' self-esteem. Don't do that."<<endl;
if(choice==5) statistics();
}
}