注册 登录
编程论坛 C语言论坛

交换变量下标问题

domore 发布于 2019-07-30 06:27, 1760 次点击
#include<stdio.h>
struct Student
    {
    int num;    //学号
    char name[20];    //名字
    float score;     //分数
    };


int main()
    {
    struct Student stu[5]=
            {
            {10010,"Li",80},
            {10015,"Po",90},
            {10020,"Wu",76},
            {10034,"Qi",79},
            {10056,"Zhang",89}
            };
    struct Student temp;    //交换用到的结构体变量temp
    const int n=5;
    int i,j;
    printf("the order is:\n");
    for(i=0;i<n-1;i++)      //按分数高低排序
        {
        for(j=i+1;j<n;j++)
            if(stu[j].score>stu[i].score)
            temp=stu[i];stu[i]=stu[j];stu[j]=temp;
            //temp=stu[j];stu[j]=stu[i];stu[i]=temp;
        }
    for(i=0;i<n;i++)
        printf("%6d %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
    return 0;
    }
只有本站会员才能查看附件,请 登录
3 回复
#2
rjsp2019-07-30 08:32
a. 不要重复发帖;
b. 别人已经回答过了。https://bbs.bccn.net/viewthread.php?tid=496059&page=1#pid2706497
#3
domore2019-08-04 07:17
回复 2楼 rjsp
他答非所问
#4
吹水佬2019-08-04 10:11
以下是引用domore在2019-8-4 07:17:42的发言:

他答非所问

不清楚问的是什么,看代码是数组排序,应该是交换数组元素的数据。
代码好象也有问题,试改改:
//temp=stu[i];stu[i]=stu[j];stu[j]=temp;
{temp=stu[i];stu[i]=stu[j];stu[j]=temp;}
希望不是答非所问

[此贴子已经被作者于2019-8-4 10:12编辑过]

1