首页 > 代码库 > UVa 12412

UVa 12412

背景:1_wa:单词拼写错误。(一定要好生对比,样例)2——wa:当database中数据消失的时候,出现了除0的情况(一旦有除法就要考虑是否有除0的现象出现) .....

继续wa..

学习:1.加了一个比保留位数小的eps=1e-5来消除浮点误差。

2.编译选项里定义了LOCAL.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct{
    char sid[11],name[21];
    int _class,math,chinese,english,programming,aggregate;
    double average;
}student;
int listn=0;
double eps=1e-5;
student list[100];

void Add(void){
l2: bool _ok=false;
    printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
    scanf("%s",list[listn].sid);
    if(list[listn].sid[0] == '0' && list[listn].sid[1] == '\0') return;
    for(int i=0;i < listn;i++)    //判断是否已经存在该学生号。
        if(strcmp(list[i].sid,list[listn].sid) == 0){
            printf("Duplicated SID.\n");
            _ok=true;
        }
    scanf("%d%s%d%d%d%d",&list[listn]._class,list[listn].name,&list[listn].chinese,&list[listn].math,&list[listn].english,&list[listn].programming);
    list[listn].aggregate=list[listn].chinese+list[listn].math+list[listn].english+list[listn].programming;
    list[listn].average=(double)(list[listn].aggregate)/4.0+eps;
    if(!_ok) listn++;
    goto l2;
}

void Remove(void){
l1: printf("Please enter SID or name. Enter 0 to finish.\n");
    char temp[21];
    scanf("%s",temp);
    if(temp[0] == '0' && temp[1] == '\0')  return;
    int count=0;
    for(int i=0;i < listn;i++){
        if(strcmp(list[i].sid,temp) == 0 || strcmp(list[i].name,temp) == 0){
            count++;
            for(int j=i;j < listn-1;j++){
                strcpy(list[j].sid,list[j+1].sid);
                strcpy(list[j].name,list[j+1].name);
                list[j]._class=list[j+1]._class;
                list[j].chinese=list[j+1].chinese;
                list[j].math=list[j+1].math;
                list[j].english=list[j+1].english;
                list[j].programming=list[j+1].programming;
            }
            listn--;
            i--;
        }
    }
    printf("%d student(s) removed.\n",count);
    goto l1;
}

void Qurey(void){
l3: printf("Please enter SID or name. Enter 0 to finish.\n");
    char temp[21];
    scanf("%s",temp);
    if(temp[0] == '0'  && temp[1] == '\0') return;
    for(int i=0;i < listn;i++){
        if(strcmp(list[i].name,temp) == 0 || strcmp(list[i].sid,temp) == 0){
            int rank=1;
            for(int j=0;j < listn;j++){
                if(list[j].aggregate > list[i].aggregate) rank++;
            }
            printf("%d %s %d %s %d %d %d %d %d %.2lf\n",rank,list[i].sid,list[i]._class,list[i].name,list[i].chinese,list[i].math,list[i].english,list[i].programming,list[i].aggregate,list[i].average);
        }
    }
    goto l3;
}

void show_statistics(void){
    printf("Please enter class ID, 0 for the whole statistics.\n");
    int classnumber;
    scanf("%d",&classnumber);
    double as=0;
    int pn=0,fn=0;
    for(int i=0;i < listn;i++){
        if(list[i]._class == classnumber || !classnumber){
            if(list[i].chinese >= 60) pn++;
            else fn++;
            as+=list[i].chinese;
        }
    }
    if(fn+pn) as/=(pn+fn);
    printf("Chinese\nAverage Score: %.2lf\nNumber of passed students: %d\nNumber of failed students: %d\n\n",as+eps,pn,fn);
    as=0;
    pn=fn=0;
    for(int i=0;i < listn;i++){
        if(list[i]._class == classnumber || !classnumber){
            if(list[i].math >= 60) pn++;
            else fn++;
            as+=list[i].math;
        }
    }
    if(fn+pn) as/=(pn+fn);
    printf("Mathematics\nAverage Score: %.2lf\nNumber of passed students: %d\nNumber of failed students: %d\n\n",as+eps,pn,fn);
    as=0;
    pn=fn=0;
    for(int i=0;i < listn;i++){
        if(list[i]._class == classnumber || !classnumber){
            if(list[i].english >= 60) pn++;
            else fn++;
            as+=list[i].english;
        }
    }
    if(fn+pn) as/=(pn+fn);
    printf("English\nAverage Score: %.2lf\nNumber of passed students: %d\nNumber of failed students: %d\n\n",as+eps,pn,fn);
    as=0;
    pn=fn=0;
    for(int i=0;i < listn;i++){
        if(list[i]._class == classnumber || !classnumber){
            if(list[i].programming >= 60) pn++;
            else fn++;
            as+=list[i].programming;
        }
    }
    if(pn+fn) as/=(pn+fn);
    printf("Programming\nAverage Score: %.2lf\nNumber of passed students: %d\nNumber of failed students: %d\n\n",as+eps,pn,fn);
    int ans[5]={0,0,0,0,0};
    for(int i=0;i < listn;i++){
        if(list[i]._class == classnumber || !classnumber){
            int num=0;
            if(list[i].chinese >= 60) num++;
            if(list[i].math >= 60) num++;
            if(list[i].english >= 60) num++;
            if(list[i].programming >= 60) num++;
            ans[num]++;
            if(num) for(int k=num-1;k > 0;k--) ans[k]++;
        }
    }
    printf("Overall:\nNumber of students who passed all subjects: %d\nNumber of students who passed 3 or more subjects: %d\nNumber of students who passed 2 or more subjects: %d\nNumber of students who passed 1 or more subjects: %d\nNumber of students who failed all subjects: %d\n\n",ans[4],ans[3],ans[2],ans[1],ans[0]);
}

int main(void){
#ifdef LOCALfg
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
#endif
    while(1){
        printf("Welcome to Student Performance Management System (SPMS).\n\n1 - Add\n2 - Remove\n3 - Query\n4 - Show ranking\n5 - Show Statistics\n0 - Exit\n\n");
        int n;
        scanf("%d",&n);
        if(n == 1) Add();
        else if(n == 2) Remove();
        else if(n == 3) Qurey();
        else if(n == 4) printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
        else if(n == 5) show_statistics();
        else break;
    }
    return 0;
}


UVa 12412