C语言建立N名学生信息登记表,其中包括学号,姓名,性别及五门功课的成绩。设置文本菜单1输入N名学生数据,2显示每个学生五门功课中的最低及最高分,3显示每门功课都不及格的人数,4检索学号为指定数的学生信息
时间: 2024-06-09 18:04:28 浏览: 79
以下是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100 // 最大学生数
#define MAX_NAME_LEN 20 // 姓名最大长度
#define MAX_ID_LEN 10 // 学号最大长度
typedef struct {
char id[MAX_ID_LEN]; // 学号
char name[MAX_NAME_LEN]; // 姓名
char gender; // 性别
int scores[5]; // 五门功课成绩
} Student;
int n = 0; // 学生数量
Student students[MAX_STUDENTS]; // 学生信息数组
// 显示菜单
void show_menu() {
printf("1. 输入学生数据\n");
printf("2. 显示每个学生五门功课中的最低及最高分\n");
printf("3. 显示每门功课都不及格的人数\n");
printf("4. 检索学号为指定数的学生信息\n");
printf("0. 退出\n");
}
// 输入学生数据
void input_students() {
printf("请输入学生数量:");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i + 1);
printf("学号:");
scanf("%s", students[i].id);
printf("姓名:");
scanf("%s", students[i].name);
printf("性别:");
scanf(" %c", &students[i].gender);
printf("五门功课成绩:");
for (int j = 0; j < 5; j++) {
scanf("%d", &students[i].scores[j]);
}
}
}
// 显示每个学生五门功课中的最低及最高分
void show_min_max_scores() {
for (int i = 0; i < n; i++) {
int min_score = students[i].scores[0];
int max_score = students[i].scores[0];
for (int j = 1; j < 5; j++) {
if (students[i].scores[j] < min_score) {
min_score = students[i].scores[j];
}
if (students[i].scores[j] > max_score) {
max_score = students[i].scores[j];
}
}
printf("%s 的五门功课中最低分为 %d,最高分为 %d\n", students[i].name, min_score, max_score);
}
}
// 显示每门功课都不及格的人数
void show_failed_students_by_subject() {
int fails[5] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < 5; j++) {
if (students[i].scores[j] < 60) {
fails[j]++;
}
}
}
for (int i = 0; i < 5; i++) {
printf("第%d门功课不及格的人数为 %d\n", i + 1, fails[i]);
}
}
// 检索学号为指定数的学生信息
void search_student_by_id() {
char id[MAX_ID_LEN];
printf("请输入要检索的学号:");
scanf("%s", id);
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("学号:%s,姓名:%s,性别:%c,五门功课成绩:", students[i].id, students[i].name, students[i].gender);
for (int j = 0; j < 5; j++) {
printf("%d ", students[i].scores[j]);
}
printf("\n");
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生信息\n");
}
}
int main() {
int choice;
do {
show_menu();
printf("请选择操作(0-4):");
scanf("%d", &choice);
switch (choice) {
case 1:
input_students();
break;
case 2:
show_min_max_scores();
break;
case 3:
show_failed_students_by_subject();
break;
case 4:
search_student_by_id();
break;
case 0:
printf("程序已退出\n");
break;
default:
printf("无效操作,请重新选择\n");
}
} while (choice != 0);
return 0;
}
```
在代码中,使用了结构体来存储每个学生的信息,使用了数组来存储所有学生的信息。实现了输入学生数据、显示每个学生五门功课中的最低及最高分、显示每门功课都不及格的人数、检索学号为指定数的学生信息等功能。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)