代码如下:
#include<bits/stdc++.h>
using namespace std;
struct node{
int CID,yu,shu,ying,bian,rank,paiming,score;
double pingjun;
char name[301],SID[501];
}a[201];
int n;
void print(){
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 ru(){
while(1){
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
cin>>a[++n].SID;
if(a[n].SID[0]=='0'&&strlen(a[n].SID)==1){
n--;
break;
}
a[n].rank=n;
cin>>a[n].CID>>a[n].name>>a[n].yu>>a[n].shu>>a[n].ying>>a[n].bian;a[n].score=a[n].yu+a[n].shu+a[n].ying+a[n].bian;a[n].pingjun=a[n].score/4.0+1e-5;
for(int i=1;i<n;i++)
if(strcmp(a[i].SID,a[n].SID)==0){
printf("Duplicated SID.\n");
n--;
break;
}
}
}
void shan(int x){
while(1){
int i,ans=0;
char c[301];
memset(c,'\0',sizeof(c));
printf("Please enter SID or name. Enter 0 to finish.\n");
cin>>c;
if(c[0]=='0'&&strlen(c)==1)
break;
for(i=1;i<=n;i++)
if(strcmp(a[i].SID,c)==0||strcmp(a[i].name,c)==0){
if(x==1){
for(int j=i;j<n;j++)
a[j]=a[j+1];
n--;
ans++;
}
else{
printf("%d %s %d %s %d %d %d %d %d %.2lf\n",a[i].paiming,a[i].SID,a[i].CID,a[i].name,a[i].yu,a[i].shu,a[i].ying,a[i].bian,a[i].score,a[i].pingjun);
}
}
if(x)
printf("%d student(s) removed.\n",ans);
}
}
void show(){
int id,bu1,bu2,bu3,bu4,guo1,guo2,guo3,guo4,total,total1,total2,total3,total4,total5;
double sum1,sum2,sum3,sum4;
bu1=bu2=bu3=bu4=guo1=guo2=guo3=guo4=sum1=sum2=sum3=sum4=total=total1=total2=total3=total4=total5=0;
printf("Please enter class ID, 0 for the whole statistics.\n");
cin>>id;
if(id!=0)
for(int i=1;i<=n;i++)
if(a[i].CID==id){
if(a[i].yu<60)bu1++;
else guo1++;
if(a[i].shu<60)bu2++;
else guo2++;
if(a[i].ying<60)bu3++;
else guo3++;
if(a[i].bian<60)bu4++;
else guo4++;
if(a[i].yu>=60&&a[i].shu>=60&&a[i].ying>=60&&a[i].bian>=60)
total1++;
if(a[i].yu>=60&&a[i].shu>=60&&a[i].ying>=60||a[i].yu>=60&&a[i].shu>=60&&a[i].bian>=60||a[i].yu>=60&&a[i].ying>=60&&a[i].bian>=60||a[i].shu>=60&&a[i].ying>=60&&a[i].bian>=60)
total2++;
if(a[i].yu>=60&&a[i].shu>=60||a[i].yu>=60&&a[i].ying>=60||a[i].yu>=60&&a[i].bian>=60||a[i].shu>=60&&a[i].ying>=60||a[i].shu>=60&&a[i].bian>=60||a[i].ying>=60&&a[i].bian>=60)
total3++;
if(a[i].yu>=60||a[i].shu>=60||a[i].ying>=60||a[i].bian>=60)
total4++;
if(a[i].yu<60&&a[i].shu<60&&a[i].ying<60&&a[i].bian<60)
total5++;
sum1+=a[i].yu;
sum2+=a[i].shu;
sum3+=a[i].ying;
sum4+=a[i].bian;
total++;
}
if(id==0)
for(int i=1;i<=n;i++){
if(a[i].yu<60)bu1++;
else guo1++;
if(a[i].shu<60)bu2++;
else guo2++;
if(a[i].ying<60)bu3++;
else guo3++;
if(a[i].bian<60)bu4++;
else guo4++;
if(a[i].yu>=60&&a[i].shu>=60&&a[i].ying>=60&&a[i].bian>=60)
total1++;
if(a[i].yu>=60&&a[i].shu>=60&&a[i].ying>=60||a[i].yu>=60&&a[i].shu>=60&&a[i].bian>=60||a[i].yu>=60&&a[i].ying>=60&&a[i].bian>=60||a[i].shu>=60&&a[i].ying>=60&&a[i].bian>=60)
total2++;
if(a[i].yu>=60&&a[i].shu>=60||a[i].yu>=60&&a[i].ying>=60||a[i].yu>=60&&a[i].bian>=60||a[i].shu>=60&&a[i].ying>=60||a[i].shu>=60&&a[i].bian>=60||a[i].ying>=60&&a[i].bian>=60)
total3++;
if(a[i].yu>=60||a[i].shu>=60||a[i].ying>=60||a[i].bian>=60)
total4++;
if(a[i].yu<60&&a[i].shu<60&&a[i].ying<60&&a[i].bian<60)
total5++;
sum1+=a[i].yu;
sum2+=a[i].shu;
sum3+=a[i].ying;
sum4+=a[i].bian;
total++;
}
printf("Chinese\n");
printf("Average Score: %.2lf\n",sum1/total+1e-5);
printf("Number of passed students: %d\n",guo1);
printf("Number of failed students: %d\n\n",bu1);
printf("Mathematics\n");
printf("Average Score: %.2lf\n",sum2/total+1e-5);
printf("Number of passed students: %d\n",guo2);
printf("Number of failed students: %d\n\n",bu2);
printf("English\n");
printf("Average Score: %.2lf\n",sum3/total+1e-5);
printf("Number of passed students: %d\n",guo3);
printf("Number of failed students: %d\n\n",bu3);
printf("Programming\n");
printf("Average Score: %.2lf\n",sum4/total+1e-5);
printf("Number of passed students: %d\n",guo4);
printf("Number of failed students: %d\n\n",bu4);
printf("Overall:\n");
printf("Number of students who passed all subjects: %d\n",total1);
printf("Number of students who passed 3 or more subjects: %d\n",total2);
printf("Number of students who passed 2 or more subjects: %d\n",total3);
printf("Number of students who passed 1 or more subjects: %d\n",total4);
printf("Number of students who failed all subjects: %d\n\n",total5);
}
void mingci(){
a[1].paiming=1;
for(int i=2;i<=n;i++){
if(a[i].score==a[i-1].score)
a[i].paiming=a[i-1].paiming;
else
a[i].paiming=i;
}
}
int cmp1(node x,node y){
return x.score>y.score;
}
int cmp2(node x,node y){
return x.rank<y.rank;
}
int main(){
while(1){
int xuanxiang;
print();
scanf("%d",&xuanxiang);
if(xuanxiang==0)break;
if(xuanxiang==1){
ru();
sort(a+1,a+n+1,cmp1);
mingci();
sort(a+1,a+n+1,cmp2);
}
if(xuanxiang==2)shan(1);
if(xuanxiang==3)shan(0);
if(xuanxiang==4)printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
if(xuanxiang==5)show();
}
return 0;
}
求大佬指出错误!!!!