mission details
The task of this level: Use a structure to store student information (including student ID, name, grades of 3 courses, and total score). It is required to modify and delete student information, up to 50 students.
related information
Reference knowledge related to structure level 1
Programming requirements
Add code in the Begin-End section of the editor on the right , use structures to store student information, and modify and delete student information.
Test instruction
The platform will test the code you write and compare the values you output with the actual correct values. Only when all data are calculated correctly can the test pass:
Test input:
10 3
1 Siyu 90 90 90
2 Amy 60 70 94
3 Jack 60 60 98
4 Jack 77 90 60
5 Banana 60 60 40
6 White 60 60 60
7 Pinkman 60 60 60
8 Fring 60 60 70
9 Ehrmantraut 80 63 61
10 Schrader 99 66 100
1 Jack
2 1 100 100 100
3 10
Test input data description: Enter n and q . The n lines below the bid document are the information of n students, and the next q lines are the corresponding operations.
1 name //为根据名字查询操作
2 sno num1 num2 num2 //为修改分数操作,修改学号为sno的3门成绩
3 sno //为删除操作
Expected output:
3 Jack 60 60 98 218
4 Jack 77 90 60 227
1 Siyu 100 100 100 300
2 Amy 60 70 94 224
3 Jack 60 60 98 218
4 Jack 77 90 60 227
5 Banana 60 60 40 160
6 White 60 60 60 180
7 Pinkman 60 60 60 180
8 Fring 60 60 70 190
9 Ehrmantraut 80 63 61 204
10 Schrader 99 66 100 265
1 Siyu 100 100 100 300
2 Amy 60 70 94 224
3 Jack 60 60 98 218
4 Jack 77 90 60 227
5 Banana 60 60 40 160
6 White 60 60 60 180
7 Pinkman 60 60 60 180
8 Fring 60 60 70 190
9 Ehrmantraut 80 63 61 204
Expected output data description: Each modification operation will output all student information, and a query operation will output the queried student information.
Start your mission and wish you success!
#include<stdio.h>
#include<string.h>
int Count;
struct student {
char sno[20], name[20];
int math, english, chinese, sum;
};
void print(struct student stu) {
printf("%s %s %d %d %d %d\n", stu.sno, stu.name, stu.math, stu.english, stu.chinese, stu.sum);
}
void query_stu(struct student s[], char* name) {
/*********Begin*********/
//遍历结构体数组,找到与name相同的学生,并打印其信息
for (int i = 0; i < Count; i++) {
if (strcmp(s[i].name, name) == 0) {
print(s[i]);
}
}
/*********End**********/
}
void delete_stu(struct student s[], char* sno) {
/*********Begin*********/
//遍历结构体数组,找到与sno相同的学生,并将其后面的元素依次向前移动一位
for (int i = 0; i < Count; i++) {
if (strcmp(s[i].sno, sno) == 0) {
for (int j = i + 1; j < Count; j++) {
s[j - 1] = s[j];
}
break;
}
}
/*********End**********/
}
void update_stu(struct student s[], char* sno, int math, int english, int chinese) {
/*********Begin*********/
//遍历结构体数组,找到与sno相同的学生,并更新其成绩和总分
for (int i = 0; i < Count; i++) {
if (strcmp(s[i].sno, sno) == 0) {
s[i].math = math;
s[i].english = english;
s[i].chinese = chinese;
s[i].sum = math + english + chinese;
break;
}
}
/*********End**********/
}
int main(void)
{
int n, q;
struct student students[50];
scanf("%d%d", &n, &q);
Count = n;
for (int i = 0; i < n; i++) {
/*********Begin*********/
//输入每个学生的信息,并计算总分
scanf("%s%s%d%d%d", &students[i].sno,
&students[i].name,
&students[i].math,
&students[i].english,
&students[i].chinese);
students[i].sum =
students[i].math +
students[i].english +
students[i].chinese;
/*********End**********/
}
while (q--) {
int op;
scanf("%d", &op);
char sno[20], name[20];
if (op == 1) {
scanf("%s", name);
query_stu(students, name);
}
else if (op == 2) {
int a, b, c;
scanf("%s%d%d%d", sno, &a, &b, &c);
update_stu(students, sno, a, b, c);
for (int i = 0; i < Count; i++)
print(students[i]);
}
else {
scanf("%s", sno);
delete_stu(students, sno);
for (int i = 0; i < Count - 1; i++)
print(students[i]);
}
}
return 0;
}