定义结构体类型: 每个同学的信息:学号、姓名(英文、无重名)、性别(英文)、三门课程成绩(语文、数学、英语功能要求: 运行后,显示如下信息,提示输入数字,并运行数字所对应的功能,如:按1,执行“显示所有同学的信息”;按6,执行“删除一个同学”。1.删除指定姓名的信息 2.显示所有不及格的同学,0.退出
时间: 2024-03-17 15:46:31 浏览: 122
好的,以下是定义结构体类型的C语言代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct Student {
int id;
char name[20];
char gender[10];
int scores[3];
};
struct Student students[MAX_STUDENTS];
int num_students = 0;
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct Student s;
printf("Enter student ID: ");
scanf("%d", &s.id);
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student gender: ");
scanf("%s", s.gender);
printf("Enter Chinese score: ");
scanf("%d", &s.scores[0]);
printf("Enter Math score: ");
scanf("%d", &s.scores[1]);
printf("Enter English score: ");
scanf("%d", &s.scores[2]);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, s.name) == 0) {
printf("Error: student name already exists\n");
return;
}
}
students[num_students++] = s;
printf("Student added successfully\n");
}
void remove_student() {
char name[20];
printf("Enter student name: ");
scanf("%s", name);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].name, name) == 0) {
for (int j = i + 1; j < num_students; j++) {
students[j - 1] = students[j];
}
num_students--;
printf("Student removed successfully\n");
return;
}
}
printf("Error: student not found\n");
}
void show_all_students() {
for (int i = 0; i < num_students; i++) {
printf("ID: %d, Name: %s, Gender: %s, Scores: %d %d %d\n",
students[i].id, students[i].name, students[i].gender,
students[i].scores[0], students[i].scores[1], students[i].scores[2]);
}
}
void show_failed_students() {
for (int i = 0; i < num_students; i++) {
int failed = 0;
for (int j = 0; j < 3; j++) {
if (students[i].scores[j] < 60) {
failed = 1;
break;
}
}
if (failed) {
printf("ID: %d, Name: %s, Gender: %s, Scores: %d %d %d\n",
students[i].id, students[i].name, students[i].gender,
students[i].scores[0], students[i].scores[1], students[i].scores[2]);
}
}
}
int main() {
int choice;
do {
printf("1. Add student\n");
printf("2. Remove student\n");
printf("3. Show all students\n");
printf("4. Show failed students\n");
printf("0. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
remove_student();
break;
case 3:
show_all_students();
break;
case 4:
show_failed_students();
break;
case 0:
break;
default:
printf("Error: invalid choice\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个程序定义了一个 `Student` 结构体类型,包含了学号、姓名、性别和三门课程成绩。程序提供了四个功能:添加学生、删除学生、显示所有学生和显示不及格的学生。用户可以根据提示输入数字,选择要执行的功能。
阅读全文