|
楼主 |
发表于 2024-10-16 11:00:38
|
显示全部楼层
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define NAME_LENGTH 11 // 10 characters + 1 for null terminator
typedef struct {
long examNumber; // 准考证号
char name[NAME_LENGTH]; // 姓名
int gender; // 性别 (1-男, 0-女)
int age; // 年龄
int score; // 高考总成绩
} Student;
typedef struct {
Student students[MAX_STUDENTS]; // 存放学生信息的数组
int count; // 当前学生数量
} SqList;
// 函数声明
void CreateSeqList(SqList *L);
void PrintSeqList(SqList L);
void SortSeqList(SqList *L);
void SplitSeqList(SqList L, SqList *Boy, SqList *Girl);
int main() {
SqList studentList;
SqList maleList;
SqList femaleList;
// 初始化
studentList.count = 0;
maleList.count = 0;
femaleList.count = 0;
// 创建考生信息表
CreateSeqList(&studentList);
// 打印考生信息
PrintSeqList(studentList);
// 排序考生信息
SortSeqList(&studentList);
printf("排序后\n");
PrintSeqList(studentList);
// 拆分考生信息表
SplitSeqList(studentList, &maleList, &femaleList);
// 打印男生信息
printf("男生信息\n");
PrintSeqList(maleList);
// 打印女生信息
printf("女生信息\n");
PrintSeqList(femaleList);
return 0;
}
// 创建考生信息表
void CreateSeqList(SqList *L) {
scanf("%d", &L->count);
if (L->count > MAX_STUDENTS) {
printf("考生数量超过限制,设置为最大值 %d。\n", MAX_STUDENTS);
L->count = MAX_STUDENTS;
}
for (int i = 0; i < L->count; i++) {
scanf("%ld,%10[^,],%d,%d,%d", &L->students[i].examNumber, L->students[i].name,
&L->students[i].gender, &L->students[i].age, &L->students[i].score);
}
}
// 打印考生信息
void PrintSeqList(SqList L) {
if (L.count == 0) {
printf("无\n");
return;
}
printf("考生号,姓名,性别,年龄,成绩\n");
for (int i = 0; i < L.count; i++) {
printf("%ld,%s,%s,%d岁,%d分\n",
L.students[i].examNumber,
L.students[i].name,
L.students[i].gender ? "男" : "女",
L.students[i].age,
L.students[i].score);
}
}
// 按高考总成绩排序
void SortSeqList(SqList *L) {
for (int i = 0; i < L->count - 1; i++) {
for (int j = 0; j < L->count - i - 1; j++) {
// 排序条件:先按成绩降序,再按考生号升序
if (L->students[j].score < L->students[j + 1].score ||
(L->students[j].score == L->students[j + 1].score &&
L->students[j].examNumber > L->students[j + 1].examNumber)) {
Student temp = L->students[j];
L->students[j] = L->students[j + 1];
L->students[j + 1] = temp;
}
}
}
}
// 按性别分组
void SplitSeqList(SqList L, SqList *Boy, SqList *Girl) {
for (int i = 0; i < L.count; i++) {
if (L.students[i].gender == 1) {
Boy->students[Boy->count++] = L.students[i];
} else {
Girl->students[Girl->count++] = L.students[i];
}
}
}
输出格式不要动,顺序表改为链表预期输出——考生号,姓名,性别,年龄,成绩
20210303,李铁,女,20岁,472分
20210302,金鑫,女,21岁,472分
20210301,陈彧其,女,19岁,523分
20210304,顾与其,女,19岁,526分
排序后
考生号,姓名,性别,年龄,成绩
20210304,顾与其,女,19岁,526分
20210301,陈彧其,女,19岁,523分
20210302,金鑫,女,21岁,472分
20210303,李铁,女,20岁,472分
男生信息
无
女生信息
考生号,姓名,性别,年龄,成绩
20210304,顾与其,女,19岁,526分
20210301,陈彧其,女,19岁,523分
20210302,金鑫,女,21岁,472分
20210303,李铁,女,20岁,472分 |
|