帮我写一个c语言学生成绩管理系统 内容包括1.输入、修改和显示n个学生的m门考试科目的成绩,计算总分和平均分。 2.分别按总分和学号进行排序。 3.输入学生的学号,显示该学生的姓名、各门课程的成绩。 4.将输入的学生成绩数据保存到文件中。 5.从文件中读出学生成绩数据,查询学生成绩。 6.程序能够进行异常处理,检查用户输入数据的有效性,在用户输入数据有错误(如类型错误)
时间: 2024-03-13 21:43:01 浏览: 56
好的,下面是一个简单的C语言学生成绩管理系统的代码,包括您所需的功能:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 20
#define MAX_SUBJECT_NUM 5
#define MAX_STUDENT_NUM 100
typedef struct {
char name[MAX_NAME_LENGTH];
int scores[MAX_SUBJECT_NUM];
int total_score;
float average_score;
int id;
} Student;
void input_scores(int n, int m, Student students[]);
void print_scores(int n, int m, Student students[]);
void sort_by_total_score(int n, Student students[]);
void sort_by_id(int n, Student students[]);
void search_student(int n, Student students[]);
void save_scores(int n, Student students[], char *filename);
void load_scores(int *n, Student students[], char *filename);
int main() {
int n = 0;
Student students[MAX_STUDENT_NUM];
printf("1. 输入学生成绩\n");
printf("2. 显示学生成绩\n");
printf("3. 按总分排序\n");
printf("4. 按学号排序\n");
printf("5. 查询学生信息\n");
printf("6. 保存学生成绩\n");
printf("7. 读取学生成绩\n");
printf("0. 退出程序\n");
while (1) {
int choice;
printf("\n请输入您的选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
input_scores(n, MAX_SUBJECT_NUM, students);
n++;
break;
case 2:
print_scores(n, MAX_SUBJECT_NUM, students);
break;
case 3:
sort_by_total_score(n, students);
break;
case 4:
sort_by_id(n, students);
break;
case 5:
search_student(n, students);
break;
case 6:
save_scores(n, students, "scores.txt");
break;
case 7:
load_scores(&n, students, "scores.txt");
break;
case 0:
printf("\n程序已退出\n");
exit(0);
default:
printf("\n无效的输入,请重新输入\n");
}
}
return 0;
}
void input_scores(int n, int m, Student students[]) {
printf("\n请输入第%d个学生的信息:\n", n + 1);
printf("姓名:");
scanf("%s", students[n].name);
printf("学号:");
scanf("%d", &students[n].id);
printf("成绩:\n");
for (int i = 0; i < m; i++) {
printf("科目%d:", i + 1);
scanf("%d", &students[n].scores[i]);
students[n].total_score += students[n].scores[i];
}
students[n].average_score = (float) students[n].total_score / m;
printf("学生信息已保存\n");
}
void print_scores(int n, int m, Student students[]) {
printf("\n所有学生的成绩如下:\n");
printf("%-5s %-10s ", "学号", "姓名");
for (int i = 0; i < m; i++) {
printf("科目%d ", i + 1);
}
printf("总分 平均分\n");
for (int i = 0; i < n; i++) {
printf("%-5d %-10s ", students[i].id, students[i].name);
for (int j = 0; j < m; j++) {
printf("%-4d ", students[i].scores[j]);
}
printf("%-4d %.2f\n", students[i].total_score, students[i].average_score);
}
}
void sort_by_total_score(int n, Student students[]) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].total_score < students[j + 1].total_score) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("\n学生成绩已按总分排序\n");
}
void sort_by_id(int n, Student students[]) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].id > students[j + 1].id) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("\n学生成绩已按学号排序\n");
}
void search_student(int n, Student students[]) {
int id;
printf("\n请输入要查询的学生学号:");
scanf("%d", &id);
for (int i = 0; i < n; i++) {
if (students[i].id == id) {
printf("\n该学生信息如下:\n");
printf("姓名:%s\n", students[i].name);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
printf("科目%d:%d\n", j + 1, students[i].scores[j]);
}
printf("总分:%d\n", students[i].total_score);
printf("平均分:%.2f\n", students[i].average_score);
return;
}
}
printf("\n未找到该学生信息\n");
}
void save_scores(int n, Student students[], char *filename) {
FILE *fp = fopen(filename, "w");
for (int i = 0; i < n; i++) {
fprintf(fp, "%d %s ", students[i].id, students[i].name);
for (int j = 0; j < MAX_SUBJECT_NUM; j++) {
fprintf(fp, "%d ", students[i].scores[j]);
}
fprintf(fp, "%d %.2f\n", students[i].total_score, students[i].average_score);
}
fclose(fp);
printf("\n学生成绩已保存到%s\n", filename);
}
void load_scores(int *n, Student students[], char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("\n文件%s不存在\n", filename);
return;
}
int id, scores[MAX_SUBJECT_NUM], total_score;
float average_score;
char name[MAX_NAME_LENGTH];
while (fscanf(fp, "%d %s", &id, name) != EOF) {
for (int i = 0; i < MAX_SUBJECT_NUM; i++) {
fscanf(fp, "%d", &scores[i]);
total_score += scores[i];
}
average_score = (float) total_score / MAX_SUBJECT_NUM;
strcpy(students[*n].name, name);
students[*n].id = id;
memcpy(students[*n].scores, scores, sizeof(scores));
students[*n].total_score = total_score;
students[*n].average_score = average_score;
(*n)++;
}
fclose(fp);
printf("\n从%s中读取了%d条学生成绩信息\n", filename, *n);
}
```
以上是一个简单的C语言学生成绩管理系统的代码,包括您所需的功能。需要注意的是,这只是一个简单的示例,您可以根据实际需求进行修改和扩展。如果您有任何疑问或需要进一步的帮助,请随时联系我。
阅读全文