c语言实现 问题描述:给出n个学生的考试成绩表,成绩表包括学生的学号、姓名、考试成绩(高等数学、英语、物理),设计一个简单的成绩管理程序。基本要求:(1)建立成绩表,能够插入、删除、修改学生的成绩记录; (2)按任一单科成绩排序; (3)计算每名学生的平均成绩; 4)根据平均成绩将成绩表按由高到低的次序排列,统计每名学生在考试中获得的名次,分数相同的为同一名次,按名次输出成绩表。提高要求:以文件形式保存成绩表,可以从文件读取数据。
时间: 2023-11-22 10:54:13 浏览: 137
以下是一个简单的C语言实现,实现了上述基本要求:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct student {
char id[20];
char name[20];
int math_grade;
int english_grade;
int physics_grade;
float avg_grade;
int rank;
};
struct student students[MAX_STUDENT_NUM];
int student_num = 0;
void print_menu() {
printf("1. 添加学生\n");
printf("2. 删除学生\n");
printf("3. 修改学生信息\n");
printf("4. 按单科成绩排序\n");
printf("5. 计算平均成绩并按排名输出\n");
printf("6. 从文件读取数据\n");
printf("7. 保存数据到文件\n");
printf("8. 退出\n");
}
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数量已达上限\n");
return;
}
struct student s;
printf("请输入学号:");
scanf("%s", s.id);
printf("请输入姓名:");
scanf("%s", s.name);
printf("请输入高数成绩:");
scanf("%d", &s.math_grade);
printf("请输入英语成绩:");
scanf("%d", &s.english_grade);
printf("请输入物理成绩:");
scanf("%d", &s.physics_grade);
s.avg_grade = (s.math_grade + s.english_grade + s.physics_grade) / 3.0;
s.rank = 0;
students[student_num++] = s;
printf("添加成功\n");
}
void delete_student() {
char id[20];
printf("请输入要删除的学生的学号:");
scanf("%s", id);
int i;
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
break;
}
}
if (i == student_num) {
printf("未找到该学生\n");
return;
}
for (int j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("删除成功\n");
}
void modify_student() {
char id[20];
printf("请输入要修改的学生的学号:");
scanf("%s", id);
int i;
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].id, id) == 0) {
break;
}
}
if (i == student_num) {
printf("未找到该学生\n");
return;
}
struct student s;
printf("请输入新的高数成绩:");
scanf("%d", &s.math_grade);
printf("请输入新的英语成绩:");
scanf("%d", &s.english_grade);
printf("请输入新的物理成绩:");
scanf("%d", &s.physics_grade);
s.avg_grade = (s.math_grade + s.english_grade + s.physics_grade) / 3.0;
s.rank = 0;
students[i] = s;
printf("修改成功\n");
}
int cmp_math_grade(const void *a, const void *b) {
return ((struct student *) b)->math_grade - ((struct student *) a)->math_grade;
}
int cmp_english_grade(const void *a, const void *b) {
return ((struct student *) b)->english_grade - ((struct student *) a)->english_grade;
}
int cmp_physics_grade(const void *a, const void *b) {
return ((struct student *) b)->physics_grade - ((struct student *) a)->physics_grade;
}
void sort_by_grade() {
int choice;
printf("请选择排序方式:\n");
printf("1. 按高数成绩排序\n");
printf("2. 按英语成绩排序\n");
printf("3. 按物理成绩排序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
qsort(students, student_num, sizeof(struct student), cmp_math_grade);
printf("按高数成绩排序成功\n");
break;
case 2:
qsort(students, student_num, sizeof(struct student), cmp_english_grade);
printf("按英语成绩排序成功\n");
break;
case 3:
qsort(students, student_num, sizeof(struct student), cmp_physics_grade);
printf("按物理成绩排序成功\n");
break;
default:
printf("无效的选择\n");
}
}
int cmp_avg_grade(const void *a, const void *b) {
if (((struct student *) b)->avg_grade > ((struct student *) a)->avg_grade) {
return 1;
} else if (((struct student *) b)->avg_grade < ((struct student *) a)->avg_grade) {
return -1;
} else {
return 0;
}
}
void calculate_rank() {
qsort(students, student_num, sizeof(struct student), cmp_avg_grade);
int rank = 1;
for (int i = 0; i < student_num; i++) {
if (i > 0 && students[i].avg_grade != students[i - 1].avg_grade) {
rank = i + 1;
}
students[i].rank = rank;
}
printf("计算排名成功\n");
}
void print_students() {
printf("学号\t姓名\t高数成绩\t英语成绩\t物理成绩\t平均成绩\t排名\n");
for (int i = 0; i < student_num; i++) {
printf("%s\t%s\t%d\t\t%d\t\t%d\t\t%.2f\t\t%d\n", students[i].id, students[i].name,
students[i].math_grade, students[i].english_grade, students[i].physics_grade,
students[i].avg_grade, students[i].rank);
}
}
void load_data() {
FILE *fp = fopen("students.txt", "r");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
char id[20], name[20];
int math_grade, english_grade, physics_grade;
while (fscanf(fp, "%s %s %d %d %d", id, name, &math_grade, &english_grade, &physics_grade) == 5) {
struct student s;
strcpy(s.id, id);
strcpy(s.name, name);
s.math_grade = math_grade;
s.english_grade = english_grade;
s.physics_grade = physics_grade;
s.avg_grade = (math_grade + english_grade + physics_grade) / 3.0;
s.rank = 0;
students[student_num++] = s;
}
fclose(fp);
printf("从文件读取数据成功\n");
}
void save_data() {
FILE *fp = fopen("students.txt", "w");
if (fp == NULL) {
printf("无法打开文件\n");
return;
}
for (int i = 0; i < student_num; i++) {
fprintf(fp, "%s %s %d %d %d\n", students[i].id, students[i].name,
students[i].math_grade, students[i].english_grade, students[i].physics_grade);
}
fclose(fp);
printf("保存数据到文件成功\n");
}
int main() {
int choice;
while (1) {
print_menu();
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
delete_student();
break;
case 3:
modify_student();
break;
case 4:
sort_by_grade();
break;
case 5:
calculate_rank();
print_students();
break;
case 6:
load_data();
break;
case 7:
save_data();
break;
case 8:
exit(0);
default:
printf("无效的选择\n");
}
printf("\n");
}
}
```
注意,这只是一个简单的实现,可能存在一些局限性和bug,可以根据实际需求进行扩展和改进。
阅读全文