66 date:2021.3.6
要点:
结构体类型成员的运算;
指向结构体类型的指针变量作函数参数;
详细代码如下:
#include <stdio.h>
#define N 8
typedef struct
{ char num[10];
double s[N];
double ave;
} STREC;
void fun(STREC *a)
{
/*
analyse:
求该学生平均分;
*/
/* Method1:
int i;
for( i = 0; i < N; i++)
a->ave = a->ave + a->s[i];
a->ave/=N;
*/
// Method2:
int i;
double sum = 0.0;
for(i = 0; i < N; i++)
{
sum += a->s[i];
}
a->ave = sum / N;
/* ERROR:
STREC *p = a->num;
double ave = 0.0,sum = 0.0;
while(a->s != NULL)
{
sum += *a ->s;
*a->num;
}
a->ave = sum / N;
*/
}
void main()
{ STREC s={"GA005",85.5,76,69.5,85,91,72,64.5,87.5};
int i;
void NONO ( );
fun( &s );
printf("The %s's student data:\n", s.num);
for(i=0;i<N; i++)
printf("%4.1f\n",s.s[i]);
printf("\nave=%7.3f\n",s.ave);
NONO();
}