UVA12412师兄帮帮忙

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sgsyacm/article/details/79309671
#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的原因,超级有帮助,附上大神链接。点击打开链接
展开阅读全文

uva12412,找不到哪里出错了

08-06

不执行删除操作都没有问题,一执行删除操作就会导致添加信息模块无法判断是否已有当前输入的学号,不知道是哪里出了问题,有没有大神帮忙看一下,另外这个是UVA12412的题目。rn[code=c]#includern#includern#includernusing namespace std;rn#define maxn 1000rnint n=0;rnint cid,Chinese,Mathematics,English,Programming;rnchar sid[15],name[15],s[15];rnconst char* course_name[] = "Chinese", "Mathematics", "English", "Programming";rnstruct studentrnrn int cid,del;rn int score[5];//Chinese,Mathematics,English,Programming,total按顺序rn double average;rn char name[10],sid[10];rnstu[maxn];rnvoid Print_Menu()rnrn printf("Welcome to Student Performance Management System (SPMS).\n\n");rn printf("1 - Add\n");rn printf("2 - Remove\n");rn printf("3 - Query\n");rn printf("4 - Show ranking\n");rn printf("5 - Show Statistics\n");rn printf("0 - Exit\n\n");rnrnint vaild()//判断是否有相同学号已经存在rnrn for(int i=0;istu[k].score[4])rn rn cnt++;rn rn return cnt+1;rnrnvoid Add()//添加信息rnrn for(;;)rn rn printf("Please enter the SID, CID, name and four scores. Enter 0 to finish.\n");rn scanf("%s",sid);rn if(strcmp(sid,"0")==0) break;rn scanf("%d%s%d%d%d%d",&cid,name,&Chinese,&Mathematics,&English,&Programming);rn if(!vaild())rn printf("Duplicated SID.\n");rn elsern rn strcpy(stu[n].sid,sid);rn stu[n].cid=cid;rn strcpy(stu[n].name,name);rn stu[n].score[0]=Chinese;rn stu[n].score[1]=Mathematics;rn stu[n].score[2]=English;rn stu[n].score[3]=Programming;rn stu[n].score[4]=stu[n].score[0]+stu[n].score[1]+stu[n].score[2]+stu[n].score[3];rn stu[n].average=stu[n].score[4]*1.0/4;rn stu[n].del=0;rn n++;rn rn rnrnvoid Remove()//删除信息rnrn for(;;)rn rn printf("Please enter SID or name. Enter 0 to finish.\n");rn scanf("%s",s);rn if(strcmp(s,"0")==0) break;rn int cnt=0;rn for(int i=0;i=60)rn passed++;rn elsern failed++;rn rn rn printf("%s\n",course_name[i]);rn printf("Average Score: %.2f\n",total*1.0/cnt);rn printf("Number of passed students: %d\n",passed);rn printf("Number of failed students: %d\n\n",failed);rn rn for(i=0;i=60)rn cnt1++;rn rn for(int k=1;k<=cnt1;k++)rn p[k]++;rn if(cnt1==0) p[0]++;rn rn rn printf("Overall:\n");rn printf("Number of students who passed all subjects: %d\n",p[4]);rn printf("Number of students who passed 3 or more subjects: %d\n",p[3]);rn printf("Number of students who passed 2 or more subjects: %d\n",p[2]);rn printf("Number of students who passed 1 or more subjects: %d\n",p[1]);rn printf("Number of students who failed all subjects: %d\n\n",p[0]);rn rn else//输入班级号码,显示该班级的总体情况rn rn for(i=0;i<4;i++)rn rn passed=0,failed=0,total=0,cnt=0;rn for(j=0;j=60)rn passed++;rn elsern failed++;rn rn rn printf("%s\n",course_name[i]);rn printf("Average Score: %.2f\n",total*1.0/cnt);rn printf("Number of passed students: %d\n",passed);rn printf("Number of failed students: %d\n\n",failed);rn rn for(i=0;i=60)rn cnt1++;rn rn for(int k=1;k<=cnt1;k++)rn p[k]++;rn if(cnt1==0) p[0]++;rn rn rn printf("Overall:\n");rn printf("Number of students who passed all subjects: %d\n",p[4]);rn printf("Number of students who passed 3 or more subjects: %d\n",p[3]);rn printf("Number of students who passed 2 or more subjects: %d\n",p[2]);rn printf("Number of students who passed 1 or more subjects: %d\n",p[1]);rn printf("Number of students who failed all subjects: %d\n\n",p[0]);rn rnrnint main()rnrn //rn //freopen("input.txt","r",stdin);rn //freopen("output.txt","w",stdout);rn //rn for(;;)rn rn Print_Menu();//打印主菜单rn int choice;rn scanf("%d",&choice);rn if(choice==0) break;rn if(choice==1) Add();//添加信息,然后排名rn if(choice==2) Remove();//删除信息,然后排名rn if(choice==3) Query();//查询信息rn if(choice==4) printf("Showing the ranklist hurts students' self-esteem. Don't do that.\n");rn if(choice==5) Statistics();//显示整体信息rn rn return 0;rn[/code] 论坛

师兄们一定要帮帮忙,感激不尽!!!!!

04-25

1.在程序中我令一个变量,计算机会给其赋予一个地址,这个地址是随机的么,计算机给出这个地址的时候有什么依据没?rn2.计算机会挨着分配内存空间的吧,像我前面令了一个变量,给了一个地址假如是-50,后面我又令了一个变量这个时候它应该将-48给这个变量的吧?rn3,怎么算开辟内存空间和释放内存空间,开辟的含义我基本上懂,但是释放时什么意思,即如果我开辟了一个内存空间其地址范围是从-50到-10我怎么将这个地址释放出来,释放出来的意思就是说这个内存空间又可以使用了么,既然都是地址为什么还要释放不直接使用,直接在里面存入你要存入的数据不就可以了么? (可不可以直接将你要赋的地址找出来赋值,如-50给这个地址赋一个长度为4的float类型的变量,有没有这种语句?)rn我这里有以前师兄们给我得解释,我还是有些不懂,代码如下:rn#include rn#include rn#include rnrn#define NULL 0 rn#define ALLOCSIZE 1000 rnchar allocbuf[ALLOCSIZE]; rnchar * allocp=allocbuf; rnchar * alloc(int n) rn rnif(allocp+n <=allocbuf+ALLOCSIZE) rnallocp+=n; rnreturn(allocp-n); rn rnelse rn return(NULL); rn rnfree(char *p) rn rnif(p>=allocbuf&&p <=allocbuf+ALLOCSIZE) rn allocp=p; rn rn/***************************************************************************************************/rn/* 测试用例*/ rnint main(void) rn rn int *p =NULL; rn p = (int *)alloc(8); rn *p = 1; rn *(p+1) = 2; rn printf("%d,%d\n",*p,*(p+1)); //输出1,2 rn printf("%d\n",*(int *)(allocp-4)); // 输出2 //依照我的理解这里的allocp-4应该是一个没有实际数据的地址变量吧,前面不是将1和2都分别赋给了指针为allocp和allocp+1这两地址了么此时的allocp也是指向的allocbuf[]数组的开头的啊,再将这个地址减去4得到的地址里面会有值么?怎么输出2的?rnrn free((char *)(p+1)); rn printf("%d\n",*(int *)(allocp-4)); rn return 0; rn rn //然后就是这个,执行了free((char *)(p+1))语句就释放了空间了么,现在释放了哪些范围内的空间,这些范围内的地址在上述程序中就没用使用了么?据我对free函数的理解也最多是:传入的地址如果是在刚才声明的那个数组所占用的地址范围内的话就将p赋值给allocp指针,这说明什么,只能说明将本来指向前面那个数组开头的地址的指针现在该为了指向p+1这个地址了嘛,这样怎么就释放空间了?所以我对释放空间有点迷糊,而且释放空间是不是我前面说的那样释放了的某个空间即地址,程序就没有使用这段指针了,如果是这样,请师兄帮我理解下,这个怎么做到释放的,它释放了那段空间。星号前面的是书上东西应该没有错,后面的是一个师兄给的举例,不知道有没有错,请帮我分析下。rn请各位师兄一定要帮帮忙,感激不尽!!!!!!!!!!rnrn rnrn 论坛

没有更多推荐了,返回首页