【求解】[常见结构体问题] 输入学生信息和学生分数后进行排序/查找

输入学生信息和学生分数后进行排序/查找的问题

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

struct data
{
    long int ID;
    float score;
};
int main()
{
    struct data student[30];

    struct data trmp;
    int max,i=0,n,choice;
    float sum,aver;
    long int found;

    printf("Input student number(n<30):\n");
    scanf("%d",&n);

begin:
    printf("Management for Students' scores\n");
    printf("1.Input record\n");
    printf("2.Caculate total and average score of course\n");
    printf("3.Sort in descending order by score\n");
    printf("4.Sort in ascending order by number\n");
    printf("5.Search by number\n");
    printf("6.Statistic analysis\n");
    printf("7.List record\n");
    printf("0.Exit\n");
    printf("Please Input your choice:\n");
    scanf("%d",&choice);
    if(choice==1)
    {
        printf("Input student's ID, name and score:\n" );
        for( i =0; i<n; i++)
        {
            scanf("%ld%f",&student[i].ID,&student[i].score);
            if(student[i].score<0&&student[i].ID<0)
            {

                break;
            }
        }

     goto begin;
    }

    else if(choice ==2)
    {

        for(int u=0; u<n; u++)
        {
            sum+=student[u].score;
        }
        aver=sum/n;
        printf("sum=%.0f,aver=%.2f\n",sum,aver);
        goto begin;
    }

    else if(choice==3)
    {
        printf("Sort in descending order by score:\n");
        for(int x=0; x<n; x++)
        {
            max =x;
            for(int z=x+1; z<n; z++)
            {
                if(student[max].score<student[z].score)
                {

                    max=z;
                }

            }
            if(max!=x)
            {
                float trmp1 =student[x].score;
                long trmp2 =student[x].ID;
                student[x].score=student[max].score;
                student[x].ID=student[max].ID;
                student[max].score=trmp1;
                student[max].score=trmp2;//错误示例
                /*trmp =student[x];

                student[x]=student[max];

                student[max]=trmp;*/




            }

        }
        for(int p=0; p<n; p++)
        {
            printf("%ld\t%.0f\n",student[p].ID,student[p].score);
        }
        goto begin;

    }

    else if(choice == 4)
    {
        printf("Sort in ascending order by number:\n");
        for(int x=0; x<n; x++)
        {
            max =x;
            for(int z=x+1; z<n; z++)
            {
                if(student[max].ID<student[z].ID)
                {

                    max=z;
                }

            }
            if(max!=x)
            {

                trmp =student[x];

                student[x]=student[max];

                student[max]=trmp;


            }

        }
        for(int x=n-1; x>=0; x--)
        {
            printf("%ld\t%.0f\n",student[x].ID,student[x].score);
        }
    goto begin;
    }

    else if(choice==5)
    {
        printf("Input the number you want to search:\n");
        scanf("%ld",&found);
        int m;
        for(int x=0; x<n; x++)
        {
            if(found==student[x].ID)

            {
                printf("%ld\t%.0f\n",student[x].ID,student[x].score);
                m=0;
                break;
            }
            else
                m=1;

        }
        if(m==1)
        {
            printf("Not found!\n");
        }
        goto begin;
    }

    else if(choice == 6)
    {
        int quality[6]= {0};
        float amount[6];
        for(int x=0; x<n; x++)
        {
            if(student[x].score>=0.0&&student[x].score<=59)
                quality[0]++;
            else if(student[x].score>=60.0&&student[x].score<=69)
                quality[1]++;
            else if(student[x].score>=70.0&&student[x].score<=79)
                quality[2]++;
            else if(student[x].score>=80.0&&student[x].score<=89)
                quality[3]++;
            else if(student[x].score>=90.0&&student[x].score<=99)
                quality[4]++;
            else
                quality[5]++;
        }
        for(int x=0; x<n; x++)
        {
            amount[x]=100.0*quality[x]/n;
        }

        printf("<60\t%d\t%0.2f%%\n",quality[0],amount[0]);//%0.2f%%的含义是:浮点数精确到小数点后两位,且只输出一个%号
        printf("60-69\t%d\t%0.2f%%\n",quality[1],amount[1]);
        printf("70-79\t%d\t%0.2f%%\n",quality[2],amount[2]);
        printf("80-89\t%d\t%0.2f%%\n",quality[3],amount[3]);
        printf("90-99\t%d\t%0.2f%%\n",quality[4],amount[4]);
        printf("100\t%d\t%0.2f%%\n",quality[5],amount[5]);
        goto begin;


    }

    else if(choice == 7)
    {
        for(int x=n-1; x>=0; x--)
        {
            printf("%ld\t%.0f\n",student[x].ID,student[x].score);

        }
        goto begin;
    }

    else if(choice==0)
    {
        printf("End of program!");
        return 0;
    }
    else
    {
        printf("Input error!\n");
        goto begin;
    }
}

(这是错误的代码,换用注释里面的就是正确的)
在以分数来进行排名的时候(choice ==3)
我准备使用选择排序法对分数进行排序,再将每个分数所对应的学号进行替换,在替换部分我是这样做的:

                trmp1 =student[x].score;
                trmp2 =student[x].ID;
                student[x].score=student[max].score;
                student[x].ID=student[max].ID;
                student[max].score=trmp1;
                student[max].score=trmp2;

我至今也没有找出为什么这是错误的;
在这里插入图片描述(使用错误示例)
在这里插入图片描述(但是用注释里面的代码就可以正常的排序)
有大佬知道为什么我的代码是错误的吗…QAQ

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值