#include<stdio.h> struct student { *学生信息结构定义* int num; 冰学号* char name[10]; 冰姓名* intcomputer,english,math;1*三门课程成繢*/ double average; *个人平均成绩*/ }; int main() int i, n; struct student stu,max; 冰定义结构变量* printf ("Input n: "); scanf ("%d", &n); printf ("Input the student's number, name and course scores\n"); for (i = 1; i <= n; i++){ printf ("No.%d: ", i); scanf %d".&stu.num,stu.name,&stu.math,&stu.english,&stu.computer); stu.average = (stu.math + stu.english + stu.computer) / 3.0; if (i == 1){ max = stu; 冰结构变量赋值*/ Felseif(max.average<stu.average){ max = stu; ["%d%5%d%d 冰结构变量赋值*/ Felseif(max.average<stu.average){ max = stu; ["%d%5%d%d printf("num:%d, max.average); return 0; name:%5, average:%.2Ifn", max.num, max.name, ) return 0; }向这个代码里面添加查询和修改功能
时间: 2024-03-20 15:44:40 浏览: 116
学生成绩管理
要添加查询和修改功能,需要在主函数中添加相应的代码。下面是修改后的代码:
```c
#include <stdio.h>
#include <string.h>
struct student {
int num; // 学号
char name[10]; // 姓名
int computer; // 计算机成绩
int english; // 英语成绩
int math; // 数学成绩
double average; // 平均成绩
};
int main() {
int i, n, choice, num;
struct student stu, max = {0};
printf("Input n: ");
scanf("%d", &n);
printf("Input the student's number, name and course scores\n");
for (i = 1; i <= n; i++) {
printf("No.%d: ", i);
scanf("%d%s%d%d%d", &stu.num, stu.name, &stu.math, &stu.english, &stu.computer);
stu.average = (stu.math + stu.english + stu.computer) / 3.0;
if (i == 1) {
max = stu;
} else if (max.average < stu.average) {
max = stu;
}
}
printf("num:%d, name:%s, average:%.2f\n", max.num, max.name, max.average);
// 添加查询和修改功能
printf("Enter 1 to query a student's information, 2 to modify a student's information: ");
scanf("%d", &choice);
if (choice == 1) {
printf("Enter the student's number: ");
scanf("%d", &num);
for (i = 1; i <= n; i++) {
printf("No.%d: ", i);
scanf("%d%s%d%d%d", &stu.num, stu.name, &stu.math, &stu.english, &stu.computer);
stu.average = (stu.math + stu.english + stu.computer) / 3.0;
if (stu.num == num) {
printf("num:%d, name:%s, math:%d, english:%d, computer:%d, average:%.2f\n", stu.num, stu.name, stu.math, stu.english, stu.computer, stu.average);
break;
}
}
if (i > n) {
printf("Student not found.\n");
}
} else if (choice == 2) {
printf("Enter the student's number: ");
scanf("%d", &num);
for (i = 1; i <= n; i++) {
printf("No.%d: ", i);
scanf("%d%s%d%d%d", &stu.num, stu.name, &stu.math, &stu.english, &stu.computer);
stu.average = (stu.math + stu.english + stu.computer) / 3.0;
if (stu.num == num) {
printf("Enter the new information:\n");
scanf("%d%s%d%d%d", &stu.num, stu.name, &stu.math, &stu.english, &stu.computer);
stu.average = (stu.math + stu.english + stu.computer) / 3.0;
printf("num:%d, name:%s, math:%d, english:%d, computer:%d, average:%.2f\n", stu.num, stu.name, stu.math, stu.english, stu.computer, stu.average);
break;
}
}
if (i > n) {
printf("Student not found.\n");
}
} else {
printf("Invalid choice.\n");
}
return 0;
}
```
在上面的代码中,我们添加了一个 `choice` 变量,用于选择查询或修改功能。当用户选择查询时,我们需要再次输入要查询的学生的学号,然后遍历学生数组,找到对应学号的学生,输出其信息。当用户选择修改时,我们也需要输入要修改的学生的学号,然后遍历学生数组,找到对应学号的学生,再次输入该学生的信息,更新结构体中的信息。如果找不到对应学号的学生,输出相应的错误信息。
阅读全文