/*动态构造存放学生的结构体数组
动态构造一个数组,存放学生的信息
然后按分数排序 */
#include<stdio.h>
#include<malloc.h>
struct Student{
int age;
char name[100];
float score;
};
int main(){
struct Student*pArr;
int len,i,j;
struct Student t;
printf("请输入学生的人数:");
scanf("%d",&len);
printf("len=%d\n",len);
pArr=(struct Student*)malloc(len*sizeof(struct Student)); //动态分配数组
for(i=0;i<len;i++){
printf("请输入第%d个学生的信息:\n",i+1);
printf("age=");
scanf("%d",&pArr[i].age);
printf("name=");
scanf("%s",pArr[i].name);
printf("score=");
scanf("%f",&pArr[i].score);
}
//排序
for(i=0;i<len-1;i++){
for(j=0;j<len-1-i;j++){
if(pArr[j].score>pArr[j+1].score){
t=pArr[j];
pArr[j]=pArr[j+1];
pArr[j+1]=t;
}
}
}
//输出
printf("\n\n学生的信息:\n");
for(i=0;i<len;i++){
printf("输出第%d个学生的信息:\n",i+1);
printf("age=%d\n",pArr[i].age);
printf("name=%s\n",pArr[i].name);
printf("score=%f\n",pArr[i].score);
}
printf("\n");
}
//代码如上,请问把代码里的输入,输出,冒泡排序的for循环写成函数调用的话怎么写呢?麻烦告诉我简单具体的写法,不太掌握这方面的,谢谢!
动态构造存放学生的结构体数组函数版怎么写?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 商朕 2020-03-29 17:05关注
Bubble_Sort(pArr,len);
void Bubble_Sort(Student student[], int len){
int i = 0;
int j = 0;for(i=0;i<len-1;i++){ for(j=0;j<len-1-i;j++){ if(student[j].score > student[j+1].score){ t=student[j]; student[j]=student[j+1]; student[j+1]=t; } } }
}
解决 无用评论 打赏 举报
悬赏问题
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能
- ¥15 jmeter脚本回放有的是对的有的是错的
- ¥15 r语言蛋白组学相关问题
- ¥15 Python时间序列如何拟合疏系数模型