用c语言写出对一个有N个学生的班级,每个学生有3门课程,分别为语文,数学,语言。该系统实现对班级成绩的录入、显示,统计等操作的管理。功能要求: (1)本系统采用一个结构体数组,每个数据的结构应当包括:学号、姓名、M门课程名称。 (2)本系统显示这样的菜单: 系统功能选项: a、 数据导入及显示 b、新记录输入 c、 成绩统计 1)显示每门课程成绩最高的学生的基本信息 2)显示每门课程的平均成绩 3)显示超过某门课程平均成绩的学生人数 g、 退出系统 注意:需保证学号唯一性 (该题目用或数组完成)
时间: 2024-03-18 09:42:11 浏览: 85
20位学生有3门课程分别是高数-大英-C语言的考试成绩-求每门课的总分和平均分.pdf
好的,这是一个较为复杂的问题,需要比较长的一段代码来实现。请注意,以下代码仅供参考。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT 100
struct Student {
int id;
char name[20];
int score[3]; // 0: Chinese, 1: Math, 2: English
};
struct Student students[MAX_STUDENT];
int student_count = 0;
int find_student(int id) {
for (int i = 0; i < student_count; i++) {
if (students[i].id == id) {
return i;
}
}
return -1;
}
void import_data() {
printf("Please enter the number of students: ");
scanf("%d", &student_count);
for (int i = 0; i < student_count; i++) {
printf("Student #%d\n", i + 1);
printf("ID: ");
scanf("%d", &students[i].id);
if (find_student(students[i].id) != -1) {
printf("Error: duplicate student ID\n");
i--;
continue;
}
printf("Name: ");
scanf("%s", students[i].name);
printf("Chinese score: ");
scanf("%d", &students[i].score[0]);
printf("Math score: ");
scanf("%d", &students[i].score[1]);
printf("English score: ");
scanf("%d", &students[i].score[2]);
}
}
void show_data() {
printf("%-10s%-20s%-10s%-10s%-10s\n", "ID", "Name", "Chinese", "Math", "English");
for (int i = 0; i < student_count; i++) {
printf("%-10d%-20s%-10d%-10d%-10d\n", students[i].id, students[i].name,
students[i].score[0], students[i].score[1], students[i].score[2]);
}
}
void add_student() {
printf("Please enter the information of the new student:\n");
printf("ID: ");
int id;
scanf("%d", &id);
if (find_student(id) != -1) {
printf("Error: duplicate student ID\n");
return;
}
strcpy(students[student_count].name, "");
students[student_count].id = id;
printf("Name: ");
scanf("%s", students[student_count].name);
printf("Chinese score: ");
scanf("%d", &students[student_count].score[0]);
printf("Math score: ");
scanf("%d", &students[student_count].score[1]);
printf("English score: ");
scanf("%d", &students[student_count].score[2]);
student_count++;
}
void print_highest(int subject) {
int highest_score = -1;
int highest_index = -1;
for (int i = 0; i < student_count; i++) {
if (students[i].score[subject] > highest_score) {
highest_score = students[i].score[subject];
highest_index = i;
}
}
printf("%-10s%-20s%-10d\n", "ID", "Name", highest_score);
printf("%-10d%-20s%-10d\n", students[highest_index].id, students[highest_index].name,
students[highest_index].score[subject]);
}
void print_average(int subject) {
int sum = 0;
for (int i = 0; i < student_count; i++) {
sum += students[i].score[subject];
}
double average = (double)sum / student_count;
printf("The average score of %s is %.2lf\n",
subject == 0 ? "Chinese" : subject == 1 ? "Math" : "English", average);
}
void count_above_average(int subject) {
int count = 0;
int sum = 0;
for (int i = 0; i < student_count; i++) {
sum += students[i].score[subject];
}
double average = (double)sum / student_count;
for (int i = 0; i < student_count; i++) {
if (students[i].score[subject] > average) {
count++;
}
}
printf("%d students got above-average score in %s\n", count,
subject == 0 ? "Chinese" : subject == 1 ? "Math" : "English");
}
int main() {
char command;
do {
printf("System options:\n");
printf("a. Import data and show\n");
printf("b. Add new record\n");
printf("c. Show statistics\n");
printf("g. Quit\n");
printf("Please enter your command: ");
scanf(" %c", &command);
switch (command) {
case 'a':
import_data();
show_data();
break;
case 'b':
add_student();
show_data();
break;
case 'c': {
int sub_command;
do {
printf("Statistics:\n");
printf("1. Show the highest score of each subject\n");
printf("2. Show the average score of each subject\n");
printf("3. Count the number of students who got above-average score\n");
printf("Please enter your command: ");
scanf("%d", &sub_command);
switch (sub_command) {
case 1:
printf("%-10s%-20s%-10s\n", "Subject", "Name", "Score");
print_highest(0);
print_highest(1);
print_highest(2);
break;
case 2:
print_average(0);
print_average(1);
print_average(2);
break;
case 3:
count_above_average(0);
count_above_average(1);
count_above_average(2);
break;
default:
printf("Invalid command\n");
break;
}
} while (sub_command != 1 && sub_command != 2 && sub_command != 3);
break;
}
case 'g':
printf("Goodbye!\n");
break;
default:
printf("Invalid command\n");
break;
}
} while (command != 'g');
return 0;
}
```
这段代码使用了一个 `struct` 结构体数组来存储学生信息,其中包括学号、姓名、三门课程的成绩。菜单选项包括数据导入及显示、新记录输入、成绩统计等。具体的实现细节可以自行参考代码注释。
阅读全文