C语言:输入多个学生的姓名和成绩,按照成绩由小到大排序

#include<stdio.h>
#include<string.h>
#define N 10
struct student                                 //定义结构体student
{
    char name[20];
    int score;
};
struct student stu[N];                         //全局定义结构体数组变量
void Datesort()
{
int i,j,temp;
char a[20],b[20];
    for(i=0;i<N-1;i++)                         //冒泡法排序
    {
        for(j=i+1;j<N;j++)
{
               if(stu[i].score>stu[j].score)     
{
                temp=stu[i].score;             //交换成绩
                stu[i].score=stu[j].score;
                stu[j].score=temp;
strcpy(a,stu[i].name);       //交换姓名 (字符串的复制函数strcpy(),调用前提为文件头必须有string.h,用a=stu[i].name这样的形式是无法实现复制的。
                strcpy(b,stu[j].name);
                strcpy(stu[j].name,a);
                strcpy(stu[i].name,b);    
        
}
  }
       }
}
void main()
{
    int i;
    printf("please enter 10 students' names and scores\n");
    for(i=0;i<N;i++)
{
        scanf("%s",&stu[i].name);
        scanf("%d",&stu[i].score);
}
Datesort();                               //调用Datesort()函数进行数据排序
    printf("name     score\n");
    for(i=0;i<N;i++)                          //输出排序后的数据
        printf("%s     %d\n",stu[i].name,stu[i].score);
}
  • 26
    点赞
  • 116
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值