题目来源:大工慕课 链接
作者:Caleb Sung
本题中要用到的结构体类型的定义放在31.structure.h
文件中,31.CreateStruct.c
文件里放的是函数crtstruct
的定义,这个函数用来为结构体数组赋值,每个学生的8门成绩通过随机数得到并放在结构体的score成员数组里。本题需要完成如下任务:
(1)写一个函数ave()
来求每个学生的平均分,并把平均分放到结构体的average
成员里。
(2)写一个函数maxstd()
在屏幕上输出平均分最高的所有学生的姓名和平均成绩(注意:平均分如有并列最高则将所有人都输出来)。
#include <stdio.h>
#include <stdlib.h>
#include "31.CreateStruct.c"//#include "31.structure.h"
/* ERROR REPORT */
/*If this line exists then the compiler reports
"31.structure.h(6) : error C2371: 'STD' : redefinition; different basic types"
So I search some blogs,say that delete this line helps*/extern int crtstruct(STD std[],int n,int m);
/*************/
int ave(STD std[],int n);
void maxstd(STD std[],int n);
#define N 20
#define M 8
int main()
{int i,j;STD std[N];crtstruct(std,N,M);ave(std,N);for(i=0;i<N;i++){printf("%s %f ",std[i].name,std[i].average);for(j=0;j<M;j++){printf("%3d",std[i].score[j]);}putchar('n');}maxstd(std,N);return 0;
}
int ave(STD std[],int n)
{// 编写函数完成题目要求。/*********begin*********//**********end**********/
}
void maxstd(STD std[],int n)
{//编写函数完成题目要求。/*********begin*********//**********end**********/
}
#include "31.structure.h"
#include <stdlib.h>
#include <time.h>
int crtstruct(STD std[],int n,int m) //本函数用来为结构体数组赋值,n表示数组std的长度,m表示std->score 的长度
{int i,j;srand(time(NULL));for(i=0;i<n;i++){(std+i)->name[0]='A'+i;(std+i)->name[1]='a'+i+1;(std+i)->name[2]='