大一新生头发要掉光了。为什么这里面的ScoreDes和ScoreAs(按照总分的升序降序排列)单独用的时候没有问题,但是放这里就有问题?主要VC6也没有报错,这个逻辑错误真的找不出来了。

大一新生头发要掉光了。为什么这里面的ScoreDes和ScoreAs(按照总分的升序降序排列)会出问题?明明单独拿出来运行的很好啊。只用看这两个函数和switch和main就够了,其他的都没有问题。主要VC6也没有报错,这个逻辑错误真的找不出来了。

另外主要出问题的地方都在switch函数里面标注出来了,因为老师的要求,不让用结构体,所以全部用的都是数组和指针,有点麻烦但是真的只能这样。。

谢谢了QAQ

/*1. 学生成绩管理系统V3.0
某班有最多不超过30人(具体人数由键盘输入)参加期末考试,考试科目最多不超过6门(具体门数由键盘输入)。参考本章例题,用二维数组作函数参数编程实现如下学生成绩管理:
(1)    录入每个学生的学号、姓名和各科考试成绩;
(2)    计算每门课程的总分和平均分;
(3)    计算每个学生的总分和平均分;
(4)    按每个学生的总分由高到低排出名次表;
(5)    按每个学生的总分由低到高排出名次表;
(6)    按学号由小到大排出各科成绩表;
(7)    按姓名的字典顺序排出各科成绩表;
(8)    按学号查询学生排名及其各科考试成绩;
(9)    按姓名查询学生排名及其各科考试成绩;
(10)    按优秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5个类别,对每门课程分别统计每个类别的人数以及所占的百分比;
(11)    输出每个学生的学号、姓名、各科考试成绩、课程总分和平均分,以及每门课程的总分和平均分。
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 30

Swap(long *a,long *b){
    long temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

SwapStr(char *a,char *b){
    char temp[20];
    strcpy(temp,a);
    strcpy(a,b);
    strcpy(b,temp);
}
 
int ReadScore(int *pRank,long *pId,char *pName[],int *pScore,int *py){
    int x,y,i,j;
    printf("Please input the total number of students and courses:");
    scanf("%d",&x);
    getchar();
    scanf("%d",&y);
    getchar();
    pScore = (int *)calloc(x*y,sizeof(int));
    if(pScore == NULL){
        printf("No enough memory!");
        exit(1);
    }
    for(i=0;i<x;i++){
        printf("Input student's id and name:");
        scanf("%d",&pId[i]);
        getchar();
        gets(pName[i]);
        *(pRank+i) = i+1;
        printf("Record student's %d scores:",y);
        for(j=0;j<y;j++){
            scanf("%d",&pScore[i*y+j]);
            getchar();
        }
    }
    *py = y;
    return x;
}

Print(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j;
    printf("Rank        Id        Name        Sub1    Sub2    Sub3    Sub4    Sub5    Sub6\n");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    for(i=0;i<x;i++){
        printf("num %d        %ld        %s        ",pRank[i],pId[i],pName[i]);
        for(j=0;j<y;j++){
            printf("%d    ",pScore[i*y+j]);
        }
        printf("\n");
    }
}

Sum01(int *pScore,int x,int y){
    int sum[6],i,j;
    float aver[6];
    for(j=0;j<y;j++){
        sum[j]=0;
        for(i=0;i<x;i++){
            sum[j]+=pScore[j+y*i];
        }
        aver[j]=(float)sum[j]/x;
        printf("Sub%d:Sum is %d,Average is %f\n",(j+1),sum[j],aver[j]);
    }
}

Sum02(int *pScore,int x,int y,char *pName[]){
    int sum[30],i,j;
    float aver[30];
    for(j=0;j<x;j++){
        sum[j]=0;
        for(i=0;i<y;i++){
            sum[j]+=pScore[i+y*j];
        }
        aver[j]=(float)sum[j]/x;
        printf("%s:Sum is %d,Average is %f\n",pName[j],sum[j],aver[j]);
    }
}

SumSing(int *pScore,int x,int y,int j){
    int sum,i;
    float aver;
    sum = 0;
    for(i=0;i<y;i++){
        sum+=pScore[i+y*j];
    }
    aver = (float)sum/y;
    printf("%d    %f",sum,aver);
}

Sum(int *pScore,int x,int y,int sum[]){
    int i,j;
    for(j=0;j<x;j++){
        sum[j]=0;
        for(i=0;i<y;i++){
            sum[j]+=pScore[i+y*j];
        }
    }
}

ScoreDes(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int sum[N],i,j,k,max,r,a,b;
    for(a=0;a<x;a++){
        sum[a]=0;
        for(b=0;b<y;b++){
            sum[a]+=pScore[b+y*a];
        }
    }
    for(i=0;i<x-1;i++){
        max = i;
        for(j=i+1;j<x;j++){
            if(sum[max]<sum[j]){
                max = j;
            }
        }
        Swap((pId+i),(pId+max));
        for(k=0;k<y;k++){
            Swap((pScore+k+i*y),(pScore+k+max*y));
        }
        SwapStr(pName[i],pName[max]);
    }
    for(r=0;r<x;r++){
        *(pRank+r)=r+1;
    }
}

ScoreAs(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int sum[N],i,j,k,max,a,b;
    for(a=0;a<x;a++){
        sum[a]=0;
        for(b=0;b<y;b++){
            sum[a]+=pScore[b+y*a];
        }
    }
    for(i=0;i<x-1;i++){
        max = i;
        for(j=i+1;j<x;j++){
            if(sum[max]>sum[j])
                max = j;
        }
        Swap((pId+i),(pId+max));
        Swap((pRank+i),(pRank+max));
        for(k=0;k<y;k++){
            Swap((pScore+k+i*y),(pScore+k+max*y));
        }
        SwapStr(pName[i],pName[max]);
    }
}

IdAs(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j,k,max;
    for(i=0;i<x-1;i++){
        max = i;
        for(j=i+1;j<x;j++){
            if(pId[max]>pId[j]){
                max = j;
            }
        }
        Swap((pId+i),(pId+max));
        Swap((pRank+i),(pRank+max));
        for(k=0;k<y;k++){
            Swap((pScore+k+i*y),(pScore+k+max*y));
        }
        SwapStr(pName[i],pName[max]);
    }
}

NameAs(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j,k,max;
    for(i=0;i<x-1;i++){
        max = i;
        for(j=i+1;j<x;j++){
            if(strcmp(pName[max],pName[j])>0){
                max = j;
            }
        }
        Swap((pId+i),(pId+max));
        Swap((pRank+i),(pRank+max));
        for(k=0;k<y;k++){
            Swap((pScore+k+i*y),(pScore+k+max*y));
        }
        SwapStr(pName[i],pName[max]);
    }
}

LinSearchId(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j,id,flag;
    flag = 1;
    printf("Please input ID which you are searching for:");
    scanf("%d",&id);
    for(i=0;i<x;i++){
        if(id == pId[i]){
            flag--;
            printf("Rank        Id        name        Sub1    Sub2    Sub3    Sub4    Sub5    Sub6\n");
            printf("-----------------------------------------------------------------------------------\n");
            
            printf("num %d        %ld        %s        ",pRank[i],pId[i],pName[i]);
            for(j=0;j<y;j++){
                printf("%d    ",pScore[i*y+j]);
            }
            printf("\n");
        }
    }
    if(flag == 1)
        printf("Sorry,this id isn't exist!");
}

LinSearchName(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j,flag;
    char name[20];
    flag = 1;
    printf("Please input Name which you are searching for:");
    gets(name);
    for(i=0;i<x;i++){
        if(strcmp(name,pName[i])==0){
            flag--;
            printf("Rank        Id        name        Sub1    Sub2    Sub3    Sub4    Sub5    Sub6\n");
            printf("-----------------------------------------------------------------------------------\n");
            
            printf("num %d        %ld        %s        ",pRank[i],pId[i],pName[i]);
            for(j=0;j<y;j++){
                printf("%d    ",pScore[i*y+j]);
            }
            printf("\n");
        }
    }
    if(flag == 1)
        printf("Sorry,this name isn't exist!");
}

Analysis(int *pRank,long *pId,char *pName[],int *pScore,int x,int y)
{
    int i,j,k;
    int cla[5]={0,0,0,0,0};
    for(j=0;j<y;j++){
        for(i=0;i<x;i++){
            if(pScore[j+i*y]>=90){
                cla[0]++;
            }else if(pScore[j+i*y]>=80&&pScore[j+i*y]<90){
                cla[1]++;
            }else if(pScore[j+i*y]>=70&&pScore[j+i*y]<80){
                cla[2]++;
            }else if(pScore[j+i*y]>=60&&pScore[j+i*y]<70){
                cla[3]++;
            }else if(pScore[j+i*y]>=0&&pScore[j+i*y]<60){
                cla[4]++;
            }
        }
        printf("---------------------------------\n");
        printf("Sub%d:",j+1);
        printf("A:%d,%f%%\n",cla[0],(float)(cla[0]*100)/x);
        printf("B:%d,%f%%\n",cla[1],(float)(cla[1]*100)/x);
        printf("C:%d,%f%%\n",cla[2],(float)(cla[2]*100)/x);
        printf("D:%d,%f%%\n",cla[3],(float)(cla[3]*100)/x);
        printf("E:%d,%f%%\n",cla[4],(float)(cla[4]*100)/x);
        printf("\n");
        for(k=0;k<5;k++){
            cla[k]=0;
        }
    }
}

PrintPlus(int *pRank,long *pId,char *pName[],int *pScore,int x,int y){
    int i,j;
    printf("Rank        Id        Name        Sub1    Sub2    Sub3    Sub4    Sub5    Sub6    Sum    Average\n");
    printf("-----------------------------------------------------------------------------------------------------------\n");
    for(i=0;i<x;i++){
        printf("num %d        %ld        %s        ",pRank[i],pId[i],pName[i]);
        for(j=0;j<y;j++){
            printf("%d    ",pScore[i*y+j]);
        }
        SumSing(pScore,x,y,i);
        printf("\n");
    }
    Sum01(pScore,x,y);
}

Switch(int *pRank,long *pId,char *pName[],int *pScore){
    int choice,x,y,i,j,flag;
    flag = 0;
    do{
        printf("Please enter your choice:");
        scanf("%d",&choice);
        getchar();
        switch(choice){
        case 1:
            if(flag > 0){
                free(pScore);
            }
            flag++;
            printf("Please input the total number of students and courses:");
            scanf("%d",&x);
            getchar();
            scanf("%d",&y);
            getchar();
            pScore = (int *)calloc(x*y,sizeof(int));
            if(pScore == NULL){
                printf("No enough memory!");
                exit(1);
            }
            for(i=0;i<x;i++){
                printf("Input student's id and name:");
                scanf("%d",&pId[i]);
                getchar();
                gets(pName[i]);
                printf("Record student's %d scores:",y);
                for(j=0;j<y;j++){
                    scanf("%d",&pScore[i*y+j]);
                    getchar();
                }
            }
            ScoreDes(pRank,pId,pName,pScore,x,y);              //在输入时进行排序,并表明排名(但是会出错,就这里不知道为啥)
            break;
        case 2:
            Sum01(pScore,x,y);
            break;
        case 3:
            Sum02(pScore,x,y,pName);
            break;
        case 4:
            ScoreDes(pRank,pId,pName,pScore,x,y);
            Print(pRank,pId,pName,pScore,x,y);                //后面的case4和case5也会跟着出现问题
            break;
        case 5:
            ScoreAs(pRank,pId,pName,pScore,x,y);
            Print(pRank,pId,pName,pScore,x,y);
            break;
        case 6:
            IdAs(pRank,pId,pName,pScore,x,y);
            Print(pRank,pId,pName,pScore,x,y);
            break;
        case 7:
            NameAs(pRank,pId,pName,pScore,x,y);
            Print(pRank,pId,pName,pScore,x,y);
            break;
        case 8:
            LinSearchId(pRank,pId,pName,pScore,x,y);
            break;
        case 9:
            LinSearchName(pRank,pId,pName,pScore,x,y);
            break;
        case 10:
            Analysis(pRank,pId,pName,pScore,x,y);
            break;
        case 11:
            ScoreDes(pRank,pId,pName,pScore,x,y);
            PrintPlus(pRank,pId,pName,pScore,x,y);
            break;
        case 0:
            break;
        default:
            printf("Invalid operator!!!\n");
            while(getchar() != '\n');
            break;
        }
        printf("\n");
        printf("-----------------------------------------------------------------------------\n");
        printf("-----------------------------------------------------------------------------\n");
    }while(choice != 0);
    free(pScore);
}

main(){
    char name[N][20];
    int rank[N],i;
    long id[N];
    long *pId;
    char *pName[N];
    int *pScore,*pRank;
    pId = id;
    pRank = rank;
    pScore = NULL;
    for(i=0;i<N;i++){
        pName[i]=name[i];
    }
    printf("\
      1.    Input record\n\
      2.    Calculate total and average score of every course\n\
      3.    Calculate total and average score of every student\n\
      4.    Sort in descending order by total score of every student\n\
      5.    Sort in ascending order by total score of every student\n\
      6.    Sort in ascending order by number\n\
      7.    Sort in dictionary order by name\n\
      8.    Search by number\n\
      9.    Search by name\n\
      10.    Statistic analysis for every course\n\
      11.    List record\n\
      0.    Exit\n");
    Switch(pRank,pId,pName,pScore);
    printf("Thanks for using this System!");
}