习题:编写一个学生成绩输出程序
1 /*编写一个学生成绩输出程序,要求:编写一个函数print,打印一个学生的成绩数组,该数组中有5个学生的数据记录,每个记录包括 2 num,name,score[3],主函数已有这些记录,用print函数输出这些记录。 3 */ 4 #include<stdio.h> 5 void print(struct Student *p ); 6 struct Student 7 { 8 int num; 9 char name[20]; 10 float score[3]; 11 }; 12 int main(void) 13 { 14 struct Student stu[5]={1010,"wang",60,70,80,1011,"zhang",70,71,72,1012,"zhao",80,81,82,1013,"xiao",90,91,92,1014,"yu",93,94,95}; 15 printf("学号\t姓名\t成绩\n"); 16 print(stu); 17 return 0; 18 } 19 void print(struct Student *p) 20 { 21 22 printf("%-4d\t%-4s\t%-4.2f\t%-4.2f\t%-4.2f\t\n",(*p).num,(*p).name,(*p).score[0],(*p).score[1],(*p).score[2]); 23 24 }
如果要输出5个学生的成绩,需要用到循环:
1 #include<stdio.h> 2 void print(struct Student *p ); 3 struct Student 4 { 5 int num; 6 char name[20]; 7 float score[3]; 8 }; 9 int main(void) 10 { 11 int i; 12 struct Student stu[5]={1010,"wang",60,70,80,1011,"zhang",70,71,72,1012,"zhao",80,81,82,1013,"xiao",90,91,92,1014,"yu",93,94,95}; 16 printf("学号\t姓名\t成绩\n"); 17 for (i=0;i<5;i++) 18 { 19 print(&stu[i]);21 } 22 return 0; 23 } 24 void print(struct Student *p) 25 { 26 27 printf("%-4d\t%-4s\t%-4.2f\t%-4.2f\t%-4.2f\t\n",(*p).num,(*p).name,(*p).score[0],(*p).score[1],(*p).score[2]); 28 29 }
浙公网安备 33010602011771号