设计一个简单的学生成绩管理系统。设有学生成绩文件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-12-14 08:35:53 浏览: 135
今天给大家分享一个C语言环境下开发的学生成绩管理系统 这个系统原理很简单:成绩的录入,输出,修改,排序,删除等等
以下是一个简单的学生成绩管理系统,其中包含了以上要求的各个功能模块:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最多存储100个学生信息
// 学生信息结构体
typedef struct {
int num; // 学号
char name[20]; // 姓名
char sex[5]; // 性别
int year, month, day; // 出生日期
float math, c, physics; // 三门课成绩
float total; // 总分
float avg; // 平均分
} Student;
Student students[MAX_STUDENTS]; // 学生信息数组
int num_students = 0; // 学生信息数量
// 从文件中读取学生信息
void read_students(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s!\n", filename);
return;
}
while (!feof(fp) && num_students < MAX_STUDENTS) {
Student *s = &students[num_students];
fscanf(fp, "%d %s %s %d %d %d %f %f %f", &s->num, s->name, s->sex, &s->year, &s->month, &s->day, &s->math, &s->c, &s->physics);
s->total = s->math + s->c + s->physics;
s->avg = s->total / 3.0;
num_students++;
}
fclose(fp);
printf("成功读取 %d 条学生信息!\n", num_students);
}
// 显示所有学生信息
void display_students() {
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大物\t总分\t平均分\n");
for (int i = 0; i < num_students; i++) {
Student *s = &students[i];
printf("%d\t%s\t%s\t%d-%02d-%02d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", s->num, s->name, s->sex, s->year, s->month, s->day, s->math, s->c, s->physics, s->total, s->avg);
}
}
// 添加一条学生信息
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("学生信息已满,无法添加!\n");
return;
}
Student *s = &students[num_students];
printf("请输入学号:");
scanf("%d", &s->num);
printf("请输入姓名:");
scanf("%s", s->name);
printf("请输入性别:");
scanf("%s", s->sex);
printf("请输入出生日期(格式:年 月 日):");
scanf("%d %d %d", &s->year, &s->month, &s->day);
printf("请输入高数成绩:");
scanf("%f", &s->math);
printf("请输入C语言成绩:");
scanf("%f", &s->c);
printf("请输入大学物理成绩:");
scanf("%f", &s->physics);
s->total = s->math + s->c + s->physics;
s->avg = s->total / 3.0;
num_students++;
printf("添加成功!\n");
}
// 按学号排序
void sort_by_num() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].num > students[j].num) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按学号排序成功!\n");
}
// 按总分排序
void sort_by_total() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total < students[j].total) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按总分排序成功!\n");
}
// 按学号查找
void find_by_num() {
int num;
printf("请输入要查找的学号:");
scanf("%d", &num);
for (int i = 0; i < num_students; i++) {
if (students[i].num == num) {
Student *s = &students[i];
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大物\t总分\t平均分\n");
printf("%d\t%s\t%s\t%d-%02d-%02d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", s->num, s->name, s->sex, s->year, s->month, s->day, s->math, s->c, s->physics, s->total, s->avg);
return;
}
}
printf("未找到该学号的学生信息!\n");
}
// 按姓名查找
void find_by_name() {
char name[20];
printf("请输入要查找的姓名:");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
Student *s = &students[i];
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大物\t总分\t平均分\n");
printf("%d\t%s\t%s\t%d-%02d-%02d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", s->num, s->name, s->sex, s->year, s->month, s->day, s->math, s->c, s->physics, s->total, s->avg);
return;
}
}
printf("未找到该姓名的学生信息!\n");
}
// 按分数查找
void find_by_score() {
float score;
printf("请输入要查找的分数:");
scanf("%f", &score);
for (int i = 0; i < num_students; i++) {
if (students[i].total == score) {
Student *s = &students[i];
printf("学号\t姓名\t性别\t出生日期\t高数\tC语言\t大物\t总分\t平均分\n");
printf("%d\t%s\t%s\t%d-%02d-%02d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", s->num, s->name, s->sex, s->year, s->month, s->day, s->math, s->c, s->physics, s->total, s->avg);
return;
}
}
printf("未找到该分数的学生信息!\n");
}
// 计算各门课的平均分
void calculate_averages() {
float math_total = 0, c_total = 0, physics_total = 0;
for (int i = 0; i < num_students; i++) {
math_total += students[i].math;
c_total += students[i].c;
physics_total += students[i].physics;
}
float math_avg = math_total / num_students;
float c_avg = c_total / num_students;
float physics_avg = physics_total / num_students;
printf("高数平均分:%.2f\n", math_avg);
printf("C语言平均分:%.2f\n", c_avg);
printf("大学物理平均分:%.2f\n", physics_avg);
}
// 保存学生信息到文件
void save_students(char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法保存文件 %s!\n", filename);
return;
}
for (int i = 0; i < num_students; i++) {
Student *s = &students[i];
fprintf(fp, "%d\t%s\t%s\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\n", s->num, s->name, s->sex, s->year, s->month, s->day, s->math, s->c, s->physics);
}
fclose(fp);
printf("成功保存 %d 条学生信息!\n", num_students);
}
// 主函数
int main() {
char filename[] = "students.txt";
read_students(filename);
int choice;
do {
printf("\n请选择操作:\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. 保存学生信息\n");
printf("0. 退出程序\n");
printf("请选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
display_students();
break;
case 2:
add_student();
break;
case 3:
sort_by_num();
break;
case 4:
sort_by_total();
break;
case 5:
find_by_num();
break;
case 6:
find_by_name();
break;
case 7:
find_by_score();
break;
case 8:
calculate_averages();
break;
case 9:
save_students(filename);
break;
case 0:
printf("程序已退出。\n");
break;
default:
printf("无效的操作!\n");
break;
}
} while (choice != 0);
return 0;
}
```
阅读全文