用c语言定义一个学生结构体,结构体包括学号和5个分数:语文,数学,英语,物理,化学,再定义一个50个结构体的数组,代表50个学生。 在操作中尽量传递指针,用指针操作。 1、用一个samples中的菜单定义各个操作; 2、操作1:输入一个结构体中的学生数据; 3、操作2:输出一个结构体中的学生数据; 4、操作3:计算输出每个学生的平均成绩,并根据平均成绩排序; 5、操作4:计算输出每一个科目的平均成绩、最高成绩,最低成绩,不及格人数,及格率; 6、操作5:计算输出不及格人员列表,不及格门次数,和各不及格科目分数;
时间: 2024-04-21 21:30:00 浏览: 125
好的,以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#define NUM_OF_SUBJECTS 5 // 科目数量
#define NUM_OF_STUDENTS 50 // 学生人数
// 学生结构体
struct Student {
int id; // 学号
int scores[NUM_OF_SUBJECTS]; // 分数数组
float avg_score; // 平均分
};
// 计算每个学生的平均成绩
void calculate_avg_score(struct Student *student) {
float sum = 0;
for (int i = 0; i < NUM_OF_SUBJECTS; i++) {
sum += student->scores[i];
}
student->avg_score = sum / NUM_OF_SUBJECTS;
}
// 操作1:输入一个结构体中的学生数据
void input_student_data(struct Student *student) {
printf("请输入学号:");
scanf("%d", &student->id);
printf("请输入成绩:\n");
for (int i = 0; i < NUM_OF_SUBJECTS; i++) {
printf("请输入%s成绩:", SUBJECTS[i]);
scanf("%d", &student->scores[i]);
}
}
// 操作2:输出一个结构体中的学生数据
void output_student_data(struct Student *student) {
printf("学号:%d,成绩:", student->id);
for (int i = 0; i < NUM_OF_SUBJECTS; i++) {
printf("%d ", student->scores[i]);
}
printf("\n");
}
// 操作3:计算输出每个学生的平均成绩,并根据平均成绩排序
void sort_by_avg_score(struct Student *students) {
// 计算每个学生的平均成绩
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
calculate_avg_score(&students[i]);
}
// 冒泡排序
for (int i = 0; i < NUM_OF_STUDENTS - 1; i++) {
for (int j = 0; j < NUM_OF_STUDENTS - i - 1; j++) {
if (students[j].avg_score > students[j + 1].avg_score) {
struct Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 输出排序结果
printf("每个学生的平均成绩(从低到高排序):\n");
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
printf("学号:%d,平均成绩:%.1f\n", students[i].id, students[i].avg_score);
}
}
// 操作4:计算输出每一个科目的平均成绩、最高成绩,最低成绩,不及格人数,及格率
void calculate_subject_statistics(struct Student *students) {
int total_scores[NUM_OF_SUBJECTS] = {0}; // 每个科目的总分
int max_scores[NUM_OF_SUBJECTS] = {0}; // 每个科目的最高分
int min_scores[NUM_OF_SUBJECTS] = {100}; // 每个科目的最低分
int num_of_failures[NUM_OF_SUBJECTS] = {0}; // 每个科目的不及格人数
float passing_rates[NUM_OF_SUBJECTS] = {0}; // 每个科目的及格率
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
for (int j = 0; j < NUM_OF_SUBJECTS; j++) {
// 计算每个科目的总分
total_scores[j] += students[i].scores[j];
// 计算每个科目的最高分
if (students[i].scores[j] > max_scores[j]) {
max_scores[j] = students[i].scores[j];
}
// 计算每个科目的最低分
if (students[i].scores[j] < min_scores[j]) {
min_scores[j] = students[i].scores[j];
}
// 计算每个科目的不及格人数
if (students[i].scores[j] < 60) {
num_of_failures[j]++;
}
}
}
// 计算每个科目的平均分和及格率
for (int i = 0; i < NUM_OF_SUBJECTS; i++) {
float avg_score = total_scores[i] / (float)NUM_OF_STUDENTS;
passing_rates[i] = (NUM_OF_STUDENTS - num_of_failures[i]) / (float)NUM_OF_STUDENTS;
printf("%s平均成绩:%.1f,最高成绩:%d,最低成绩:%d,不及格人数:%d,及格率:%.2f\n",
SUBJECTS[i], avg_score, max_scores[i], min_scores[i], num_of_failures[i], passing_rates[i]);
}
}
// 操作5:计算输出不及格人员列表,不及格门次数,和各不及格科目分数
void calculate_failure_statistics(struct Student *students) {
printf("不及格人员列表:\n");
int num_of_failures[NUM_OF_SUBJECTS] = {0}; // 每个科目的不及格门数
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
int num_of_failures_for_student = 0; // 当前学生的不及格门数
printf("学号:%d,不及格科目:", students[i].id);
for (int j = 0; j < NUM_OF_SUBJECTS; j++) {
if (students[i].scores[j] < 60) {
num_of_failures[j]++;
num_of_failures_for_student++;
printf("%s ", SUBJECTS[j]);
}
}
printf(",不及格门数:%d\n", num_of_failures_for_student);
}
printf("每个科目的不及格门数:\n");
for (int i = 0; i < NUM_OF_SUBJECTS; i++) {
printf("%s不及格门数:%d\n", SUBJECTS[i], num_of_failures[i]);
}
}
int main() {
struct Student students[NUM_OF_STUDENTS]; // 学生数组
int choice;
while (1) {
printf("\n");
printf("********************************\n");
printf("* 1. 输入学生数据 *\n");
printf("* 2. 输出学生数据 *\n");
printf("* 3. 排序输出学生数据 *\n");
printf("* 4. 科目统计 *\n");
printf("* 5. 不及格统计 *\n");
printf("* 0. 退出程序 *\n");
printf("********************************\n");
printf("请输入操作选项:");
scanf("%d", &choice);
switch (choice) {
case 0:
exit(0);
case 1:
printf("请输入学生数据:\n");
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
input_student_data(&students[i]);
}
break;
case 2:
printf("学生数据如下:\n");
for (int i = 0; i < NUM_OF_STUDENTS; i++) {
output_student_data(&students[i]);
}
break;
case 3:
sort_by_avg_score(students);
break;
case 4:
calculate_subject_statistics(students);
break;
case 5:
calculate_failure_statistics(students);
break;
default:
printf("无效的选项!\n");
}
}
return 0;
}
```
这里涉及到一些宏定义和数组,需要先定义一些常量:
```c
#define NUM_OF_SUBJECTS 5 // 科目数量
#define NUM_OF_STUDENTS 50 // 学生人数
const char *SUBJECTS[NUM_OF_SUBJECTS] = {"语文", "数学", "英语", "物理", "化学"};
```
运行程序后,根据菜单选择操作即可。
阅读全文