有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序, 成绩, 年龄
2014-07-24 19:54
781 查看
typedef struct stu {
char name[20];//存储学生姓名
char sex;//存储学生性别
int age;//存储学生年龄
float score;
}Stu;
Stu stu[5] = {
{"mengmeng", 'N',38,
100},
{"shuaishuai", 'G',28,
10},
{"leilei", 'M',21,
23},
{"mingming", 'G',20,
39},
{"honghong", 'G',12,
101}
};
//输出结构体的结构体数组成员
void printAllStudentInfo(Stu a[],int count);//a用来接收结构体数组, count用来接收数组元素的个数
void sortStudentScore(Stu a[],int count);//按成绩排序
void sortStudentAge(Stu a[],int count);//按年龄排序
void sortStudentName(Stu a[],int count);//按姓名排序
void printAllStudentInfo(Stu a[],int count) {
for (int i =
0; i < count; i++) {
printf("name : %s, sex : %c, age : %d, score : %.2f\n", a[i].name, a[i].sex,
a[i].age, a[i].score);
}
}//打印所有学生的信息
void sortStudentScore(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].score > a[j +1].score) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按成绩排序
void sortStudentAge(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].age > a[j +1].age) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按年龄排序
void sortStudentName(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (strcmp(tempStudent.name, a[i].name) <0) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按姓名排序
printf("原结构体学生列表为:\n");
printAllStudentInfo(stu,5);
printf("按学生的成绩升序排序为:\n");
sortStudentScore(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的姓名降序排序排序为:\n");
sortStudentName(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的年龄从升序排序排序为:\n");
sortStudentAge(stu,
5);
printAllStudentInfo(stu,5);
char name[20];//存储学生姓名
char sex;//存储学生性别
int age;//存储学生年龄
float score;
}Stu;
Stu stu[5] = {
{"mengmeng", 'N',38,
100},
{"shuaishuai", 'G',28,
10},
{"leilei", 'M',21,
23},
{"mingming", 'G',20,
39},
{"honghong", 'G',12,
101}
};
//输出结构体的结构体数组成员
void printAllStudentInfo(Stu a[],int count);//a用来接收结构体数组, count用来接收数组元素的个数
void sortStudentScore(Stu a[],int count);//按成绩排序
void sortStudentAge(Stu a[],int count);//按年龄排序
void sortStudentName(Stu a[],int count);//按姓名排序
void printAllStudentInfo(Stu a[],int count) {
for (int i =
0; i < count; i++) {
printf("name : %s, sex : %c, age : %d, score : %.2f\n", a[i].name, a[i].sex,
a[i].age, a[i].score);
}
}//打印所有学生的信息
void sortStudentScore(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].score > a[j +1].score) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按成绩排序
void sortStudentAge(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (a[j].age > a[j +1].age) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按年龄排序
void sortStudentName(Stu a[],int count) {
Stu tempStudent = {0};
for (int i =
0; i < 4; i++) {
for (int j =0; j <
4 - i; j++) {
if (strcmp(tempStudent.name, a[i].name) <0) {
tempStudent = a[j + 1];
a[j + 1] = a[j];
a[j] = tempStudent;
}
}
}
}//按姓名排序
printf("原结构体学生列表为:\n");
printAllStudentInfo(stu,5);
printf("按学生的成绩升序排序为:\n");
sortStudentScore(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的姓名降序排序排序为:\n");
sortStudentName(stu,
5);
printAllStudentInfo(stu,5);
printf("按学生的年龄从升序排序排序为:\n");
sortStudentAge(stu,
5);
printAllStudentInfo(stu,5);
相关文章推荐
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- C语言,有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 有5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- 有 4000 5名学生保存在结构体数组中,编程按学生的成绩升序排序,按学生的姓名降序排序,按年龄从低到高排序
- //有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生全部信息。 // //对上述5名学生数组,按成绩从高到低排序,并输出。
- 有5名学生保存在结构体数组中,编程查找成绩最高者,输出该学生的全部信息.
- 对数组中的自定义的类型根据姓名,年龄排序(升序, 降序)sortedArrayUsingSelector
- 结构体指针数组完成输入学生姓名学号数学语文成绩修改排序显示等功能
- 在一个长度为10的整型数组里面,保存了班级10个学生的考试成绩。要求编写5个函数,分别实现计算考试的总分,最高分,最低分,平均分和考试成绩降序排序
- 数组排序之对一组学生按照成绩, 学号,年龄, 姓名排序~
- 第一周:项目3 学生成绩的结构体数组(包括学号、姓名、三门课成绩、总分、均分)
- 学生结构体数组,按成绩排序、按年 龄排序,按名字排序...如何实现?
- 结构体数组经典例题--学生成绩的输入,总分及平均分----编程随笔(3)
- 问题十六:每位同学的信息学号、姓名、C++、高数、英语成绩,定义一个学生成绩的结构体数组。
- C/C++动态自定义结构体数组实例锻炼-学生成绩排序
- 创建一个学生类(Student),包括学号和成绩,编程输入和显示学生的信息,建立一个人类(Person),包含姓名、性别和年龄,并作为学生类的基类
- (C语言-结构体)主函数创建5个学生的数组,写一个排序函数,让学生按姓名从小到大排序,主函数输出排序后的结果。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个 Student 类型的对象数组,对学号进行 升序排序并输出
- 十六周任务二 学生成绩排序文件score.dat 中保存的是100 名学生的姓名和C++课、高数和英语成绩,将成绩排序后保存到ordered_score.dat文件中
- 1.给出一个Person类里面包含姓名、年龄、成绩,声明5个Person对象数组,要求对数组中的内容进行排序,排序规则如下:按成绩由高到低排序,如果成绩一样,按年龄由高到低排序。