uDebug的数据和Luogu样例都已经过了
#include<bits/stdc++.h>
using namespace std;
struct Student{
string name,sid,cid;
int chinese,math,english,program,sum,rank;
bool cz;
}student[205];
int st;
bool cmp(Student x,Student y){
if(x.cz==0&&y.cz==0) return x.sum>y.sum;
else return x.cz<y.cz;
}
bool cmp2(Student x,Student y){
if(x.cz==0&&y.cz==0) return x.sid<y.sid;
else return x.cz<y.cz;
}
void SPMS_AddStudent(){
sort(student,student+st,cmp);
student[st].cz=0;
while(true){
printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
cin>>student[st].sid;
if(student[st].sid=="0") return;
cin>>student[st].cid>>student[st].name>>student[st].chinese>>student[st].math>>student[st].english>>student[st].program;
student[st].sum=student[st].chinese+student[st].math+student[st].english+student[st].program;
bool f=1;
for(int i=0;i<st;i++){
if(student[st].sid==student[i].sid&&student[st].cz==0){
printf("Duplicated SID.\n");
f=0;
break;
}
}
student[st].cz=0;
if(f) st++;
student[st].cz=0;
}
}
void SPMS_DelStudent(){
while(true){
printf("Please enter SID or name. Enter 0 to finish.\n");
string ns;
cin>>ns;
if(ns=="0") return;
int deld=0;
for(int i=0;i<st;i++){
if((ns==student[i].sid||ns==student[i].name)&&student[i].cz==0){
deld++;
student[i].cz=1;
}
}
printf("%d student(s) removed.\n",deld);
sort(student,student+st,cmp);
st-=deld;
}
}
void SPMS_Search(){
sort(student,student+st,cmp);
int score=student[0].sum,pm=0;
for(int i=0;i<st;i++){
if(score!=student[i].sum){
score=student[i].sum;
pm=i;
}
student[i].rank=pm+1;
}
sort(student,student+st,cmp2);
while(true){
printf("Please enter SID or name. Enter 0 to finish.\n");
string ns;
cin>>ns;
if(ns=="0") return;
for(int i=0;i<st;i++){
if((ns==student[i].sid||ns==student[i].name)&&student[i].cz==0){
cout<<student[i].rank<<" "<<student[i].sid<<" "<<student[i].cid<<" "<<student[i].name<<" ";
printf("%d %d %d %d %d %.2lf\n",student[i].chinese,student[i].math,student[i].english,student[i].program,student[i].sum,((double)student[i].sum)/4);
}
}
}
}
void SPMS_Ranking(){
printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
return ;
}
void SPMS_Classes(){
printf("Please enter class ID, 0 for the whole statistics.\n");
string ns;
cin>>ns;
double Chinese_pjf=0,Math_pjf=0,English_pjf=0,Program_pjf=0;
int Chinese_psd=0,Chinese_fld=0;
int Math_psd=0,Math_fld=0;
int English_psd=0,English_fld=0;
int Program_psd=0,Program_fld=0;
int Psd[5],studenttot=0;
memset(Psd,0,sizeof(Psd));
if(ns=="0"){
for(int i=0;i<st;i++){
studenttot++;
int apsd=0;
Chinese_pjf+=student[i].chinese;
if(student[i].chinese>=60){
Chinese_psd++;
apsd++;
}
else Chinese_fld++;
Math_pjf+=student[i].math;
if(student[i].math>=60) {
Math_psd++;
apsd++;
}
else Math_fld++;
English_pjf+=student[i].english;
if(student[i].english>=60) {
English_psd++;
apsd++;
}
else English_fld++;
Program_pjf+=student[i].program;
if(student[i].program>=60) {
Program_psd++;
apsd++;
}
else Program_fld++;
Psd[apsd]++;
}
}
else{
for(int i=0;i<st;i++){
if(student[i].cid==ns&&student[i].cz==0){
studenttot++;
int apsd=0;
Chinese_pjf+=student[i].chinese;
if(student[i].chinese>=60){
Chinese_psd++;
apsd++;
}
else Chinese_fld++;
Math_pjf+=student[i].math;
if(student[i].math>=60) {
Math_psd++;
apsd++;
}
else Math_fld++;
English_pjf+=student[i].english;
if(student[i].english>=60) {
English_psd++;
apsd++;
}
else English_fld++;
Program_pjf+=student[i].program;
if(student[i].program>=60) {
Program_psd++;
apsd++;
}
else Program_fld++;
Psd[apsd]++;
}
}
}
printf("Chinese\n");
if(studenttot!=0) printf("Average Score: %.2lf\n",Chinese_pjf/studenttot);
else printf("Average Score: -nan\n");
printf("Number of passed students: %d\n",Chinese_psd);
printf("Number of failed students: %d\n",Chinese_fld);
printf("\n");
printf("Mathematics\n");
if(studenttot!=0) printf("Average Score: %.2lf\n",Math_pjf/studenttot);
else printf("Average Score: -nan\n");
printf("Number of passed students: %d\n",Math_psd);
printf("Number of failed students: %d\n",Math_fld);
printf("\n");
printf("English\n");
if(studenttot!=0) printf("Average Score: %.2lf\n",English_pjf/studenttot);
else printf("Average Score: -nan\n");
printf("Number of passed students: %d\n",English_psd);
printf("Number of failed students: %d\n",English_fld);
printf("\n");
printf("Programming\n");
if(studenttot!=0) printf("Average Score: %.2lf\n",Program_pjf/studenttot);
else printf("Average Score: -nan\n");
printf("Number of passed students: %d\n",Program_psd);
printf("Number of failed students: %d\n",Program_fld);
printf("\n");
printf("Overall:\n");
printf("Number of students who passed all subjects: %d\n",Psd[4]);
printf("Number of students who passed 3 or more subjects: %d\n",Psd[3]+Psd[4]);
printf("Number of students who passed 2 or more subjects: %d\n",Psd[2]+Psd[3]+Psd[4]);
printf("Number of students who passed 1 or more subjects: %d\n",Psd[1]+Psd[2]+Psd[3]+Psd[4]);
printf("Number of students who failed all subjects: %d\n",Psd[0]);
printf("\n");
}
void SPMS_main(){
while(true){
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");
printf("\n");
int willdo;
scanf("%d",&willdo);
if(willdo==0) return;
else if(willdo==1) SPMS_AddStudent();
else if(willdo==2) SPMS_DelStudent();
else if(willdo==3) SPMS_Search();
else if(willdo==4) SPMS_Ranking();
else if(willdo==5) SPMS_Classes();
}
return ;
}
int main(){
SPMS_main();
return 0;
}