1 /*修改学生成绩,结构指针作为函数参数*/
2 #include <stdio.h>
3 struct student
4 {
5 int num;
6 char name[10];
7 int computer, english, math;
8 double average;
9 };
10 int update_score(struct student *p, int n, int num, int course, int score);
11 int main(void)
12 {
13 int course, i, n, num, pos, score;
14 struct student students[50];
15
16 printf("Input n:");
17 scanf("%d", &n);
18 for (i = 0; i < n; i++)
19 {
20 printf("Input the info of No.%d:\n", i + 1);
21 printf("number:");
22 scanf("%d", &students[i].num);
23 printf("name:");
24 scanf("%s", students[i].name);
25 printf("math score:");
26 scanf("%d", &students[i].math);
27 printf("english score:");
28 scanf("%d", &students[i].english);
29 printf("computer score:");
30 scanf("%d", &students[i].computer);
31 }
32
33 printf("Input the number of the students to be updated:");
34 scanf("%d", &num);
35 printf("Choice the course:1.math 2.english 3.computer:");
36 scanf("%d", &course);
37 printf("Input the new score:");
38 scanf("%d", &score);
39
40 pos = update_score(students, n, num, course, score);
41
42 if (pos == -1)
43 {
44 printf("Not found!\n");
45 }
46 else
47 {
48 printf("After update:\n");
49 printf("%d\t%d\t%d\t%d\n", students[pos].num, students[pos].math, students[pos].english, students[pos].computer);
50 }
51
52 return 0;
53 }
54 int update_score(struct student *p, int n, int num, int course, int score)
55 {
56 int i, pos;
57 for (i = 0; i < n; i++, p++)
58 {
59 if (p->num == num)
60 {
61 break;
62 }
63 }
64 if (i < n)
65 {
66 switch (course)
67 {
68 case 1:
69 p->math = score;
70 break;
71 case 2:
72 p->english = score;
73 break;
74 case 3:
75 p->computer = score;
76 break;
77 }
78 pos = i;
79 }
80 else
81 {
82 pos = -1;
83 }
84
85 return pos;
86 }