用c语言设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别()、出生日期(birthday,包括年、月、日,分别表示为年、月、日)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作: (1)建立学生成绩信息结构数组(从student.txt读入数据) (2)显示。即输出出所有成绩。(3)添加。可添加一条或多条记录。(4)排序。分为两个方面,按学号递增排序和按总分递减排序。(5)查找。分为三方面:按学号查找、按姓名查找和按分数查找。(6)计算。计算各门课的平均分数。(7)保存。退出程序前将结果保存到studeng.txt。2)要求 (1)设计一个简单的界面 (2)每一个功能设为一个模块
时间: 2023-12-10 13:42:55 浏览: 215
很好,下面是一个简单的学生成绩管理系统的C语言程序,共包含七个模块,分别实现了题目要求的七个功能。其中,使用了结构体数组存储学生成绩信息,文件IO读写学生成绩信息,以及对学生成绩信息进行排序、查找、计算等操作。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100 // 最大学生数
#define MAX_NAME_LEN 20 // 最大姓名长度
#define MAX_DATE_LEN 11 // 最大日期长度
// 学生信息结构体
typedef struct {
int num; // 学号
char name[MAX_NAME_LEN]; // 姓名
char gender; // 性别
char birthday[MAX_DATE_LEN]; // 出生日期
float score[3]; // 三门功课成绩
float total_score; // 总分
float average_score; // 平均分
} Student;
// 函数声明
void read_file(Student *students, int *student_num, const char *filename);
void show(Student *students, int student_num);
void add(Student *students, int *student_num);
void sort_by_num(Student *students, int student_num);
void sort_by_score(Student *students, int student_num);
void search_by_num(Student *students, int student_num);
void search_by_name(Student *students, int student_num);
void search_by_score(Student *students, int student_num);
void calculate_average_score(Student *students, int student_num);
void save_file(Student *students, int student_num, const char *filename);
// 主函数
int main() {
Student students[MAX_STUDENT_NUM]; // 学生信息数组
int student_num = 0; // 学生数目
char filename[] = "student.txt"; // 学生成绩信息文件名
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("= 8. 计算各门课的平均分数 =\n");
printf("= 9. 保存学生成绩信息到文件student.txt =\n");
printf("= 0. 退出程序 =\n");
printf("=============================================\n");
printf("请输入您的选择(0-9):");
scanf("%d", &choice);
switch (choice) {
case 0:
// 退出程序
printf("谢谢使用,再见!\n");
exit(0);
case 1:
// 显示所有学生信息
show(students, student_num);
break;
case 2:
// 添加学生信息
add(students, &student_num);
break;
case 3:
// 按学号递增排序
sort_by_num(students, student_num);
break;
case 4:
// 按总分递减排序
sort_by_score(students, student_num);
break;
case 5:
// 按学号查找
search_by_num(students, student_num);
break;
case 6:
// 按姓名查找
search_by_name(students, student_num);
break;
case 7:
// 按分数查找
search_by_score(students, student_num);
break;
case 8:
// 计算各门课的平均分数
calculate_average_score(students, student_num);
break;
case 9:
// 保存学生成绩信息到文件student.txt
save_file(students, student_num, filename);
break;
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
// 从文件中读取学生成绩信息
void read_file(Student *students, int *student_num, const char *filename) {
FILE *fp;
if ((fp = fopen(filename, "r")) == NULL) {
printf("无法打开文件%s!\n", filename);
exit(-1);
}
while (!feof(fp)) {
fscanf(fp, "%d %s %c %s %f %f %f\n",
&students[*student_num].num, students[*student_num].name,
&students[*student_num].gender, students[*student_num].birthday,
&students[*student_num].score[0], &students[*student_num].score[1],
&students[*student_num].score[2]);
students[*student_num].total_score = students[*student_num].score[0] +
students[*student_num].score[1] +
students[*student_num].score[2];
students[*student_num].average_score = students[*student_num].total_score / 3.0;
(*student_num)++;
}
fclose(fp);
}
// 显示所有学生信息
void show(Student *students, int student_num) {
int i;
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
for (i = 0; i < student_num; i++) {
printf("%d\t%s\t%c\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].num,
students[i].name, students[i].gender, students[i].birthday,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].average_score);
}
}
// 添加学生信息
void add(Student *students, int *student_num) {
int i, n;
printf("请输入要添加的学生数目:");
scanf("%d", &n);
if (*student_num + n > MAX_STUDENT_NUM) {
printf("学生数目超过数组最大容量%d!\n", MAX_STUDENT_NUM);
return;
}
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息(学号 姓名 性别 出生日期 高数成绩 C语言成绩 大学物理成绩):",
i + 1);
scanf("%d %s %c %s %f %f %f", &students[*student_num].num, students[*student_num].name,
&students[*student_num].gender, students[*student_num].birthday,
&students[*student_num].score[0], &students[*student_num].score[1],
&students[*student_num].score[2]);
students[*student_num].total_score = students[*student_num].score[0] +
students[*student_num].score[1] +
students[*student_num].score[2];
students[*student_num].average_score = students[*student_num].total_score / 3.0;
(*student_num)++;
}
printf("添加成功!\n");
}
// 按学号递增排序
void sort_by_num(Student *students, int student_num) {
int i, j;
Student temp;
for (i = 0; i < student_num - 1; i++) {
for (j = 0; j < student_num - i - 1; j++) {
if (students[j].num > students[j + 1].num) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按学号递增排序成功!\n");
}
// 按总分递减排序
void sort_by_score(Student *students, int student_num) {
int i, j;
Student temp;
for (i = 0; i < student_num - 1; i++) {
for (j = 0; j < student_num - i - 1; j++) {
if (students[j].total_score < students[j + 1].total_score) {
temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printf("按总分递减排序成功!\n");
}
// 按学号查找
void search_by_num(Student *students, int student_num) {
int i, num;
printf("请输入要查找的学号:");
scanf("%d", &num);
for (i = 0; i < student_num; i++) {
if (students[i].num == num) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%d\t%s\t%c\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].num,
students[i].name, students[i].gender, students[i].birthday,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].average_score);
return;
}
}
printf("未找到学号为%d的学生!\n", num);
}
// 按姓名查找
void search_by_name(Student *students, int student_num) {
int i;
char name[MAX_NAME_LEN];
printf("请输入要查找的姓名:");
scanf("%s", name);
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%d\t%s\t%c\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].num,
students[i].name, students[i].gender, students[i].birthday,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].average_score);
}
}
printf("未找到姓名为%s的学生!\n", name);
}
// 按分数查找
void search_by_score(Student *students, int student_num) {
int i, j;
float score;
int found = 0;
printf("请输入要查找的分数:");
scanf("%f", &score);
for (i = 0; i < student_num; i++) {
for (j = 0; j < 3; j++) {
if (students[i].score[j] == score) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%d\t%s\t%c\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", students[i].num,
students[i].name, students[i].gender, students[i].birthday,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].average_score);
found = 1;
break;
}
}
}
if (!found) {
printf("未找到分数为%.2f的学生!\n", score);
}
}
// 计算各门课的平均分数
void calculate_average_score(Student *students, int student_num) {
int i, j;
float average_score[3] = {0};
for (i = 0; i < student_num; i++) {
for (j = 0; j < 3; j++) {
average_score[j] += students[i].score[j];
}
}
for (j = 0; j < 3; j++) {
average_score[j] /= student_num;
}
printf("各门课的平均分数为:高数%.2f,C语言%.2f,大学物理%.2f\n",
average_score[0], average_score[1], average_score[2]);
}
// 保存学生成绩信息到文件student.txt
void save_file(Student *students, int student_num, const char *filename) {
FILE *fp;
int i;
if ((fp = fopen(filename, "w")) == NULL) {
printf("无法打开文件%s!\n", filename);
exit(-1);
}
for (i = 0; i < student_num; i++) {
fprintf(fp, "%d %s %c %s %.2f %.2f %.2f\n", students[i].num, students[i].name,
students[i].gender, students[i].birthday, students[i].score[0],
students[i].score[1], students[i].score[2]);
}
fclose(fp);
printf("保存学生成绩信息到文件%s成功!\n", filename);
}
```
阅读全文