C语言编程小白,求解答

现有N门课程M个班级的学生成绩信息需要存储和处理。其中,学生信息包含学号、姓名、各科成绩等信息。使用分治思维对问题进行设计,建议使用菜单界面进行功能布局
至少完成以下功能:
(1)从文件中读取学生的学号,姓名及成绩等信息:(2)计算各学生成绩的平均分和总分:(3)统计各分数段的学生人数及所占的百分比并输出:(4)将N个学生的学号、姓名及M门课程的成绩、总分、平均分写到文件存储。

以下是使用分治思维进行设计的C语言代码,实现了题目中要求的4个功能,并使用菜单界面进行功能布局:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体
typedef struct {
char id[20]; // 学号
char name[20]; // 姓名
float score[10]; // 课程分数
float total; // 总分
float average; // 平均分
} Student;
// 全局变量
Student students[1000]; // 学生数组
int totalStudents = 0; // 学生总数
int totalClasses = 0; // 课程总数
char classNames[10][20]; // 课程名称数组
// ========== 帮助函数 ==========
// 显示菜单
void showMenu() {
printf("\n");
printf("==============================\n");
printf(" 1. 从文件中读取学生信息\n");
printf(" 2. 计算各学生成绩的总分和平均分\n");
printf(" 3. 统计各分数段的学生人数并输出\n");
printf(" 4. 将学生信息及各学科成绩输出到文件\n");
printf(" 0. 退出程序\n");
printf("==============================\n");
printf("请选择操作:\n");
}
// 读取一行
char *readLine(char *buffer, int size) {
int ch, pos = 0;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (pos < size - 1) {
buffer[pos++] = ch;
}
}
buffer[pos] = '\0';
return buffer;
}
// ========== 主要功能函数 ==========
// 从文件中读取学生信息
void loadStudents() {
char filename[100];
printf("请输入数据文件名:\n");
readLine(filename, 100);
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("无法打开文件 %s\n", filename);
return;
}
int lineNum = 0;
char buffer[1024];
while (fgets(buffer, 1024, file)) {
lineNum++;
if (lineNum == 1) {
// 解析课程名称
char *token = strtok(buffer, ",");
while (token) {
strcpy(classNames[totalClasses++], token);
token = strtok(NULL, ",");
}
} else {
// 解析学生信息
char *token = strtok(buffer, ",");
strcpy(students[totalStudents].id, token);
token = strtok(NULL, ",");
strcpy(students[totalStudents].name, token);
for (int i = 0; i < totalClasses; i++) {
token = strtok(NULL, ",");
students[totalStudents].score[i] = atof(token);
students[totalStudents].total += students[totalStudents].score[i];
}
students[totalStudents].average = students[totalStudents].total / totalClasses;
totalStudents++;
}
}
fclose(file);
printf("成功读取 %d 名学生信息\n", totalStudents);
}
// 计算各学生成绩的总分和平均分
void calculateScore() {
float classTotal[10] = {0};
float classAverage[10] = {0};
float totalTotal = 0;
for (int i = 0; i < totalStudents; i++) {
for (int j = 0; j < totalClasses; j++) {
classTotal[j] += students[i].score[j];
}
totalTotal += students[i].total;
}
for (int i = 0; i < totalClasses; i++) {
classAverage[i] = classTotal[i] / totalStudents;
printf("课程 %s 总分为 %.2f,平均分为 %.2f\n", classNames[i], classTotal[i], classAverage[i]);
}
printf("总分为 %.2f,平均分为 %.2f\n", totalTotal, totalTotal / (totalStudents * totalClasses));
}
// 统计各分数段的学生人数并输出
void calculateDistribution() {
float distribution[21] = {0}; // 成绩分布,每个元素存储一个分数段的学生人数
for (int i = 0; i < totalStudents; i++) {
int score = (int) (students[i].total / totalClasses / 5); // 计算分数段
distribution[score]++;
}
printf("各分数段学生人数及百分比如下:\n");
for (int i = 0; i < 20; i++) {
printf("[%d-%d]:%.2f%%\n", i * 5, i * 5 + 4, distribution[i] / totalStudents * 100);
}
printf("100:%.2f%%\n", distribution[20] / totalStudents * 100);
}
// 将学生信息及各学科成绩输出到文件
void saveStudents() {
char filename[100];
printf("请输入要存储的文件名:\n");
readLine(filename, 100);
FILE *file = fopen(filename, "w");
if (file == NULL) {
printf("无法打开文件 %s\n", filename);
return;
}
// 输出表头
fprintf(file, "学号,姓名");
for (int i = 0; i < totalClasses; i++) {
fprintf(file, ",%s", classNames[i]);
}
fprintf(file, ",总分,平均分\n");
// 输出学生信息
for (int i = 0; i < totalStudents; i++) {
fprintf(file, "%s,%s", students[i].id, students[i].name);
for (int j = 0; j < totalClasses; j++) {
fprintf(file, ",%.2f", students[i].score[j]);
}
fprintf(file, ",%.2f,%.2f\n", students[i].total, students[i].average);
}
fclose(file);
printf("成功将学生信息保存到文件 %s\n", filename);
}
// ========== 程序入口 ==========
int main() {
while (1) {
showMenu();
char command[10];
readLine(command, 10);
if (strcmp(command, "0") == 0) {
break;
} else if (strcmp(command, "1") == 0) {
loadStudents();
} else if (strcmp(command, "2") == 0) {
calculateScore();
} else if (strcmp(command, "3") == 0) {
calculateDistribution();
} else if (strcmp(command, "4") == 0) {
saveStudents();
} else {
printf("无效命令!\n");
}
}
return 0;
}
通过运行上述代码,用户可以在菜单中选择需要执行的功能,并按照程序提示进行操作。其中,(1)从文件中读取学生的学号,姓名及成绩等信息,需要用户提供数据文件名;(4)将N个学生的学号、姓名及M门课程的成绩、总分、平均分写到文件存储,需要用户提供存放文件名。
温馨提示:答案为网友推荐,仅供参考