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

交换变量下标问题

domore 发布于 2019-07-27 09:24, 1533 次点击
#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;
    }
只有本站会员才能查看附件,请 登录
1 回复
#2
zbjzbj2019-07-27 10:27
{temp=stu[i];stu[i]=stu[j];stu[j]=temp;}
1