1 //输入信息时形参只能用指针,输出信息时行参可以是指针、也可以是普通变量
2 #include<stdio.h>
3 #include <string.h>
4 #define MAX 5
5
6 struct student{
7 char name[20];
8 char id[10];
9 int score;
10 };
11
12 struct classroom{
13 int n;
14 struct student stu[MAX];
15 };
16 typedef struct classroom classroom_t;
17
18 //给班级初始化
19 void init_classroom(struct classroom *p)
20 {
21 p->n = 0;
22 memset(p->stu ,0 ,sizeof(p->stu));
23 return;
24 }
25 //输入学生信息的函数
26 void input_student(struct student *p)
27 {
28 printf("Input name:");
29 scanf("%s", p->name);
30
31 printf("Input id:");
32 scanf("%s", p->id);
33
34 printf("Input score:");
35 scanf("%d", &p->score);
36 return;
37 }
38 //输入班级信息的函数
39 void input_classroom(classroom_t *p,int n)//此时形参中定义的n是在栈中重新创建的,并不是教室结构体中的n(p->n)
40 {
41 int i;
42 if(n > MAX)
43 {
44 printf("the n is invalid,must less than or equal %d\n", MAX);
45 return;
46 }
47 for(i = 0; i < n ; i++)
48 {
49 printf("------------------------------\n");
50 input_student(p->stu + i );
51 p->n++;
52
53 }
54 //p->n = b;
55 // printf("%d\n",p->n);
56 // printf("%d\n",b);
57 return;
58 }
59
60 //输出学生信息的函数
61 // void output_student(struct student s)
62 // {
63 // printf("name\tid\tscore\t\n");
64 // printf("%s\t%s\t%d\t\n",s.name, s.id, s.score);
65
66 // printf("name:%s\n",s.name);
67 // printf("id:%d\n",s.id);
68 // printf("score:%d\n", s.score);
69 // return;
70 // }
71
72 void _output_student(struct student *pstu)
73 {
74
75 printf("name\tid\tscore\n");
76 printf("%s\t%s\t%d\n",pstu->name, pstu->id, pstu->score);
77
78 // printf("name:%s\n",pstu->name);
79 // printf("id:%d\n",pstu->id);
80 // printf("score:%d\n", pstu->score);
81 return;
82 }
83 //输出班级信息的函数
84 // void output_classroom(classroom_t *proom)
85 // {
86 // int i ;
87
88 // for(i = 0; i <proom->n ;i++)
89 // {
90 // printf("-------------%d------------\n", i);
91 // _output_student(&proom->stu[i]);
92 // _output_student(proom->stu + i);
93 // output_student(proom->stu[i]);
94 // }
95
96 // return;
97 // }
98
99 // void _output_classroom(classroom_t *proom)
100 // {
101 // int i ;
102
103 // printf("name\tid\tscore\n");
104 // for(i = 0;i < 3 ; i++)
105 // {
106 // printf("%s\t%s\t%d\n",proom->stu[i].name, proom->stu[i].id, proom->stu[i].score);
107 // }
108 // return;
109 // }
110
111
112 void output_classroom(classroom_t broom)
113 {
114 int i;
115 for(i = 0; i < broom.n; i++)
116 {
117 //output_student(broom.stu[i]);//输出变量的形式是p[i]
118 _output_student(&broom.stu[i]);
119 }
120
121 return;
122 }
123
124
125 int main(int argc, const char *argv[])
126 {
127
128 struct classroom room;
129 init_classroom(&room);
130 input_classroom(&room,3);
131 output_classroom(room);
132
133 //output_classroom(&room);
134
135 return 0;
136 }