当前位置:网站首页>c语言结构体中的冒泡排序
c语言结构体中的冒泡排序
2022-08-03 04:26:00 【BSP初级小学僧】
题目:使用结构体以及函数,首先录入学生信息,依据学生成绩,对学生相关信息进行排序。
#include <stdio.h>
#include <string.h>
struct stu_informa
{
int id;
char name[20];
int sore;
};
void struct_input(struct stu_informa stu[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("请输入第%d个学生信息(id、name、sore):\n",i);
scanf("%d",&stu[i].id);
getchar();
gets(stu[i].name);
scanf("%d",&stu[i].sore);
getchar();
}
printf("\n");
}
void struct_print(struct stu_informa stu[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\n");
printf("stu[%d].id=%d\n",i,stu[i].id);
printf("stu[%d].name=%s\n",i,stu[i].name);
printf("stu[%d].sore=%d\n",i,stu[i].sore);
}
}
void struct_paixu(struct stu_informa stu[],int n)
{
int i,j;
struct stu_informa temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(stu[j].sore>stu[j+1].sore)
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
}
void test(void)
{
struct stu_informa stu[3];
struct_input(stu,3);
struct_paixu(stu,3);
struct_print(stu,3);
}
int main()
{
test();
return 0;
}
运行结果:
边栏推荐
猜你喜欢
随机推荐
9.新闻分类:多分类问题
12.机器学习基础:评估机器学习模型
中断系统需要解决的问题
【STM32】入门(三):按键使用-GPIO端口输出控制
链动2+1模式简单,奖励结构丰厚,自主裂变?
t条件判断语句与if循环
直播|StarRocks 技术内幕 :低基数全局字典优化
富瑞宣布战略交易,以简化运营,持续专注于打造领先的独立全服务型全球投行公司
7.Keras开发简介
WinForm的控件二次开发
【STM32】入门(四):外部中断-按键通过中断动作
C# WPF设备监控软件(经典)-上篇
4.深度学习的几何解释与梯度的优化
urlencode 和rawurlencode的区别
MySQL 入门:Case 语句很好用
online test paper concept
MySQL【约束】
install ambari
leetcode刷题学习之路
安装ambari