利用 C 语言当中的结构体知识,建立学生结构体 Stu,对应的属性有 name(姓名),id(学号),score(分数)。请为这些属性分配好合适的数据 类型,并定义一个能容纳 50 个的 Stu 结构体数组。具有添加,删除,修改,遍历学生信息以及退出系统
时间: 2023-10-06 07:05:59 浏览: 68
下面是一个示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENT_NUM 50
typedef struct {
char name[20];
int id;
float score;
} Stu;
void add_student(Stu students[], int *num) {
if (*num >= MAX_STUDENT_NUM) {
printf("The student list is already full.\n");
return;
}
printf("Please input the name, id and score of the student:\n");
scanf("%s %d %f", students[*num].name, &students[*num].id, &students[*num].score);
(*num)++;
}
void delete_student(Stu students[], int *num) {
int id;
printf("Please input the id of the student to delete:\n");
scanf("%d", &id);
for (int i = 0; i < *num; i++) {
if (students[i].id == id) {
for (int j = i; j < *num - 1; j++) {
students[j] = students[j + 1];
}
(*num)--;
printf("The student has been deleted.\n");
return;
}
}
printf("The student with id %d is not found.\n", id);
}
void modify_student(Stu students[], int num) {
int id;
printf("Please input the id of the student to modify:\n");
scanf("%d", &id);
for (int i = 0; i < num; i++) {
if (students[i].id == id) {
printf("Please input the new name, id and score of the student:\n");
scanf("%s %d %f", students[i].name, &students[i].id, &students[i].score);
printf("The student has been modified.\n");
return;
}
}
printf("The student with id %d is not found.\n", id);
}
void print_student(Stu student) {
printf("%s %d %.2f\n", student.name, student.id, student.score);
}
void list_students(Stu students[], int num) {
printf("There are %d students in the list:\n", num);
for (int i = 0; i < num; i++) {
print_student(students[i]);
}
}
int main() {
Stu students[MAX_STUDENT_NUM];
int num = 0;
int choice;
while (1) {
printf("Please choose an operation:\n");
printf("1. Add a student\n");
printf("2. Delete a student\n");
printf("3. Modify a student\n");
printf("4. List all students\n");
printf("5. Quit the system\n");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student(students, &num);
break;
case 2:
delete_student(students, &num);
break;
case 3:
modify_student(students, num);
break;
case 4:
list_students(students, num);
break;
case 5:
printf("Goodbye!\n");
return 0;
default:
printf("Invalid choice.\n");
break;
}
}
}
```
在这个代码中,我们定义了一个学生结构体 `Stu`,其中包含了三个属性:姓名(`name`)、学号(`id`)和分数(`score`)。我们还定义了一个容纳 50 个学生信息的 `Stu` 数组 `students`。
在 `main` 函数中,我们通过一个循环不断提示用户选择操作。根据用户的选择,调用相应的函数来实现添加、删除、修改、遍历学生信息和退出系统等功能。
在添加学生信息时,我们首先检查数组是否已满,如果已满则提示用户无法添加。否则,让用户输入姓名、学号和分数,并将这些信息存储到数组中。
在删除学生信息时,我们首先让用户输入要删除的学生的学号,然后遍历数组,找到该学号对应的学生并删除。如果没有找到,则提示用户该学生不存在。
在修改学生信息时,我们首先让用户输入要修改的学生的学号,然后遍历数组,找到该学号对应的学生并修改。如果没有找到,则提示用户该学生不存在。
在遍历学生信息时,我们遍历整个数组,并依次输出每个学生的信息。
这个程序只是一个简单的示例,实际应用中可能需要更复杂的数据结构和算法来实现更复杂的功能。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)