当前位置:网站首页>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;
} 运行结果:

边栏推荐
猜你喜欢
随机推荐
【软件工程之美 - 专栏笔记】35 | 版本发布:软件上线只是新的开始
数字化时代,企业如何建立自身的云平台与商业模式的选择?
工程水文学试题库
Dialog manager in the fourth chapter: the dialog message loop
关于#sql#的问题,如何解决?
lc marathon 8.2
OpenFOAM extracts equivalency and calculates area
Windows 安装PostgreSQL
数据库性能系列之索引(中)
12.机器学习基础:评估机器学习模型
TCP相关面试常问
online test paper concept
肖sir__简历
富瑞宣布战略交易,以简化运营,持续专注于打造领先的独立全服务型全球投行公司
BIOTIN ALKYNE CAS:773888-45-2价格,供应商
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
Can Oracle EMCC be installed independently?Or does it have to be installed on the database server?
传统企业如何转型社交电商,泰山众筹的玩法有哪些?
常见亲脂性细胞膜染料DiO, Dil, DiR, Did光谱图和实验操作流程
v-text指令:设置标签内容









