4.編一個(gè)程序,輸入下列學(xué)生成績表中的數(shù)據(jù),并用結(jié)構(gòu)體數(shù)組存放,然后統(tǒng)計(jì)并輸出三門課程的名稱和平均分?jǐn)?shù)。
name foxbase pascal c
wang 98.0 87.0 77.0
qian 90.5 91.0 88.0
sun 74.0 77.5 66.5
li 84.5 64.5 55.0
「解」類似上一程序,直接給出程序如下:
# include
# include
# define N 100
struct stu {
char namep[20];
int foxbase;
int pascal;
int c;
} students[N], *p;
void main ()
{ int n, sf, sb, sc;
char buf[120];
printf(“輸入n( %d) ! \n”, N);
do {
scanf(“%d”, n);
if(n 0 n N ) BREAK;
printf(“輸入 n不合理! 請重新輸入! \n”);
} while(1);
printf(“輸入%d個(gè)人的姓名、Foxbase成績、Pascal成績、C成績 \n”, n);
for( p= students;p students+n; p++);
scanf(“%s”,buf );strncpy(p - name, buf, 19);
scanf(“%d%d%d”, p - foxbase, p - pascal, p - c)
}
sf=sb=sc =0;
for( p=students; p STUDENTS+N; P++) {
sf+= p - foxbase; sb+= p - pascal; sc+= p - c;
}
printf(“Foxbase:%.2f \t Pascal:%.2f \t c:%.2f \n”,
(float)sf/n,(float)sb/n,(float)sc/n);
}