设计一个简单的学生成绩管理系统。设有学生成绩文件student.txt,每位学生成绩信息包含学号(num)、姓名(name)、性别(sex)、出生日期(birthday,包括年、月、日,分别表示为year,month,day)、3门功课的成绩(高数、C语言、大学物理)、总分和平均分。设计程序,要求从文件student.txt中读出学生成绩表,之后参对成绩表进行如下操作: (1)建立学生成绩信息结构数组(从student.txt读入数据) (2)显示。即输出出所有成绩。 (3)添加。可添加一条或多条记录。 (4)排序。分为两个方面,按学号递增排序和按总分递减排序。 (5)查找。分为三方面:按学号查找、按姓名查找和按分数查找。 (6)计算。计算各门课的平均分数。 (7)保存。退出程序前将结果保存到studeng.txt。 2)要求 (1)设计一个简单的界面 (2)每一个功能设为一个模块并将其用c语言表示出来
时间: 2023-11-27 20:51:41 浏览: 72
以下是一个简单的学生成绩管理系统的C语言实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
// 学生信息结构体
typedef struct {
char num[10]; // 学号
char name[20]; // 姓名
char sex[5]; // 性别
int year, month, day; // 出生日期
float score[3]; // 3门课程成绩
float total_score; // 总分
float avg_score; // 平均分
} Student;
// 学生信息结构体数组
Student students[MAX_STUDENTS];
int student_count = 0;
// 建立学生信息结构体数组
void build_students() {
FILE *fp = fopen("student.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n");
exit(1);
}
while (!feof(fp)) { // 读到文件结尾
fscanf(fp, "%s %s %s %d-%d-%d %f %f %f\n",
students[student_count].num,
students[student_count].name,
students[student_count].sex,
&students[student_count].year,
&students[student_count].month,
&students[student_count].day,
&students[student_count].score[0],
&students[student_count].score[1],
&students[student_count].score[2]);
students[student_count].total_score = students[student_count].score[0] + students[student_count].score[1] + students[student_count].score[2];
students[student_count].avg_score = students[student_count].total_score / 3.0;
student_count++;
}
fclose(fp);
}
// 显示所有学生信息
void show_students() {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
for (int i = 0; i < student_count; i++) {
printf("%s\t%s\t%s\t%d-%d-%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",
students[i].num, students[i].name, students[i].sex,
students[i].year, students[i].month, students[i].day,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].avg_score);
}
}
// 添加学生信息
void add_student() {
printf("请输入学生信息(学号 姓名 性别 出生日期 高数成绩 C语言成绩 大学物理成绩):\n");
scanf("%s %s %s %d-%d-%d %f %f %f",
students[student_count].num,
students[student_count].name,
students[student_count].sex,
&students[student_count].year,
&students[student_count].month,
&students[student_count].day,
&students[student_count].score[0],
&students[student_count].score[1],
&students[student_count].score[2]);
students[student_count].total_score = students[student_count].score[0] + students[student_count].score[1] + students[student_count].score[2];
students[student_count].avg_score = students[student_count].total_score / 3.0;
student_count++;
}
// 按学号递增排序
void sort_by_num() {
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (strcmp(students[i].num, students[j].num) > 0) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 按总分递减排序
void sort_by_score() {
for (int i = 0; i < student_count - 1; i++) {
for (int j = i + 1; j < student_count; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
// 按学号查找
void search_by_num() {
char num[10];
printf("请输入学生学号:\n");
scanf("%s", num);
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].num, num) == 0) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%s\t%s\t%s\t%d-%d-%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",
students[i].num, students[i].name, students[i].sex,
students[i].year, students[i].month, students[i].day,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].avg_score);
return;
}
}
printf("未找到该学生!\n");
}
// 按姓名查找
void search_by_name() {
char name[20];
printf("请输入学生姓名:\n");
scanf("%s", name);
for (int i = 0; i < student_count; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%s\t%s\t%s\t%d-%d-%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",
students[i].num, students[i].name, students[i].sex,
students[i].year, students[i].month, students[i].day,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].avg_score);
return;
}
}
printf("未找到该学生!\n");
}
// 按分数查找
void search_by_score() {
float score;
printf("请输入学生成绩:\n");
scanf("%f", &score);
for (int i = 0; i < student_count; i++) {
if (students[i].total_score == score) {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大学物理\t总分\t平均分\n");
printf("%s\t%s\t%s\t%d-%d-%d\t%.1f\t%.1f\t%.1f\t%.1f\t%.1f\n",
students[i].num, students[i].name, students[i].sex,
students[i].year, students[i].month, students[i].day,
students[i].score[0], students[i].score[1], students[i].score[2],
students[i].total_score, students[i].avg_score);
}
}
printf("未找到该成绩!\n");
}
// 计算各门课的平均成绩
void calculate_avg_score() {
float avg_score[3] = {0.0, 0.0, 0.0};
for (int i = 0; i < student_count; i++) {
avg_score[0] += students[i].score[0];
avg_score[1] += students[i].score[1];
avg_score[2] += students[i].score[2];
}
avg_score[0] /= student_count;
avg_score[1] /= student_count;
avg_score[2] /= student_count;
printf("高数平均成绩:%.1f\nC语言平均成绩:%.1f\n大学物理平均成绩:%.1f\n",
avg_score[0], avg_score[1], avg_score[2]);
}
// 保存学生信息到文件
void save_students() {
FILE *fp = fopen("student.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
exit(1);
}
for (int i = 0; i < student_count; i++) {
fprintf(fp, "%s %s %s %d-%d-%d %.1f %.1f %.1f\n",
students[i].num, students[i].name, students[i].sex,
students[i].year, students[i].month, students[i].day,
students[i].score[0], students[i].score[1], students[i].score[2]);
}
fclose(fp);
}
// 主函数
int main() {
build_students();
int choice;
do {
printf("\n1. 显示所有学生信息\n");
printf("2. 添加学生信息\n");
printf("3. 按学号递增排序\n");
printf("4. 按总分递减排序\n");
printf("5. 按学号查找\n");
printf("6. 按姓名查找\n");
printf("7. 按分数查找\n");
printf("8. 计算各门课平均分数\n");
printf("9. 保存并退出\n");
printf("请输入您的选择(1-9):\n");
scanf("%d", &choice);
switch (choice) {
case 1:
show_students();
break;
case 2:
add_student();
break;
case 3:
sort_by_num();
break;
case 4:
sort_by_score();
break;
case 5:
search_by_num();
break;
case 6:
search_by_name();
break;
case 7:
search_by_score();
break;
case 8:
calculate_avg_score();
break;
case 9:
save_students();
printf("已保存并退出!\n");
break;
default:
printf("输入无效,请重新输入!\n");
break;
}
} while (choice != 9);
return 0;
}
```
这个程序能够从文件中读取学生成绩信息,可以添加、排序、查找、计算平均分数,并能将最终结果保存到文件中。用户可以通过简单的界面进行操作。
阅读全文