欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA12412师兄帮帮忙

程序员文章站 2024-03-20 09:58:28
...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EPS 1e-5
int N;
struct student
{
    int glass;
    char s[120],name[120];
    int Chinese,Math,English,Program,Sum;
} stu[110];

int rank(int k)
{
    int i,n = 1;
    for(i=0; i<N; i++)
    {
        if(stu[i].Sum>stu[k].Sum)
            n++;
    }
    return n;
}
void print_menu()
{
    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 Add()
{
    int k;
    getchar();
    for(;;)
    {
        int sign = 1;;
        char str[120];
        printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");
        scanf("%s",str);
        if(!strcmp(str,"0"))
            break;
        strcpy(stu[N].s,str);
        scanf("%d%s%d%d%d%d",&stu[N].glass,stu[N].name,&stu[N].Chinese,&stu[N].Math,&stu[N].English,&stu[N].Program);
        stu[N].Sum = stu[N].Chinese+stu[N].Math+stu[N].English+stu[N].Program;
        for(k=0; k<N; k++)
        {
            if(!strcmp(stu[N].s,stu[k].s))
            {
                sign=0;
                break;
            }
        }
        if(sign)
            N++;
        else
            printf("Duplicated SID.\n");
    }
}
void Remove()
{
    getchar();
    for(;;)
    {
        int i,j,num = 0;
        printf("Please enter SID or name. Enter 0 to finish.\n");
        char str[120];
        scanf("%s",str);
        if(strlen(str) == 1&& str[0] == '0')
            break;
        for(i=0; i<N; i++)
        {
            if(!strcmp(str,stu[i].name)||!strcmp(str,stu[i].s))
            {
                num++;
                N--;
                for(j=i; j<N; j++)
                    memcpy(stu+j,stu+j+1,sizeof(struct student));
                i--;
            }
        }
        printf("%d student(s) removed.\n", num);
    }
}
void Query()
{
    getchar();
    int i;
    for(;;)
    {
        printf("Please enter SID or name. Enter 0 to finish.\n");
        char str[120];
        scanf("%s",str);
        if(!strcmp(str,"0"))
            break;
        for(i=0; i<N; i++)
        {
            if(!strcmp(str,stu[i].name)||!strcmp(str,stu[i].s))
            {
                printf("%d %s %d %s %d %d %d %d %d %.2f\n", rank(i), stu[i].s, stu[i].glass,
                       stu[i].name, stu[i].Chinese, stu[i].Math,
                       stu[i].English, stu[i].Program, stu[i].Sum, stu[i].Sum / 4.0 + EPS);;
            }
        }
    }
}
void Sta()
{
    printf("Please enter class ID, 0 for the whole statistics.\n");
    int id,i,ca=0,cp=0,ma=0,mp=0,ea=0,ep=0,pa=0,pp=0,ap=0,thp=0,twp=0,op=0,af=0;
    scanf("%d",&id);
    int num1=0;
    for(i=0; i<N; i++)
    {
        int num = 0;
        if(id!=0&&stu[i].glass!=id)
            continue;
        if(stu[i].Chinese>=60)
        {
            cp++;
            num++;
        }
        if(stu[i].Math>=60)
        {
            mp++;
            num++;
        }
        if(stu[i].English>=60)
        {
            ep++;
            num++;
        }
        if(stu[i].Program>=60)
        {
            pp++;
            num++;
        }
        ca+=stu[i].Chinese;
        ma+=stu[i].Math;
        ea+=stu[i].English;
        pa+=stu[i].Program;
        num1++;
        if(num==4)
            ap++;
        else if(num==3)
            thp++;
        else if(num==2)
            twp++;
        else if(num==1)
            op++;
        else
            af++;
    }
    printf("Chinese\n");
    printf("Average Score: %.2f\n",ca*1.0/num1+EPS);
    printf("Number of passed students: %d\n",cp);
    printf("Number of failed students: %d\n\n",num1-cp);
    printf("Mathematics\n");
    printf("Average Score: %.2f\n",ma*1.0/num1+EPS);
    printf("Number of passed students: %d\n",mp);
    printf("Number of failed students: %d\n\n",num1-mp);
    printf("English\n");
    printf("Average Score: %.2f\n",ea*1.0/num1+EPS);
    printf("Number of passed students: %d\n",ep);
    printf("Number of failed students: %d\n\n",num1-ep);
    printf("Programming\n");
    printf("Average Score: %.2f\n",pa*1.0/num1+EPS);
    printf("Number of passed students: %d\n",pp);
    printf("Number of failed students: %d\n\n",num1-pp);
    printf("Overall:\n");
    printf("Number of students who passed all subjects: %d\n",ap);
    printf("Number of students who passed 3 or more subjects: %d\n",ap+thp);
    printf("Number of students who passed 2 or more subjects: %d\n",ap+thp+twp);
    printf("Number of students who passed 1 or more subjects: %d\n",ap+thp+twp+op);
    printf("Number of students who failed all subjects: %d\n\n", af);
}
int main()
{
    int order;
    N = 0;
    for(;;)
    {
        print_menu();
        scanf("%d",&order);
        if(order==0)
            break;
        else if(order==1)
            Add();
        else if(order==2)
            Remove();
        else if(order==3)
            Query();
        else if(order==5)
            Sta();
        else
            printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");
    }
    return 0;
}
这道题是紫书上最后一道例题,交上以后各种wa,也是超级恶心,做了整整一天,做到人生绝望。后来从网上找了答案一点一点的比对,希望找到出错的地方,但是没有找到。不过,遇到一位好心的大神,帮忙看了代码,找到了错误,大神的博客总结很到位,总结了这道题各种可能wa的原因,超级有帮助,附上大神链接。点击打开链接