在一个数据文件中存放若干学生数据记录,每条记录都有如下数据项:学号,姓名,性别,成绩。
编一个程序,采用 顺序存储结构 存储这批数据,并对该数据进行排序。要求:数组前部为男同学,后部为女同学,并且男女同学都按成绩递减排序,分别计算男生合格率、女生合格率、全班的成绩平均分,并把排序后的学生数据记录及计算结果存入另一个数据文件中。
题目:学生顺序表的处理
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- devmiao 2015-06-18 05:32关注
#include "stdio.h" #include <malloc.h> #include <stdlib.h> #include <string> struct Student { int num; char name[20]; int sex;//0女,1男 int score; }stu; struct list { struct Student student; struct list *next; }; struct list *CreatFemale(struct list *head,struct Student stu)//女生信息 { struct list *p; struct list *p1,*p2; p = head; p1 = (struct list *)malloc(sizeof(struct list)); p1->student = stu; if (head == NULL) { head = p1; p1->next = NULL; } else { while (p->next != NULL && stu.score >= p->student.score) { p2 = p; p = p->next; } if (stu.score < p->student.score) { if (p == head) { head = p1; p1->next = p; } else { p2->next = p1; p1->next = p; } } else { p->next = p1; p1->next = NULL; } } return head; } struct list *CreatMale(struct list *head,struct Student stu)//男生信息 { struct list *p; struct list *ptr; struct list *q; struct list *p1,*p2; q = head; ptr = head; p1 = (struct list *)malloc(sizeof(struct list)); p1->student = stu; while (q->next != NULL && q->student.sex == 0) { ptr = q; q = q->next; } p = q; if (head == NULL) { head = p1; p1->next = NULL; } else { while (p->next != NULL && stu.score >= p->student.score) { p2 = p; p = p->next; } if (stu.score < p->student.score && p->student.sex == 1) { if (p == q ) { p = p1; ptr->next = p1; p1->next = q; } else { p2->next = p1; p1->next = p; } } else { p->next = p1; p1->next = NULL; } } return head; } void Print(struct list *head)//输出到文件 { FILE *fp; char Sex[10]; fp = fopen("data1.txt","w"); if (fp == NULL) { printf("can't open the file!\n"); exit(0); } struct list *p; struct list *q; p = head; while (p != NULL) { if (p->student.sex == 0) { strcpy(Sex,"女"); } else { strcpy(Sex,"男"); } fprintf(fp,"%d %s %s %d\n",p->student.num,p->student.name,Sex,p->student.score); q = p->next; free(p); p = q; } fclose(fp); } main() { struct list *head; FILE *fp; head = NULL; fp = fopen("data.txt","r"); if (fp == NULL) { printf("can't open the file!\n"); exit(0); } while (!feof(fp)) { fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score); if (stu.sex == 0) { head = CreatFemale(head,stu);//女 } } fseek(fp,0L,0); while (!feof(fp)) { fscanf(fp,"%d%s%*c%d%d",&stu.num,stu.name,&stu.sex,&stu.score); if (stu.sex == 1) { head = CreatMale(head,stu);//男的 } } fclose(fp); Print(head); }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
- ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
- ¥15 乘性高斯噪声在深度学习网络中的应用
- ¥15 运筹学排序问题中的在线排序
- ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
- ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
- ¥15 C++ 头文件/宏冲突问题解决
- ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
- ¥50 安卓adb backup备份子用户应用数据失败
- ¥20 有人能用聚类分析帮我分析一下文本内容嘛