c: struct sort descending and ascending

 写法方式之一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 * @file hello.c
 * @author your name (geovindu)
 * @brief
 * @ide vscode c11,c17 windows 10
 * @version 0.1
 * @date 2023-11-05
 *
 * @copyright Copyright (c) 2023
 *
 */
 
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
 
struct num1
{
    char a; //1
    char b; //2
    double c; //8
    short d;
};
 
struct num2
{
    char a; //1
    double b;//2
    char c;
    int d;
 
};
 
/**
 * @brief 学生
 *
 */
struct Student
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 成绩
     *
     */
    int score;
};
 
/**
 * @brief 英雄
 *
 */
struct Hero
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 性别
     *
     */
    char sex[2];
};
 
/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b){
 
    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}
 
 
 
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n)
 {
         
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    cmp(&her[j],&her[j+1]);
            }
        }
 }
 
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void swap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}
 
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    swap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
            }
        }
 }
 
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age<her[j+1].age)
                    swap(&her[j],&her[j+1]);
 
            }
        }
 }
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
        }
    }
 
int main()
{
 
    puts("hello c language world!涂聚文\t");
    printf("涂聚文");
    struct Student stu={"张三",18,100};
 
    struct Student *p=&stu;
    p->score=80;
    printf("姓名%s 年龄%d 成绩%d \n",p->name,p->age,p->score);
    printf("输入5位英雄:\n");
    printf("姓名\t 年龄 \t 性别:\n");
    int n;
    struct Hero sz[100];
    n=5;
    for(int i=0;i<n;i++){
        scanf("%s %d %s",&sz[i].name,&sz[i].age,&sz[i].sex);
    }
 
    /*
    qsort函数参数:
    
    */
    //1
    //qsort(sz,n,sizeof(sz[0]),cmp);
    //2
    //SortBubble(sz,5);
    //3
    //SortBubbleDesc(sz,5);
    //4
    SortBubbleAsc(sz,5);
    //qsort(sz,n,sizeof(sz[0]),cmpSort);
    printf("\n按年龄升序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %s \t%d \n",sz[i].name,sz[i].sex,sz[i].age);
    }
 
 
    struct num1 n1;
    struct num2 n2;
    printf("sizeof(n1)=%d,sizeof(n2)%d\n",sizeof(n1),sizeof(n2));
 
 
    system("pause");
    return 0;
}

  

 写法方式之二:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
 * @file StudentStructSort.h
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  Ubuntu 22.4
 * @brief 结构体排序示例
 
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 */
 
#ifndef STUDENTSTRUCTSORT_H_
#define STUDENTSTRUCTSORT_H_
  
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>
 
/**
 * @brief 英雄
 *
 */
struct Hero
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 性别
     *
     */
    char sex[2];
};
  
 
/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b);
 
 
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n);
 
 
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py);
 
 
 
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n);
 
 
 
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n);
 
 
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n);
 
 
#endif

  

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
 * @file StudentStructSort..c
 * @brief 结构体排序示例
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 */
 
#include "include/StudentStructSort.h"
 
 
 
 
/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b){
  
    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}
  
  
  
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n)
 {
          
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    cmp(&her[j],&her[j+1]);
            }
        }
 }
  
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}
  
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
            }
        }
 }
  
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age<her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
  
            }
        }
 }
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
        }
    }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * @file geovindu.h
 * @brief
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 * @copyright Copyright (c) 2023
 *
 */
 
 
 
#ifndef GEOVINDU_H_
#define GEOVINDU_H_
  
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
 
/**
 * @brief
 *
 */
void displayHero();
 
 
 
#endif

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
 * @file geovindu.c
 * @brief
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  Ubuntu 22.4
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 * @copyright Copyright (c) 2023
 *
 */
 
#include "include/StudentStructSort.h"
 
 
/**
 * @brief
 *
 */
void displayHero()
{
    printf("输入5位英雄:\n");
    printf("姓名\t 年龄 \t 性别:\n");
    int n;
    struct Hero sz[100];
    n=5;
    for(int i=0;i<n;i++){
        scanf("%s %d %s",&sz[i].name,&sz[i].age,&sz[i].sex);
    }
  
    /*
    qsort函数参数:
     
    */
    //1
    //qsort(sz,n,sizeof(sz[0]),cmp);
    //2
    //SortBubble(sz,5);
    //3
    SortBubbleDesc(sz,5);
    printf("\n按年龄降序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %d \t%s \n",sz[i].name,sz[i].age,sz[i].sex);
    }
    //4
    SortBubbleAsc(sz,5);
    //qsort(sz,n,sizeof(sz[0]),cmpSort);
    printf("\n按年龄升序为:\n\n");
    printf("姓名\t 年龄 \t 性别:\n");
    for(int i=0;i<n;i++){
        printf("%s\t %d \t%s \n",sz[i].name,sz[i].age,sz[i].sex);
    }
}

  

调用:

1
2
3
4
printf("hello c world, \n");
printf("你好,中国\n");
 
displayHero();

  

输出:

 

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
 * @file StudentStructSort.h
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  windows 10
 * @brief 结构体排序示例
 
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 */
 
#ifndef STUDENTSTRUCTSORT_H_
#define STUDENTSTRUCTSORT_H_
  
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>
 
/**
 * @brief 英雄
 *
 */
struct Hero
{
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int age;
    /**
     * @brief 性别
     *
     */
    char sex[2];
};
  
/**
 * @brief 学生实体
 *
 */
struct Student
{
 
    /**
     * @brief 姓名
     *
     */
    char name[20];
    /**
     * @brief 年龄
     *
     */
    int  age;
    /**
     * @brief 班级
     *
     */
    char class[20];
    /**
     * @brief 成绩
     *
     */
    double score;
 
 
 
};
 
/**
 * @brief 老师实体
 *
 */
struct Teacher
{
 
    /**
     * @brief 教师编号
     *
     */
    int id;
    /**
     * @brief 老师姓名
     *
     */
    char *Name;
    /**
     * @brief 教师年龄
     *
     */
    int age;
    /**
     * @brief 所教管的学生集合
     *
     */
    //struct Student **stus;
    struct Student stus[5];
} Teacher;
 
/**
 * @brief 老师实体
 *
 */
struct NewTeacher
{
 
    /**
     * @brief 教师编号
     *
     */
    int id;
    /**
     * @brief 老师姓名
     *
     */
    char *Name;
    /**
     * @brief 教师年龄
     *
     */
    int age;
    /**
     * @brief 所教管的学生集合
     *
     */
    //struct Student **stus;
    struct Student **stus;
 
} NewTeacher;
 
/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b);
 
 
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n);
 
 
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py);
 
 
 
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n);
 
 
 
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n);
 
 
  /**
   * @brief
   *
   * @param her
   * @param n
   */
void PrintList(struct Hero her[],int n);
 
 
    /**
     * @brief 静态编历老师
     *
     */
void DisplayTeacher();
 
/**
 * @brief 遍历老师学生
 *
 */
void DisplayTeacher1();
 
/**
 * @brief
 *
 * @param p
 * @param n1 老师个数
 * @param n2 学生个数
 */
void initTeacher(struct NewTeacher *p, int n1, int n2);
 
/**
 * @brief Create a Teacher object
 *
 * @param p
 * @param n1 老师个数
 * @param n2  学生个数
 * @return int
 */
int createTeacher(struct NewTeacher **p, int n1, int n2);
 
 
 
#endif

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
 * @file StudentStructSort..c
 * @brief 结构体排序示例
 * @author       geovindu,Geovin Du,涂聚文 (geovindu@163.com)
 * ide: vscode c11,c17  windows 10
 * @date 2023-11-05
 * @version 0.1
 * @copyright   geovindu 站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 */
 
#include "include/StudentStructSort.h"
 
 
#define false 0
#define true 1
 
/**
 * @brief 升序排序
 *
 * @param a
 * @param b
 * @return int
 */
int cmp(const void *a,const void *b)
{
  
    struct Hero c=*(struct Hero*)a;
    struct Hero d=*(struct Hero*)b;
    //按升序排序
    return c.age-d.age;
}
  
  
  
/**
 * @brief 升降
 *
 * @param her
 * @param n
 */
void SortBubble(struct Hero her[10],int n)
 {
          
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    cmp(&her[j],&her[j+1]);
            }
        }
 }
  
/**
 * @brief 比较
 *
 * @param px
 * @param py
 */
void TuSwap(struct Hero *px, struct Hero *py) // Definition of Swap function
{
  struct Hero temp;
  temp = *px;
  *px = *py;
  *py = temp;
}
  
/**
 * @brief 升序
 *
 * @param her
 * @param n
 */
void SortBubbleAsc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age>her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
                    //temp=her[j];
                   // her[j]=her[j+1];
                   // her[j+1]=temp;
            }
        }
 }
  
/**
 * @brief 降序
 *
 * @param her
 * @param n
 */
void SortBubbleDesc(struct Hero her[10],int n)
 {
        int i,j;
        struct Hero temp;
        for(int i=0;i<n-1;i++)
        {
            for(int j=0;j<n-i-1;j++)
            {
                if(her[j].age<her[j+1].age)
                    TuSwap(&her[j],&her[j+1]);
  
            }
        }
 }
  /**
   * @brief
   *
   * @param her
   * @param n
   */
   void PrintList(struct Hero her[],int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("信息:%s \t %d\t %s$\n",her[i].name,her[i].age,her[i].sex);
        }
    }
 
 
 
    /**
     * @brief 静态编历老师
     *
     */
    void DisplayTeacher()
    {
        //struct Teacher teachs[3];
        struct Student stusd[5];
 
 
        struct Student **st1[5] ={ {"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}};
        struct Student **st2[5] ={ {"李一", 17, "二班",50},{"李三", 18, "二班",78},{"李三", 18, "二班",78},{"李三", 19, "二班",57},{"李三", 18, "二班",90}};
        struct Student **st3[5] ={ {"赵一", 19, "三班",68},{"赵三", 17, "三班",98},{"赵三", 15, "三班",97},{"赵三", 17, "三班",98},{"赵三", 16, "三班",78}};
 
        struct Teacher te1={1000,"吴丽老师",31,st1};
        struct Teacher te2={1001,"林祥生老师",29,st2};
        struct Teacher te3={1002,"何磊老师",32,st3};
 
        //struct Teacher te1={1000,"吴丽老师",31, {{"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}}};
        //struct Teacher te2={1000,"林祥生老师",29, {{"张一", 17, "二班",50},{"张三", 18, "二班",78},{"张三", 18, "二班",78},{"张三", 19, "二班",57},{"张三", 18, "二班",90}}};
        //struct Teacher te3={1000,"何磊老师",32, {{"张一", 19, "三班",68},{"张三", 17, "三班",98},{"张三", 15, "三班",97},{"张三", 17, "三班",98},{"张三", 16, "三班",78}}};
 
        //struct Teacher teachs[3]={te1,te2,te3};
        struct Teacher teachs[3]={{1000,"吴丽老师",31,{ {"张一", 18, "一班",100},{"张二", 15, "一班",45},{"张三", 16, "一班",89},{"张四", 18, "一班",99},{"张五", 17, "一班",88}}},
        {1001,"林祥生老师",29,{ {"李一", 17, "二班",50},{"李二", 18, "二班",78},{"李三", 18, "二班",78},{"李四", 19, "二班",57},{"李五", 18, "二班",90}}},
        {1002,"何磊老师",32,{ {"赵一", 19, "三班",68},{"赵二", 17, "三班",98},{"赵三", 15, "三班",97},{"赵四", 17, "三班",98},{"赵五", 16, "三班",78}}}
        };
 
 
         
        for(int i=0;i<3;i++)
        {
           printf("老师姓名\t编号\t年龄\n");
            printf("%s\t%d\t%d\t%s\n",teachs[i].Name,teachs[i].id,teachs[i].age,teachs[i].stus);
         
            printf("管理学生\n");
            printf("姓名\t班级\t年龄\t分数\n");
            //stus=teachs[i].stus;
            struct Student *ppt=teachs[i].stus;
            for(int j=0;j<5;j++)
            {
                   printf("%s\t%s\t%d\t%.2f\n", teachs[i].stus[j].name,teachs[i].stus[j].class,teachs[i].stus[j].age,teachs[i].stus[j].score);
            }
            //stusd=teachs[i].stus;
            //struct Student *stus=ppt[0];
            //for (; i <sizeof(books)/sizeof(struct Book); i++) {
            //for(int j=0;j<sizeof(teachs[i].stus)/sizeof(struct Student);j++)
            //{
               //printf("%s\t%s\t%d\t%d\t%d\n",stus->name,stus->class,stus->age,stus->score);
 
            //}
           // for(int j=0;j<sizeof(teachs[i].stus)/sizeof(struct Student);j++)
           // {
               //printf("%s\t%s\t%d\t%d\t%d\n",ppt->name,ppt->class,ppt->age,ppt->score);
 
            //}
            //ppt++;
            //PrintStuList(ppt,5);
 
            printf("\n");
        }
 
 
   }
 
 
/**
 * @brief Create a Teacher object
 *
 * @param p
 * @param n1 老师个数
 * @param n2  学生个数
 * @return int
 */
int createTeacher(struct NewTeacher **p, int n1, int n2)
{
    int len=0;
    int i,j,k;   //分配5个Student信息
    *p = (struct NewTeacher*)malloc (sizeof(struct NewTeacher)*n1);
 
    for (i=0;i<n1;i++)  //分配5个导师的地址空间
    {
        (*p)[i].Name=(char *)malloc (sizeof(char)*20); 
        if ((*p)[i].Name == NULL)
        {
          return false;
        }
    }
 
    //(*p)->stu=(char **)malloc (sizeof(char*)*5);
    for (j=0;j<n2;j++)
        {
         (*p)[j].stus=(char **)malloc (sizeof(char)*n2);
         if ((*p)[j].stus == NULL)
         {
           return false;
         }
         for (k=0;k<3;k++)
         {
         (*p)[j].stus[k]=(char *)malloc (sizeof(char)*20);
         if ((*p)[j].stus[k] == NULL)
             {
             return false;
             }
         }
        }
    return true;
}
/**
 * @brief
 *
 * @param p
 * @param n1 老师个数
 * @param n2 学生个数
 */
void initTeacher(struct NewTeacher *p, int n1, int n2)
{
      int i,j,k=0;  //
      if (p == NULL)
      {
        printf ("error\n");
      }
      //n1 =3; //导师个数
      //n2 = 5 //学生
         struct Student *st1[5] ={ {"张一", 18, "一班",100},{"张三", 15, "一班",45},{"张三", 16, "一班",89},{"张三", 18, "一班",99},{"张三", 17, "一班",88}};
        struct Student *st2[5] ={ {"张一", 17, "二班",50},{"张三", 18, "二班",78},{"张三", 18, "二班",78},{"张三", 19, "二班",57},{"张三", 18, "二班",90}};
        struct Student *st3[5] ={ {"张一", 19, "三班",68},{"张三", 17, "三班",98},{"张三", 15, "三班",97},{"张三", 17, "三班",98},{"张三", 16, "三班",78}};
 
     puts ("-----导师赋值------");
     for (i=0;i<n1;i++)
     {
        char *buf[3]={"吴丽老师","林祥生老师","何磊老师"};
        int *sage[3]={31,29,32};
        int *sid[3]={10,11,12};
        //p[i].age=22+i;
        //printf ("%d",p[i].age);
        p[i].id=sid[i];
        p[i].Name=buf[i];
        p[i].age=sage[i];
        //strcpy (p[i].id,sid[i]);
        //strcpy (p[i].Name,buf[i]);
        //strcpy (p[i].age,sage[i]);
 
        //  printf ("%s",p[i].tName);
        for (j=0;j<n2;j++)
        {
           
          //struct Student *arr[]={"小黎","小田","小张","小王","小胡","小范",
             // "小杨","小石","小柯"};
            //p[i].stus[j]->name=st1[j]->name;
            //p[i].stus[j]->class=st1[j]->class;
            //p[i].stus[j]->age=st1[j]->age;
           // p[i].stus[j]->score=st1[j]->score;
          //strcpy (p[i].stus[j],st1[k]);
          ++k;
        //  printf ("%s\n",p[i].stu[j]);
        }
     }
}
 
/**
 * @brief 打印结构体成员信息
 *
 * @param p
 * @param n1 老师个数
 * @param n2 学生个数
 */
void printTeacher(struct NewTeacher *p, int n1, int n2)
{
      int i,j;
      if (p == NULL)
      {
       printf ("error\n");
      }
 
      for (i=0;i<n1;i++)
      {
        printf ("\t\t%s\t%d\t%d\n",p[i].Name,p[i].id,p[i].age);  //导师
        for (j=0;j<n2;j++)
        {
           //printf ("\t%s",p[i].stus[j]);
        }
        putchar('\n');
        printf ("\t\t%d\n\n",p[i].age);
      }
}
 
/**
 * @brief 根据导师名字排序, 降序
 *
 * @param p
 * @param n 老师个数
 */
void sortTeacher(struct NewTeacher *p, int n)
{
      struct NewTeacher t1;
      int i,j;
       if (p==NULL)
       {
         printf ("error\n");
       }
       for (i=0;i<n-1;i++)
       {
         for (j=i+1;j<n;j++)
         {
           if ((strcmp (p[i].Name,p[j].Name))>0)
           {
             t1=p[i];
             p[i]=p[j];
             p[j]=t1;
           }
         }
       }
 
}
/**
 * @brief 释放空间,在函数内部把p赋值为NULL
 *
 * @param p
 * @param n1 老师个数
 * @param n2 学生个数
 */
void freeTeacher(struct NewTeacher **p, int n1, int n2)
{
       int i,j;
       if (p == NULL)
       {
         printf ("Empty\n");
       }
       else
           {
              for (i=0;i<n1;i++)
              {
                 for (j=0;j<n2;j++)
                 {
                   if (p[i]->stus[j]!=NULL)
                   {
                     free (p[i]->stus[i]);
                     p[i]->stus[j]=NULL;
                   }
                 }
                 if (p[i]->stus!= NULL)
                 {
                   free (p[i]->stus);
                   p[i]->stus=NULL;
                 }
              }
           }
}
 
/**
 * @brief 遍历老师学生
 *
 */
void DisplayTeacher1()
{
 
        int ret = 0;
        int n1 = 3; //导师个数
        int n2 = 5; //学生
        struct Teacher *p = NULL;
        ret = createTeacher(&p, n1, n2);
        if (ret == false)
            {
            printf("createTeacher err:%d\n", ret);
            exit (EXIT_FAILURE);
            }
 
        initTeacher(p, n1, n2); //给成员赋值
        //打印成员,排序前
        printf("排序前:\n");
        printTeacher(p, n1, n2);
        //根据导师名字排序, 降序
        sortTeacher(p, n1);
        //打印成员,排序后
        printf("\n排序后:\n");
        printTeacher(p, n1, n2);
}
 
 
    
    /**
     * @brief
     *
     * @param stusd
     * @param n
     */
    void PrintStuList(struct Student *stusd,int n)
    {
        for(int i=0;i<n;i++)
        {
            printf("%s\t%s\t%d\t%d\t%d\n",stusd[i].name,stusd[i].class,stusd[i].age,stusd[i].score);
        }
    }

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
꧁꫞꯭我加油꫞꧂🐬🌹:
/**
 * @brief       学生
 *
 */
struct GaoStudent
{
 char *name;  // 或用 char *name
 float score;
 int teachid;
};
 
/**
 * @brief       老师
 *
 */
struct GaoTeacher
{
 
    int id;
    char *name;//1名老师,管理3名学生
    //struct GaoStudent stu[130]; // 或用: 这里什么类型,对应的
     struct GaoStudent *stu  
 
};
 
/**
 * @brief      
 *
 */
void teststu()
{
    //学生
    struct GaoStudent stu[9] = {
    {"Tom", 89.00,3 },
    { "Marry", 79.00,1},
    { "Jerry", 86.00,2 },
    { "张三", 88.00,3 },
    { "李四", 82.00,1 },
    { "王五", 80.00,2},
    { "Kate", 79.60 ,3},
    { "Bob", 89.78,1 },
    { "Lily", 90.36 ,2}
    };
 
   struct GaoStudent studata3[3] ={    {"Tom", 89.00,3 },
    { "Marry", 79.00,1},
    { "Jerry", 86.00,2 }};
   struct GaoStudent *stu3=studata3;
    struct GaoStudent studata4[3] ={
        { "张三", 88.00,3 },
        { "李四", 82.00,1 },
        { "王五", 80.00,2}
    };
    struct GaoStudent *stu4=studata4;
   struct GaoStudent studata5[3] =
   {
    { "Kate", 79.60 ,3},
    { "Bob", 89.78,1 },
    { "Lily", 90.36 ,2}
   };
   struct GaoStudent *stu5=studata5;
    // 老师
    struct GaoTeacher t[3] = {
    { 1, "刘杰",stu3},
    { 2, "王大", stu4},
    { 3, "陈小", stu5}
    };
 
    for(int i = 0; i < 3; i++)
    {
    printf("编号%d 老师%s管理的学生有: \n", t[i].id,t[i].name ); //, t[i].stu->name
    //数组时用
    //struct GaoStudent *pt=&t[i].stu;
    struct GaoStudent *pt=t[i].stu;
    printf("姓名\t 分数\n");
    for(int j=0;j<3;j++)
    {
            printf("%s\t%.2f\n",(*pt++).name,pt->score);
    }
    printf("\n");
    };
     
}

  

vscode 调资源文件

 

Eclipse IDE for Embedded C and C++ Developers 调头文件

 

 

1
2
3
        //vscode 两个写法都可以。
        //printf("%s\t%.2f\t%d\n",(*pt++).name,(*pt).score,(*pt).teachid);
        printf("%s\t%.2f\t%d\n",(*pt++).name,pt->score,pt->teachid);

  

CLin 2023.1 结构也是调头文件

 

posted @   ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-11-05 CSharp: Factory Method Pattern in donet 6
2009-11-05 Dijkstra's Shortest Path Algorithm(最短路径算法)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示