p421.5
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define CSIZE 4 4 #define SCORE 3 5 6 void get_info(struct students *p); 7 void get_average(struct students *p); 8 void print_info(struct students *p); 9 void print_class_average(struct students *p); 10 11 struct names{ 12 char firstname[40]; 13 char lastname[40]; 14 }; 15 16 struct students{ 17 struct names name; 18 float grade[SCORE]; 19 float average; 20 }; 21 22 int main(void) 23 { 24 struct students sheet[CSIZE]={ 25 {{"absbs", "bdcdc"}, 0, 0, 0, 0}, 26 {{"cfvfv", "dfgfg"}, 0, 0, 0, 0}, 27 {{"enmnm", "fcvcv"}, 0, 0, 0 ,0}, 28 {{"ghbhb", "hiyiy"}, 0, 0, 0, 0} 29 }; 30 31 32 get_info(sheet); 33 get_average(sheet); 34 print_info(sheet); 35 print_info(sheet); 36 37 system("pause"); 38 return 0; 39 } 40 41 void get_info(struct students *p) 42 { 43 int i,j; 44 for(i=0; i<CSIZE; i++){ 45 puts("please input student name"); 46 scanf("%s%s",&((p+i)->name.firstname),&((p+i)->name.lastname)); 47 puts("input the score"); 48 for(j=0; j<SCORE; j++) 49 scanf("%f",&((p+i)->grade[j])); 50 } 51 } 52 53 void get_average(struct students *p) 54 { 55 float sum; 56 int i, j; 57 for(i=0; i<CSIZE; i++){ 58 for(j=0, sum=0; j<SCORE; j++) 59 sum+=((p+i)->grade[j]); 60 (p+i)->average=sum/SCORE; 61 } 62 } 63 64 void print_info(struct students *p) 65 { 66 int i, j; 67 for(i=0; i<CSIZE; i++){ 68 printf("%s\t%s\n", (p+i)->name.firstname, (p+i)->name.lastname); 69 printf("grade:\n"); 70 for(j=0; j<SCORE; j++) 71 printf("%f ",(p+i)->grade[j]); 72 printf("\n"); 73 printf("the average grade is %.2f", (p+i)->average); 74 } 75 } 76 77 void print_class_average(struct students *p) 78 { 79 int i; 80 float sum=0; 81 82 for(i=0; i<CSIZE; i++) 83 sum+=(p+i)->average; 84 85 printf("the class average grade is %.2f", sum/CSIZE); 86 }
时间: 2024-11-11 06:18:27