如何在以上代码的基础上实现修改学生信息:根据输入的学号,可以修改该学生的姓名和成绩信息
时间: 2024-03-17 07:40:29 浏览: 27
要实现根据学号修改学生信息的功能,您可以在程序中添加一个新的函数,名为modify()。这个函数可以通过遍历学生数组,查找到对应学号的学生信息,并让用户输入新的姓名和成绩信息,最后对原有的学生信息进行更新。
下面是一个示例代码,可以帮助您了解如何实现这个功能:
```
#include <stdio.h>
struct student {
int num;
char name[20];
float score[3];
} stu[5];
void print(struct student stu[5]);
void modify(struct student stu[5], int num);
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("num:");
scanf("%d", &stu[i].num);
printf("name:");
scanf("%s", &stu[i].name);
int j;
for (j = 0; j < 3; j++) {
printf("score:");
scanf("%f", &stu[i].score[j]);
}
printf("\n");
}
print(stu);
int num;
printf("Please input the student number you want to modify:");
scanf("%d", &num);
modify(stu, num);
print(stu);
return 0;
}
void print(struct student stu[5]) {
int i, j;
printf(" num name score1 score2 score3 \n");
for (i = 0; i < 5; i++) {
printf("%5d%10s", stu[i].num, stu[i].name);
for (j = 0; j < 3; j++)
printf("%10.2f", stu[i].score[j]);
printf("\n");
}
}
void modify(struct student stu[5], int num) {
int i;
for (i = 0; i < 5; i++) {
if (stu[i].num == num) {
printf("The student you want to modify is:\n");
printf("%5d%10s%10.2f%10.2f%10.2f\n", stu[i].num, stu[i].name, stu[i].score[0], stu[i].score[1], stu[i].score[2]);
printf("Please input the new name:");
scanf("%s", &stu[i].name);
int j;
for (j = 0; j < 3; j++) {
printf("Please input the new score%d:", j + 1);
scanf("%f", &stu[i].score[j]);
}
printf("Modify successfully!\n");
return;
}
}
printf("Student not found!\n");
}
```
在上述代码中,我们在main()函数中新增了一个变量num,用于保存用户输入的学号。然后,我们调用了modify()函数,将学生数组和学号作为参数传递给这个函数。在modify()函数中,我们使用一个for循环遍历学生数组,查找到对应学号的学生信息,并让用户输入新的姓名和成绩信息。如果找到了对应的学生,就对其信息进行更新,然后直接返回。如果没有找到对应的学生,就输出“Student not found!”的提示信息。
希望这个示例代码能够帮助您实现修改学生信息的功能。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)