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 %dn", 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("%dn",p->n);
 56     // printf("%dn",b);
 57     return;
 58 }
 59 
 60 //输出学生信息的函数
 61 // void output_student(struct student s)
 62 // {
 63     // printf("nametidtscoretn");
 64     // printf("%st%st%dtn",s.name, s.id, s.score);
 65     
 66     // printf("name:%sn",s.name);
 67     // printf("id:%dn",s.id);
 68     // printf("score:%dn", s.score);
 69     // return;
 70 // }
 71 
 72 void _output_student(struct student *pstu)
 73 {
 74 
 75     printf("nametidtscoren");
 76     printf("%st%st%dn",pstu->name, pstu->id, pstu->score);
 77 
 78     // printf("name:%sn",pstu->name);
 79     // printf("id:%dn",pstu->id);
 80     // printf("score:%dn", 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("nametidtscoren");
104     // for(i = 0;i < 3 ; i++)
105     // {
106         // printf("%st%st%dn",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 }

 

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/thismajor/p/15366341.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

4091 9.8元 100元 0.98折
3577 0元 45元 限免