设计并实现一个成绩分析系统,能够实现录入、保存一个班级学生多门课程的成绩、并对成绩进行分析等功能.具体要求如下: (1)定义一个菜单,方便用户实现下述操作.要求菜单筒沾、易操作、界而美观• (2)建立破盘文件input. dat,用于保存学生及其成绩信息.惮细内容参见表1M. (3)读取磁盘文件input. dat中的数据,并进行处理.要求实现如下功能: •按各门课程成绩排序,将排序的结果保存到磁盘文件中. •计算每人的平均成绩,按平均成绩排序,将排序的结果保存到破盘文件中. •能够统计各门课程的平均成绩、最高分、最低分、不及格人数、60〜69分人数、70〜 79分人数、80〜89分人数、90分以上人数. •根据学生姓名或学号查询其各门课成绩,需考虑重名情况的处理. 用C语言实现
时间: 2024-03-07 11:54:04 浏览: 75
好的,我理解了你的要求。下面是一个简单的成绩分析系统的C语言实现,根据你的要求进行了设计和开发。
首先是程序的主要代码部分:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_RECORDS 100
#define MAX_COURSES 5
typedef struct {
char name[MAX_NAME_LEN];
int id;
int scores[MAX_COURSES];
float avg_score;
} student_t;
typedef struct {
char name[MAX_NAME_LEN];
float avg_score;
float max_score;
float min_score;
int fail_num;
int sixty_num;
int seventy_num;
int eighty_num;
int ninety_num;
} course_t;
student_t records[MAX_RECORDS];
int record_count = 0;
course_t courses[MAX_COURSES];
int course_count = 0;
void display_menu();
void add_record();
void save_records();
void load_records();
void sort_scores();
void sort_avg_scores();
void analyze_scores();
void query_scores();
int main() {
load_records();
display_menu();
return 0;
}
void display_menu() {
int choice = 0;
do {
printf("\nPlease choose an option:\n");
printf("1. Add record\n");
printf("2. Save records\n");
printf("3. Load records\n");
printf("4. Sort scores\n");
printf("5. Sort average scores\n");
printf("6. Analyze scores\n");
printf("7. Query scores\n");
printf("8. Exit\n");
printf("Enter your choice (1-8): ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_record();
break;
case 2:
save_records();
break;
case 3:
load_records();
break;
case 4:
sort_scores();
break;
case 5:
sort_avg_scores();
break;
case 6:
analyze_scores();
break;
case 7:
query_scores();
break;
case 8:
printf("\nGoodbye!\n");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
} while (choice != 8);
}
void add_record() {
if (record_count == MAX_RECORDS) {
printf("\nError: Maximum number of records reached.\n");
return;
}
student_t new_record;
printf("\nEnter student name: ");
scanf("%s", new_record.name);
printf("Enter student ID: ");
scanf("%d", &new_record.id);
for (int i = 0; i < course_count; i++) {
printf("Enter score for course %s: ", courses[i].name);
scanf("%d", &new_record.scores[i]);
}
records[record_count++] = new_record;
printf("\nRecord added successfully.\n");
}
void save_records() {
FILE *fp = fopen("input.dat", "wb");
if (fp == NULL) {
printf("\nError: Failed to open file.\n");
return;
}
fwrite(&record_count, sizeof(record_count), 1, fp);
fwrite(records, sizeof(student_t), record_count, fp);
fclose(fp);
printf("\nRecords saved successfully.\n");
}
void load_records() {
FILE *fp = fopen("input.dat", "rb");
if (fp == NULL) {
printf("\nError: Failed to open file.\n");
return;
}
fread(&record_count, sizeof(record_count), 1, fp);
fread(records, sizeof(student_t), record_count, fp);
fclose(fp);
printf("\nRecords loaded successfully.\n");
}
void sort_scores() {
int course_index = 0;
printf("\nEnter course index (0-%d): ", course_count - 1);
scanf("%d", &course_index);
if (course_index < 0 || course_index >= course_count) {
printf("\nError: Invalid course index.\n");
return;
}
for (int i = 0; i < record_count - 1; i++) {
for (int j = i + 1; j < record_count; j++) {
if (records[i].scores[course_index] < records[j].scores[course_index]) {
student_t temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
}
save_records();
printf("\nScores sorted by course %s.\n", courses[course_index].name);
}
void sort_avg_scores() {
for (int i = 0; i < record_count; i++) {
float total_score = 0;
for (int j = 0; j < course_count; j++) {
total_score += records[i].scores[j];
}
records[i].avg_score = total_score / course_count;
}
for (int i = 0; i < record_count - 1; i++) {
for (int j = i + 1; j < record_count; j++) {
if (records[i].avg_score < records[j].avg_score) {
student_t temp = records[i];
records[i] = records[j];
records[j] = temp;
}
}
}
save_records();
printf("\nRecords sorted by average score.\n");
}
void analyze_scores() {
for (int i = 0; i < course_count; i++) {
float total_score = 0;
float max_score = 0;
float min_score = 100;
int fail_num = 0;
int sixty_num = 0;
int seventy_num = 0;
int eighty_num = 0;
int ninety_num = 0;
for (int j = 0; j < record_count; j++) {
total_score += records[j].scores[i];
if (records[j].scores[i] > max_score) {
max_score = records[j].scores[i];
}
if (records[j].scores[i] < min_score) {
min_score = records[j].scores[i];
}
if (records[j].scores[i] < 60) {
fail_num++;
} else if (records[j].scores[i] >= 60 && records[j].scores[i] < 70) {
sixty_num++;
} else if (records[j].scores[i] >= 70 && records[j].scores[i] < 80) {
seventy_num++;
} else if (records[j].scores[i] >= 80 && records[j].scores[i] < 90) {
eighty_num++;
} else {
ninety_num++;
}
}
courses[i].avg_score = total_score / record_count;
courses[i].max_score = max_score;
courses[i].min_score = min_score;
courses[i].fail_num = fail_num;
courses[i].sixty_num = sixty_num;
courses[i].seventy_num = seventy_num;
courses[i].eighty_num = eighty_num;
courses[i].ninety_num = ninety_num;
}
printf("\nScore analysis completed.\n");
}
void query_scores() {
int choice = 0;
do {
printf("\nPlease choose a query option:\n");
printf("1. Query by name\n");
printf("2. Query by ID\n");
printf("3. Return to main menu\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: {
char name[MAX_NAME_LEN];
printf("\nEnter student name: ");
scanf("%s", name);
int found_count = 0;
for (int i = 0; i < record_count; i++) {
if (strcmp(records[i].name, name) == 0) {
found_count++;
printf("\nRecord found:\n");
printf("Name: %s\n", records[i].name);
printf("ID: %d\n", records[i].id);
for (int j = 0; j < course_count; j++) {
printf("Score for course %s: %d\n", courses[j].name, records[i].scores[j]);
}
}
}
if (found_count == 0) {
printf("\nNo record found.\n");
}
break;
}
case 2: {
int id = 0;
printf("\nEnter student ID: ");
scanf("%d", &id);
int found_count = 0;
for (int i = 0; i < record_count; i++) {
if (records[i].id == id) {
found_count++;
printf("\nRecord found:\n");
printf("Name: %s\n", records[i].name);
printf("ID: %d\n", records[i].id);
for (int j = 0; j < course_count; j++) {
printf("Score for course %s: %d\n", courses[j].name, records[i].scores[j]);
}
}
}
if (found_count == 0) {
printf("\nNo record found.\n");
}
break;
}
case 3:
printf("\nReturning to main menu.\n");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
} while (choice != 3);
}
```
上述代码中,定义了两个结构体,student_t表示学生信息,course_t表示课程信息。同时定义了两个全局数组,records存储学生信息,courses存储课程信息。主函数中首先调用load_records函数从磁盘文件中读取记录,然后显示菜单,根据用户的选择执行相应的操作。
下面是各个操作的具体实现:
- add_record: 添加一条学生记录,包括姓名、学号和多门课程的成绩。
- save_records: 将所有学生记录保存到磁盘文件input.dat中。
- load_records: 从磁盘文件input.dat中读取所有学生记录。
- sort_scores: 按指定的课程成绩从高到低排序,然后将排序结果保存到磁盘文件中。
- sort_avg_scores: 计算每个学生的平均成绩,然后按平均成绩从高到低排序,最后将排序结果保存到磁盘文件中。
- analyze_scores: 统计各门课程的平均成绩、最高分、最低分、不及格人数、60~69分人数、70~79分人数、80~89分人数、90分以上人数。
- query_scores: 根据学生姓名或学号查询其各门课成绩,需考虑重名情况的处理。
程序中使用了文件读写操作、排序算法和简单的统计分析,可以满足你的要求。不过需要注意的是,由于本程序没有进行输入数据的验证,如果输入了错误的数据可能会导致程序崩溃或者输出错误的结果。
阅读全文