student.h
1 #include<stdio.h>
2 #include<string.h>
3 #define MaxSize 50
4 struct student
5 {
6 int num;
7 char name[10];
8 int computer, english, math;
9 double average;
10 };
11 void new_student(struct student students[]);
12 void output_student(struct student students[]);
13 void average(struct student students[]);
14 void sort(struct student students[]);
15 void modify(struct student students[]);
16 void search_student(struct student students[], int num);
main.c
1 #include "student.h"
2 int Count = 0;
3 int main(void)
4 {
5 struct student students[MaxSize];
6 new_student(students);
7 average(students);
8 printf("New
");
9 output_student(students);
10 sort(students);
11 printf("After Sort:
");
12 output_student(students);
13 modify(students);
14 printf("After Modify:
");
15 output_student(students);
16 int num;
17 printf("Enter num:");
18 scanf_s("%d", &num);
19 search_student(students, num);
20
21 return 0;
22 }
input_output.c
1 #include "student.h"
2
3 extern Count;
4 void new_student(struct student students[])
5 {
6 int i, n;
7 if (Count == MaxSize)
8 {
9 printf("The array is full!
");
10
11 return;
12 }
13 printf("Input the Number of Students:");
14 scanf_s("%d", &n);
15 for (i = 0; i < n; i++)
16 {
17 printf("Input the student's num:");
18 scanf_s("%d", &students[i].num);
19 printf_s("Input the student's name:");
20 scanf_s("%s", students[i].name, 10);
21 printf("Input the student's math.score:");
22 scanf_s("%d", &students[i].math);
23 printf("Input the student's english.score:");
24 scanf_s("%d", &students[i].english);
25 printf("Input the student's computer.score:");
26 scanf_s("%d", &students[i].computer);
27
28 Count++;
29 }
30 }
31 void output_student(struct student students[])
32 {
33 int i;
34 if (Count == 0)
35 {
36 printf("Count of students is zero!
");
37 return;
38 }
39 printf("num name math english computer average
");
40 for (i = 0; i < Count; i++)
41 {
42 printf("%d %s %d %d %d %f
", students[i].num, students[i].name, students[i].math, students[i].english, students[i].computer, students[i].average);
43 }
44 }
aver_sort.c
1 #include "student.h"
2 extern Count;
3 void average(struct student students[])
4 {
5 int i;
6 for (i = 0; i < Count; i++)
7 {
8 students[i].average = 1.0 * (students[i].math + students[i].english + students[i].computer) / 3;
9 }
10 }
11 void sort(struct student students[])
12 {
13 struct student temp;
14 int i, j, index;
15 for (i = 0; i < Count - 1; i++)
16 {
17 index = i;
18 for (j = i + 1; j < Count; j++)
19 {
20 if (students[j].average > students[index].average)
21 {
22 index = j;
23 }
24 }
25 temp = students[i];
26 students[i] = students[index];
27 students[index] = temp;
28 }
29 }
modify.c
1 #include "student.h"
2 extern Count;
3 void modify(struct student students[])
4 {
5 int num, course, score, i;
6 struct student* p = students;
7 printf("Input the number of the students to be updated:");
8 scanf_s("%d", &num);
9 printf("Choice the course:1.math 2.english 3.computer:");
10 scanf_s("%d", &course);
11 printf("Input the new score:");
12 scanf_s("%d", &score);
13 for (i = 0; i < Count; i++, p++)
14 {
15 if (p->num == num)
16 {
17 break;
18 }
19 }
20 if (i < Count)
21 {
22 switch (course)
23 {
24 case 1:p->math = score; break;
25 case 2:p->english = score; break;
26 case 3:p->computer = score; break;
27 }
28 }
29 }
30 void search_student(struct student students[], int num)
31 {
32 int i, flag = 0;
33 if (Count == 0)
34 {
35 printf("Count of students is zero!
");
36 return;
37 }
38 for (i = 0; i < Count; i++)
39 {
40 if (students[i].num == num)
41 {
42 flag = 1;
43 break;
44 }
45 }
46 if (flag)
47 {
48 printf("num:%d, name:%s, math:%d, english:%d, computer:%d, average:%.2f
",
49 students[i].num, students[i].name, students[i].math, students[i].english, students[i].computer, students[i].average);
50 }
51 else
52 {
53 printf("Not Found!
");
54 }
55 };