#include <stdio.h>#include <stdlib.h>#include <string.h>// 定义学生结构体struct student { char id[20]; // 学号 char name[20]; // 姓名 char class[20]; // 班级 int math; // 高数成绩 int eng; // 英语成绩 int total; // 总成绩};// 定义链表节点结构体struct node { struct student stu; struct node *next;};// 定义全局变量int count = 0; // 记录学生人数struct student students[50]; // 学生数组struct node *head = NULL; // 链表头指针// 主函数int main() { // 显示菜单 int choice; while (1) { printf("===========================\n"); printf(" 菜单 \n"); printf("===========================\n"); printf("[1] 录入学生信息\n"); printf("[2] 按学号排序\n"); printf("[3] 插入学生信息\n"); printf("[4] 删除学生信息\n"); printf("[5] 查询学生信息\n"); printf("[6] 统计成绩\n"); printf("[7] 输出所有学生信息\n"); printf("[0] 退出程序\n"); printf("请选择操作:"); scanf("%d", &choice); // 执行对应操作 switch (choice) { case 1: input_students(); break; case 2: sort_by_id(); break; case 3: insert_students(); break; case 4: delete_students(); break; case 5: search_students(); break; case 6: statistics(); break; case 7: output_students(); break; case 0: printf("程序已退出.\n"); return 0; default: printf("无效操作,请重新选择.\n"); break; } } return 0;}// 录入学生信息int input_students() { printf("请输入学生信息(输入-1结束):\n"); while (1) { // 判断是否结束录入 if (count == 50) { printf("学生人数已达到最大值50人,录入终止.\n"); break; } char id[20]; printf("学号:"); scanf("%s", id); if (strcmp(id, "-1") == 0) { printf("学生信息录入完毕.\n"); break; } int i; for (i = 0; i < count; i++) { // 判断是否已存在该学生信息 if (strcmp(students[i].id, id) == 0) { printf("该学号已存在,请重新输入.\n"); break; } } if (i == count) { strcpy(students[count].id, id); printf("姓名:"); scanf("%s", students[count].name); printf("班级:"); scanf("%s", students[count].class); printf("高数成绩:"); scanf("%d", &students[count].math); printf("英语成绩:"); scanf("%d", &students[count].eng); students[count].total = students[count].math + students[count].eng; // 计算总成绩 count++; printf("学生信息录入成功.\n"); } } return 0;} // 按学号排序int sort_by_id() { int i, j; struct student temp; for (i = 0; i < count - 1; i++) { for (j = 0; j < count - 1 - i; j++) { if (strcmp(students[j].id, students[j + 1].id) > 0) { temp = students[j]; students[j] = students[j + 1]; students[j + 1] = temp; } } } printf("按学号排序成功.\n"); return 0;}// 插入学生信息int insert_students() { // 读取要插入的学生信息 struct student new_stu; printf("请输入要插入的学生信息:\n"); printf("学号:"); scanf("%s", new_stu.id); printf("姓名:"); scanf("%s", new_stu.name); printf("班级:"); scanf("%s", new_stu.class); printf("高数成绩:"); scanf("%d", &new_stu.math); printf("英语成绩:"); scanf("%d", &new_stu.eng); new_stu.total = new_stu.math + new_stu.eng; // 计算总成绩 // 判断是否已存在该学号 int i, j; for (i = 0; i < count; i++) { if (strcmp(students[i].id, new_stu.id) == 0) { printf("该学号已存在,插入失败.\n"); } } // 插入节点并排序 struct node *p = head; struct node *prev = NULL; struct node *new_node = (struct node*) malloc(sizeof(struct node)); new_node->stu = new_stu; new_node->next = NULL; while (p != NULL && strcmp(p->stu.id, new_stu.id) > 0) { prev = p; p = p->next; } if (prev == NULL) { new_node->next = head; head = new_node; } else { prev->next = new_node; new_node->next = p; } printf("学生信息插入成功.\n"); return 0;}// 删除学生信息int delete_students() { char id[20]; printf("请输入要删除的学生学号:"); scanf("%s", id); // 在数组中查找并删除 int i, j; for (i = 0; i < count; i++) { if (strcmp(students[i].id, id) == 0) { for (j = i; j < count - 1; j++) { students[j] = students[j + 1]; } count--; printf("学生信息删除成功.\n"); } } // 在链表中查找并删除 struct node *p = head; struct node *prev = NULL; while (p != NULL && strcmp(p->stu.id, id) != 0) { prev = p; p = p->next; } if (p == NULL) { printf("未找到该学号对应的学生信息,删除失败.\n"); } else { if (prev == NULL) { head = p->next; } else { prev->next = p->next; } free(p); printf("学生信息删除成功.\n"); } return 0;}// 查询学生信息int search_students() { int choice; printf("===========================\n"); printf(" 查询菜单 \n"); printf("===========================\n"); printf("[1] 按学号查询\n"); printf("[2] 按姓名查询\n"); printf("[3] 按分数段查询\n"); printf("请选择查询方式:"); scanf("%d", &choice); switch (choice) { case 1: { char id[20]; printf("请输入要查询的学生学号:"); scanf("%s", id); int i; for (i = 0; i < count; i++) { if (strcmp(students[i].id, id) == 0) { printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n"); printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].class, students[i].math, students[i].eng, students[i].total); } } printf("未找到该学号对应的学生信息.\n"); break; } case 2: { char name[20]; printf("请输入要查询的学生姓名:"); scanf("%s", name); int i; for (i = 0; i < count; i++) { if (strcmp(students[i].name, name) == 0) { printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n"); printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].class, students[i].math, students[i].eng, students[i].total); } } printf("未找到该姓名对应的学生信息.\n"); break; } case 3: { int section; printf("请输入分数段(每隔10分为一个段):"); scanf("%d", §ion); int i, j; for (i = 0; i <= 100; i += 10) { if (section == i) { printf("成绩在%d~%d分之间的学生有:\n", i, i + 9); printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n"); for (j = 0; j < count; j++) { if (students[j].total >= i && students[j].total <= i + 9) { printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[j].id, students[j].name, students[j].class, students[j].math, students[j].eng, students[j].total); } } } } printf("无效分数段,请重新查询.\n"); break; } default: printf("无效查询方式,请重新选择.\n"); break; } return 0;}// 统计成绩int statistics() { int math_total = 0, eng_total = 0, total_total = 0; // 各科总分 int i; for (i = 0; i < count; i++) { math_total += students[i].math; eng_total += students[i].eng; total_total += students[i].total; } float math_avg = (float) math_total / count; // 高数平均分 float eng_avg = (float) eng_total / count; // 英语平均分 float total_avg = (float) total_total / count; // 总成绩平均分 printf("高数总分:%d,平均分:%.2f\n", math_total, math_avg); printf("英语总分:%d,平均分:%.2f\n", eng_total, eng_avg); printf("总成绩总分:%d,平均分:%.2f\n", total_total, total_avg); return 0;}// 输出所有学生信息int output_students() { printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n"); int i; for (i = 0; i < count; i++) { printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name, students[i].class, students[i].math, students[i].eng, students[i].total); } struct node *p = head; while (p != NULL) { printf("%s\t%s\t%s\t%d\t%d\t%d\n", p->stu.id, p->stu.name, p->stu.class, p->stu.math, p->stu.eng, p->stu.total); p = p->next; } return 0;}
自己慢慢调,做一步就执行看看,不要写好几百行一起执行
你这代码咋看
整体修改完善如下,改动处见注释,供参考。查询里 按分数段查询写法有问题,不能实现功能,留给题主解决。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
struct student {
char id[20]; // 学号
char name[20]; // 姓名
char Class[20]; // 班级 //char class[20]; 修改
int math; // 高数成绩
int eng; // 英语成绩
int total; // 总成绩
};
// 定义链表节点结构体
struct node {
struct student stu;
struct node* next;
};
// 定义全局变量
int count = 0; // 记录学生人数
struct student students[50]; // 学生数组
struct node* head = NULL; // 链表头指针
int input_students();
int sort_by_id();
int insert_students();
int delete_students();
int search_students();
int statistics();
int output_students();
void insert_node(struct student stu); // 修改
// 主函数
int main()
{
// 显示菜单
int choice;
while (1) {
printf("===========================\n");
printf(" 菜单 \n");
printf("===========================\n");
printf("[1] 录入学生信息\n");
printf("[2] 按学号排序\n");
printf("[3] 插入学生信息\n");
printf("[4] 删除学生信息\n");
printf("[5] 查询学生信息\n");
printf("[6] 统计成绩\n");
printf("[7] 输出所有学生信息\n");
printf("[0] 退出程序\n");
printf("请选择操作:");
scanf("%d", &choice); // 执行对应操作
switch (choice) {
case 1: input_students();
break;
case 2: sort_by_id();
break;
case 3: insert_students();
break;
case 4: delete_students();
break;
case 5: search_students();
break;
case 6: statistics();
break;
case 7: output_students();
break;
case 0: printf("程序已退出.\n");
return 0;
default: printf("无效操作,请重新选择.\n");
break;
}
}
return 0;
}
// 链表按学号降序插入
void insert_node(struct student stu) // 修改
{
struct node* p = head;
struct node* prev = NULL;
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->stu = stu;
new_node->next = NULL;
while (p != NULL && strcmp(p->stu.id, stu.id) > 0)
{
prev = p;
p = p->next;
}
if (prev == NULL) {
new_node->next = head;
head = new_node;
}
else {
prev->next = new_node;
new_node->next = p;
}
}
// 录入学生信息
int input_students()
{
printf("请输入学生信息(输入-1结束):\n");
while (1) {
// 判断是否结束录入
if (count == 50) {
printf("学生人数已达到最大值50人,录入终止.\n");
break;
}
char id[20];
printf("学号:");
scanf("%s", id);
if (strcmp(id, "-1") == 0) {
printf("学生信息录入完毕.\n");
break;
}
int i;
for (i = 0; i < count; i++) { // 判断是否已存在该学生信息
if (strcmp(students[i].id, id) == 0) {
printf("该学号已存在,请重新输入.\n");
break;
}
}
if (i == count) {
strcpy(students[count].id, id);
printf("姓名:");
scanf("%s", students[count].name);
printf("班级:");
scanf("%s", students[count].Class);
printf("高数成绩:");
scanf("%d", &students[count].math);
printf("英语成绩:");
scanf("%d", &students[count].eng);
students[count].total = students[count].math + students[count].eng; // 计算总成绩
insert_node(students[count]); // 修改
count++;
printf("学生信息录入成功.\n");
}
} return 0;
}
// 按学号排序
int sort_by_id()
{
int i, j;
struct student temp;
for (i = 0; i < count - 1; i++) {
for (j = 0; j < count - 1 - i; j++) {
if (strcmp(students[j].id, students[j + 1].id) < 0) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按学号排序成功.\n");
return 0;
}
// 插入学生信息
int insert_students()
{ // 读取要插入的学生信息
int i, j;
struct student new_stu;
printf("请输入要插入的学生信息:\n");
printf("学号:");
scanf("%s", new_stu.id);
for (i = 0; i < count; i++) { // 判断是否已存在该学号 修改
if (strcmp(students[i].id, new_stu.id) == 0) {
printf("该学号已存在,插入失败.\n");
return 1; // 修改
}
}
printf("姓名:");
scanf("%s", new_stu.name);
printf("班级:");
scanf("%s", new_stu.Class);
printf("高数成绩:");
scanf("%d", &new_stu.math);
printf("英语成绩:");
scanf("%d", &new_stu.eng);
new_stu.total = new_stu.math + new_stu.eng; // 计算总成绩
students[count++] = new_stu; // 修改
// 插入节点并排序
insert_node(new_stu); // 修改
printf("学生信息插入成功.\n");
return 0;
}
// 删除学生信息
int delete_students()
{
char id[20];
printf("请输入要删除的学生学号:");
scanf("%s", id);
// 在数组中查找并删除
int i, j;
for (i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
for (j = i; j < count - 1; j++) {
students[j] = students[j + 1];
}
count--;
printf("学生信息删除成功.\n");
}
}
// 在链表中查找并删除
struct node* p = head;
struct node* prev = NULL;
while (p != NULL && strcmp(p->stu.id, id) != 0)
{
prev = p;
p = p->next;
}
if (p == NULL) {
printf("未找到该学号对应的学生信息,删除失败.\n");
}
else {
if (prev == NULL) {
head = p->next;
}
else {
prev->next = p->next;
}
free(p);
printf("学生信息删除成功.\n");
}
return 0;
}
// 查询学生信息
int search_students()
{
int choice;
printf("===========================\n");
printf(" 查询菜单 \n");
printf("===========================\n");
printf("[1] 按学号查询\n");
printf("[2] 按姓名查询\n");
printf("[3] 按分数段查询\n");
printf("请选择查询方式:");
scanf("%d", &choice);
switch (choice) {
case 1: {
char id[20];
printf("请输入要查询的学生学号:");
scanf("%s", id);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n");
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].Class, students[i].math, students[i].eng, students[i].total);
break;
}
}
if (i == count)
printf("未找到该学号对应的学生信息.\n");
break;
}
case 2: {
char name[20];
printf("请输入要查询的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n");
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].Class, students[i].math, students[i].eng, students[i].total);
break;
}
}
if (i == count)
printf("未找到该姓名对应的学生信息.\n");
break;
}
case 3: {
int section;
printf("请输入分数段(每隔10分为一个段):");
scanf("%d", §ion);
int i, j;
for (i = 0; i <= 100; i += 10) {
if (section == i) {
printf("成绩在%d~%d分之间的学生有:\n", i, i + 9);
printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n");
for (j = 0; j < count; j++) {
if (students[j].total >= i && students[j].total <= i + 9) {
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[j].id, students[j].name,
students[j].Class, students[j].math, students[j].eng, students[j].total);
}
}
}
}
printf("无效分数段,请重新查询.\n");
break;
}
default:
printf("无效查询方式,请重新选择.\n");
break;
}
return 0;
}
// 统计成绩
int statistics()
{
int math_total = 0, eng_total = 0, total_total = 0; // 各科总分
int i;
for (i = 0; i < count; i++) {
math_total += students[i].math;
eng_total += students[i].eng;
total_total += students[i].total;
}
float math_avg = (float)math_total / count; // 高数平均分
float eng_avg = (float)eng_total / count; // 英语平均分
float total_avg = (float)total_total / count; // 总成绩平均分
printf("高数总分:%d,平均分:%.2f\n", math_total, math_avg);
printf("英语总分:%d,平均分:%.2f\n", eng_total, eng_avg);
printf("总成绩总分:%d,平均分:%.2f\n", total_total, total_avg);
return 0;
}
// 输出所有学生信息
int output_students()
{
printf("学号\t姓名\t班级\t高数\t英语\t总成绩\n");
int i;
for (i = 0; i < count; i++) {
printf("%s\t%s\t%s\t%d\t%d\t%d\n", students[i].id, students[i].name,
students[i].Class, students[i].math, students[i].eng, students[i].total);
}
struct node* p = head;
while (p != NULL) {
printf("%s\t%s\t%s\t%d\t%d\t%d\n", p->stu.id, p->stu.name,
p->stu.Class, p->stu.math, p->stu.eng, p->stu.total);
p = p->next;
}
return 0;
}