以下程序用来计算任意个学生三门课的平均分并按平均分由高到低排序并打印输出。其中函数Input用来读取m个学生的信息,Total1函数用来计算总分和平均分,Sort函数按平均分由高到低排序,Print函数打印排序后的名次、、姓名、各科成绩及总分和平均分。请在主函数空白处填写适当内容将程序补充完整(分隔符请用英文输入法下的逗号填写不要有多余的空格,每一空白处占用一行填写后请换行)。#include
#include
struct student { long num; char name[20]; int Eng; int Com; int Mth;};void Input( struct student *p, int n);void Total1(struct student *p, int *psum, float *pave, int n);void Sort(struct student *p, int *psum, float *pave, int n);void Print(struct student *p, int *psum, float *pave, int n);main(){ struct student *p=NULL; int *psum=NULL; float *pave=NULL; int m; printf("请输入班级实际人数:\n"); scanf("%d",&m); p = ( struct student * )malloc( m*sizeof ( struct student )); psum = ( int * )malloc( m*sizeof ( int )); pave = ( float * )malloc( m*sizeof( float )); if( p ==NULL || psum == NULL || pave == NULL ) exit(0); Input( ); Total1( ); Sort( ); Print( );}void Input( struct student *p, int n){ int i; for(i=0;i
num, (p+i)->name,&(p+i)->Eng, &(p+i)->Com, &(p+i)->Mth ); }}void Total1(struct student *p,int *psum, float *pave,int n){ int i; for( i = 0; I < n; i++ ) { psum[i] = 0; psum[i] = psum[i] + p[i].Eng+p[i].Com + p[i].Mth; pave[i] =( float )psum[i]/3; }}void Sort( struct student *p, int *psum, float *pave, int n){ int i,j,iterm; float fterm; struct student pterm; for ( i = 0; i < n-1; i++ ) { for ( j = i+1; j < n; j++ ) { if ( pave[i] < pave[j] ) { iterm = psum[i]; psum[i] = psum[j]; psum[j] = iterm; fterm = pave[i]; pave[i] = pave[j]; pave[j] = fterm; pterm = p[i]; p[i] = p[j]; p[j] = pterm; } } }}void Print( struct student *p, int *psum, float *pave, int n){ int i; printf("名次\t%\t姓名\t英语\t计算机\t数学\t总分\t平均分\n"); for ( i = 0; i < n; i++ ) { printf("%d\t%ld\t%s\t%d\t%d\t%d\t%d\t%.0f\n",i+1, (p+i)->num, (p+i)->name, (p+i)->Eng, (p+i)->Com,(p+i)->Mth, psum[i], pave[i] ); }}