1. 程式人生 > 其它 >第六階段_typedef關鍵字詳解:建立一個classroom的結構體用來儲存一個班級 的資訊,所有學生的資訊,以及班級人數

第六階段_typedef關鍵字詳解:建立一個classroom的結構體用來儲存一個班級 的資訊,所有學生的資訊,以及班級人數

  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 }