# include <stdio.h> struct student { /* 学生信息结构定义 */ int num; /* 学号 */ char name[10]; /* 姓名 */ int computer, english, math; /* 三门课程成绩 */ 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%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:%.2lf\n", max.num, max.name, max.average); return 0; }向这个代码里面添加一个查询和修改学生的成绩功能
时间: 2024-02-14 07:16:15 浏览: 61
好的,可以尝试如下添加查询和修改学生成绩的代码:
```c++
#include <stdio.h>
#include <string.h>
struct student { /* 学生信息结构定义 */
int num; /* 学号 */
char name[10]; /* 姓名 */
int computer, english, math; /* 三门课程成绩 */
double average; /* 个人平均成绩 */
};
int main() {
int i, n, choice;
struct student stu[100],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%s%d%d%d",&stu[i].num,stu[i].name,&stu[i].math,&stu[i].english,&stu[i].computer);
stu[i].average = (stu[i].math + stu[i].english + stu[i].computer) / 3.0;
if (i == 1){
max = stu[i]; /* 结构变量赋值 */
}else if (max.average < stu[i].average){
max = stu[i];
}
}
printf("num:%d, name:%s, average:%.2lf\n", max.num, max.name, max.average);
while(1) {
printf("\nWhat do you want to do?\n");
printf("1. Query student's grade\n");
printf("2. Modify student's grade\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch(choice) {
case 1:
int num_query;
printf("Input student's number to query: ");
scanf("%d", &num_query);
for(int j = 1; j <= n; j++) {
if(stu[j].num == num_query) {
printf("Student info: num:%d, name:%s, math:%d, english:%d, computer:%d, average:%.2lf\n", stu[j].num, stu[j].name, stu[j].math, stu[j].english, stu[j].computer, stu[j].average);
break;
}
if(j == n) {
printf("No student found with the number %d\n", num_query);
}
}
break;
case 2:
int num_modify, course_modify, new_grade;
printf("Input student's number to modify: ");
scanf("%d", &num_modify);
printf("Which course grade do you want to modify? (1: math, 2: english, 3: computer) ");
scanf("%d", &course_modify);
printf("Input new grade: ");
scanf("%d", &new_grade);
for(int j = 1; j <= n; j++) {
if(stu[j].num == num_modify) {
switch(course_modify) {
case 1:
stu[j].math = new_grade;
break;
case 2:
stu[j].english = new_grade;
break;
case 3:
stu[j].computer = new_grade;
break;
default:
printf("Invalid input\n");
break;
}
stu[j].average = (stu[j].math + stu[j].english + stu[j].computer) / 3.0;
printf("Grade modified successfully!\n");
printf("New student info: num:%d, name:%s, math:%d, english:%d, computer:%d, average:%.2lf\n", stu[j].num, stu[j].name, stu[j].math, stu[j].english, stu[j].computer, stu[j].average);
break;
}
if(j == n) {
printf("No student found with the number %d\n", num_modify);
}
}
break;
case 3:
printf("Exit program.\n");
return 0;
default:
printf("Invalid input\n");
break;
}
}
return 0;
}
```
这个程序添加了一个 while 循环,让用户可以重复进行查询和修改操作,直到选择退出程序。在查询学生成绩时,用户需要输入学生的学号,程序会遍历学生信息结构体数组,找到对应学号的学生信息并输出。在修改学生成绩时,用户需要输入学生的学号、想要修改的科目编号以及新的成绩,程序会根据这些信息修改对应学生的成绩,并重新计算平均成绩,最后输出修改后的学生信息。
阅读全文