- /* Write a program that fits the following recipe:
- Calculate the average score value for each structure and assign it to the proper member.
- Print the information in each structure.
- Print the class average for each of the numeric structure members.
- */
- #include <stdio.h>
- #define MAX 50
- #define CSIZE 4
- #define N 3
- struct name{
- char fname[MAX];
- char lname[MAX];
- };
- struct student{
- struct name stu;
- float grade[N];
- float average;
- };
- void get_input(struct student *); // get input from keyboard for score
- void calculate_avg(struct student *); // calculate the average score
- void display(struct student *); // display the content of the structures
- void display_avg(struct student *); // display the average of all the student
- int main(void)
- {
- struct student jmu[CSIZE]={
- {.stu={"wang","cheng"}},
- {.stu={"wu","baojia"}},
- {.stu={"chen","shaofeng"}},
- {.stu={"zhuo","hongyou"}}
- };
- get_input(jmu);
- calculate_avg(jmu);
- display(jmu);
- display_avg(jmu);
- return 0;
- }
- void get_input(struct student *pst)
- {
- int i,j;
- for(i=0;i<CSIZE;i++)
- {
- printf("Input the %d scores for %s%s.\\n",N,pst[i].stu.fname,pst[i].stu.lname);
- for(j=0;j<N;j++)
- scanf("%f",&pst[i].grade[j]);
- }
- }
- void calculate_avg(struct student *pst)
- {
- int i,j;
- float tot;
- for(i=0;i<CSIZE;i++)
- {
- for(tot=0,j=0;j<N;j++)
- tot+=pst[i].grade[j];
- pst[i].average=tot/N;
- }
- }
- void display(struct student *pst)
- {
- int i,j;
- for(i=0;i<CSIZE;i++)
- {
- printf("Name:%s%s, Score:",pst[i].stu.fname,pst[i].stu.lname);
- for(j=0;j<N;j++)
- printf("%2.f ",pst[i].grade[j]);
- printf(",Average:%.2f\\n",pst[i].average);
- }
- }
- void display_avg(struct student *pst)
- {
- int i;
- float total;
- for(total=0,i=0;i<CSIZE;i++)
- total+=pst[i].average;
- printf("\\nAll average: %.2f\\n",total/CSIZE);
- }
- //该片段来自于http://www.codesnippet.cn/detail/030920135577.html
来源: http://www.codesnippet.cn/detail/030920135577.html