指向结构体的指针作业

 1 #include <stdio.h>
 2 #define N 5
 3 struct Student {
 4     char name[20];
 5     int num;
 6     char sex;
 7     int age;
 8     char department[20];
 9 };
10 void inputStudent(struct Student *p, int n);
11 void sortStudent(struct Student *p, int n);
12 void swap(struct Student *p, struct Student *q);
13 void outputStudent(struct Student *p, int n);
14 int main(void)
15 {
16     struct Student student[N];
17     
18     inputStudent(student, N);
19     sortStudent(student, N);
20     outputStudent(student, N);
21     
22     return 0;
23 }
24 void inputStudent(struct Student *p, int n)
25 {
26     int i;
27     for (i = 0; i < n; i++, p++) {
28         printf("请输入第%d个学生姓名:", i + 1);
29         scanf("%s", p->name);
30         printf("请输入第%d个学生学号:", i + 1);
31         scanf("%d", &(p->num));
32         getchar();
33         printf("请输入第%d个学生性别:", i + 1);
34         scanf("%c", &(p->sex));
35         printf("请输入第%d个学生院系:", i + 1);
36         scanf("%s", p->department);
37         getchar();
38     }
39 }
40 void sortStudent(struct Student *p, int n)
41 {
42     struct Student *end_p, *index_p, *temp_p;
43     
44     //指向最后一个学生的指针
45     end_p = p + n -1;
46     //遍历结构体数组
47     for (; p < end_p; p++) {
48         index_p = p;
49         //找出最小值
50         for (temp_p = index_p + 1; temp_p <= end_p; temp_p++) {
51             if (temp_p->num < index_p->num) {
52                 index_p = temp_p;
53             }
54         }
55         if (index_p != p) {
56             swap(p, index_p);
57         }
58     }
59 }
60 void swap(struct Student *p, struct Student *q)
61 {
62     struct Student temp;
63     
64     temp = *p;
65     *p = *q;
66     *q = temp;
67 }
68 void outputStudent(struct Student *p, int n)
69 {
70     struct Student *end_p;
71     
72     end_p = p + n - 1;
73     for (; p <= end_p; p++) {
74         printf("%s	%d	%c	%s
", p->name, p->num, p->sex ,p->department);
75     }
76 }

原文地址:https://www.cnblogs.com/2018jason/p/12929782.html