用c语言写实现学生信息的录入、修改、插入、删除、查询、计算总评成绩、根据总评程序排序和划分等级、按学号查询学生成绩等。 (1)从文件输入学生的成绩信息,包含学号、姓名、期末成绩和平时成绩; (2)显示全部学生成绩信息; (3)更新某个学生的基本信息或课程成绩; (4)对成绩统计分析(总分、平均分、最高分、最低分、及格率等); (5)按学号查找并显示某个学生的成绩; (6)按姓名查找并显示某个学生的成绩; (7)按总评成绩由高到低排序显示; (8)统计各分数段人数并显示; (9)设计一个菜单,具有上述规定的操作要求、退出系统等最基本的功能。
时间: 2023-06-19 13:08:07 浏览: 191
这是一个比较完整的学生成绩管理系统,可以按照以下步骤进行实现:
1. 定义学生信息结构体,包括学号、姓名、期末成绩和平时成绩等字段。
```c
typedef struct {
int id; // 学号
char name[20]; // 姓名
float final_score; // 期末成绩
float daily_score; // 平时成绩
float total_score; // 总评成绩
} Student;
```
2. 定义存储学生信息的数组,并从文件中读取学生信息初始化数组。
```c
#define MAX_STUDENT_NUM 100 // 最大学生数
Student students[MAX_STUDENT_NUM]; // 学生数组
int student_num = 0; // 实际学生数
// 从文件中读取学生信息初始化数组
void load_students(const char* filename) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Failed to open file %s\n", filename);
return;
}
while (fscanf(fp, "%d %s %f %f\n", &students[student_num].id, students[student_num].name,
&students[student_num].final_score, &students[student_num].daily_score) != EOF) {
students[student_num].total_score = students[student_num].final_score * 0.6 + students[student_num].daily_score * 0.4;
student_num++;
}
fclose(fp);
}
```
3. 实现菜单功能,包括录入、修改、插入、删除、查询、计算总评成绩、根据总评成绩排序和划分等级、按学号查询学生成绩等操作。
```c
// 显示菜单
void show_menu() {
printf("1. 显示全部学生成绩\n");
printf("2. 更新某个学生的基本信息或课程成绩\n");
printf("3. 对成绩统计分析\n");
printf("4. 按学号查找并显示某个学生的成绩\n");
printf("5. 按姓名查找并显示某个学生的成绩\n");
printf("6. 按总评成绩由高到低排序显示\n");
printf("7. 统计各分数段人数并显示\n");
printf("0. 退出系统\n");
}
// 录入学生成绩
void add_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数已达到最大值,无法继续添加\n");
return;
}
printf("请输入学生信息:\n");
printf("学号:");
scanf("%d", &students[student_num].id);
printf("姓名:");
scanf("%s", students[student_num].name);
printf("期末成绩:");
scanf("%f", &students[student_num].final_score);
printf("平时成绩:");
scanf("%f", &students[student_num].daily_score);
students[student_num].total_score = students[student_num].final_score * 0.6 + students[student_num].daily_score * 0.4;
student_num++;
printf("添加成功!\n");
}
// 修改学生成绩
void update_student() {
int id;
printf("请输入要修改的学生学号:");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("请输入新的学生信息:\n");
printf("学号:");
scanf("%d", &students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("期末成绩:");
scanf("%f", &students[i].final_score);
printf("平时成绩:");
scanf("%f", &students[i].daily_score);
students[i].total_score = students[i].final_score * 0.6 + students[i].daily_score * 0.4;
printf("修改成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
// 插入学生成绩
void insert_student() {
if (student_num >= MAX_STUDENT_NUM) {
printf("学生数已达到最大值,无法继续添加\n");
return;
}
int id, pos;
printf("请输入要插入的学生信息:\n");
printf("学号:");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("学号 %d 已存在,无法插入\n", id);
return;
}
else if (students[i].id > id) {
pos = i;
break;
}
}
if (i == student_num) {
pos = student_num;
}
printf("姓名:");
scanf("%s", students[pos].name);
printf("期末成绩:");
scanf("%f", &students[pos].final_score);
printf("平时成绩:");
scanf("%f", &students[pos].daily_score);
students[pos].id = id;
students[pos].total_score = students[pos].final_score * 0.6 + students[pos].daily_score * 0.4;
student_num++;
printf("插入成功!\n");
}
// 删除学生成绩
void delete_student() {
int id;
printf("请输入要删除的学生学号:");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
int j;
for (j = i; j < student_num - 1; j++) {
students[j] = students[j + 1];
}
student_num--;
printf("删除成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
// 显示全部学生成绩
void show_students() {
printf("学号\t姓名\t期末成绩\t平时成绩\t总评成绩\n");
int i;
for (i = 0; i < student_num; i++) {
printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\n", students[i].id, students[i].name,
students[i].final_score, students[i].daily_score, students[i].total_score);
}
}
// 按学号查找学生成绩
void find_by_id() {
int id;
printf("请输入要查找的学生学号:");
scanf("%d", &id);
int i;
for (i = 0; i < student_num; i++) {
if (students[i].id == id) {
printf("学号\t姓名\t期末成绩\t平时成绩\t总评成绩\n");
printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\n", students[i].id, students[i].name,
students[i].final_score, students[i].daily_score, students[i].total_score);
return;
}
}
printf("未找到该学生!\n");
}
// 按姓名查找学生成绩
void find_by_name() {
char name[20];
printf("请输入要查找的学生姓名:");
scanf("%s", name);
int i;
for (i = 0; i < student_num; i++) {
if (strcmp(students[i].name, name) == 0) {
printf("学号\t姓名\t期末成绩\t平时成绩\t总评成绩\n");
printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\n", students[i].id, students[i].name,
students[i].final_score, students[i].daily_score, students[i].total_score);
return;
}
}
printf("未找到该学生!\n");
}
// 按总评成绩排序并显示
void sort_by_total_score() {
int i, j;
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) {
Student tmp = students[j];
students[j] = students[j + 1];
students[j + 1] = tmp;
}
}
}
printf("学号\t姓名\t期末成绩\t平时成绩\t总评成绩\n");
for (i = 0; i < student_num; i++) {
printf("%d\t%s\t%.2f\t\t%.2f\t\t%.2f\n", students[i].id, students[i].name,
students[i].final_score, students[i].daily_score, students[i].total_score);
}
}
// 划分各分数段人数并显示
void show_score_distribution() {
int score_count[10] = {0}; // 各分数段人数计数器
int i;
for (i = 0; i < student_num; i++) {
int score = (int) students[i].total_score / 10;
if (score >= 0 && score < 10) {
score_count[score]++;
}
}
printf("分数段\t人数\n");
for (i = 0; i < 10; i++) {
printf("%d-%d\t%d\n", i * 10, i * 10 + 9, score_count[i]);
}
}
// 对成绩进行统计分析
void analyze_scores() {
float total_score_sum = 0.0; // 总分
float avg_score = 0.0; // 平均分
float max_score = 0.0; // 最高分
float min_score = 100.0; // 最低分
int pass_num = 0; // 及格人数
int i;
for (i = 0; i < student_num; i++) {
total_score_sum += students[i].total_score;
if (students[i].total_score > max_score) {
max_score = students[i].total_score;
}
if (students[i].total_score < min_score) {
min_score = students[i].total_score;
}
if (students[i].total_score >= 60) {
pass_num++;
}
}
avg_score = total_score_sum / student_num;
printf("总分:%f\n", total_score_sum);
printf("平均分:%f\n", avg_score);
printf("最高分:%f\n", max_score);
printf("最低分:%f\n", min_score);
printf("及格率:%f%%\n", (float) pass_num / student_num * 100);
}
// 主函数
int main() {
load_students("students.txt");
while (1) {
show_menu();
int choice;
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
show_students();
break;
case 2:
update_student();
break;
case 3:
analyze_scores();
break;
case 4:
find_by_id();
break;
case 5:
find_by_name();
break;
case 6:
sort_by_total_score();
break;
case 7:
show_score_distribution();
break;
case 0:
return 0;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
}
```
4. 编写主函数,完成整个程序的运行。
```c
int main() {
load_students("students.txt");
while (1) {
show_menu();
int choice;
printf("请选择操作:");
scanf("%d", &choice);
switch (choice) {
case 1:
show_students();
break;
case 2:
update_student();
break;
case 3:
analyze_scores();
break;
case 4:
find_by_id();
break;
case 5:
find_by_name();
break;
case 6:
sort_by_total_score();
break;
case 7:
show_score_distribution();
break;
case 0:
return 0;
default:
printf("无效的选择,请重新输入!\n");
break;
}
}
}
```
以上就是一个简单的学生成绩管理系统的实现,可以根据实际需要进行扩展和优化。
阅读全文