#include <stdio.h>
typedef struct student
{
int id;
double math;
double english;
double chinese;
double computer;
} student;
int order[35];
double addsum[35];
double addaver[35];
student stu[35];
int stu_cnt = 0;
void input()
{
stu_cnt = 0;
while( stu_cnt < 35 )
{
printf( "输入学号(输入0退出输入):" );
scanf( "%d", &stu[stu_cnt].id );
fflush( stdin );
if ( stu[stu_cnt].id <= 0 )
break;
printf( "输入4门成绩(数学 英语 语文 计算机):" );
scanf( "%lf %lf %lf %lf", &stu[stu_cnt].math, &stu[stu_cnt].english,
&stu[stu_cnt].chinese, &stu[stu_cnt].computer );
fflush( stdin );
addsum[stu_cnt] = stu[stu_cnt].math + stu[stu_cnt].english +
stu[stu_cnt].chinese, stu[stu_cnt].computer;
addaver[stu_cnt] = addsum[stu_cnt] / 4.0;
order[stu_cnt] = stu[stu_cnt].id;
stu_cnt++;
}
}
int query( int id )
{
int i = 0;
for( i=0; i<stu_cnt; i++ )
{
if ( stu[i].id == id )
return i;
}
return -1;
}
void gen_order()
{
int i,j;
for( i=0; i<stu_cnt; i++ )
{
for( j=i+1; j<stu_cnt; j++ )
{
if ( addsum[i] - addsum[j] < 0.001 )
{
int temp_id = order[i];
order[i] = order[j];
order[j] = temp_id;
}
}
}
}
void output()
{
int i;
printf( "名次 学号 数学 英语 语文 计算机 总分 平均\n" );
for( i=0; i<stu_cnt; i++ )
{
int j = query( order[i] );
if ( j<0 )
{
printf( "order[%d]= %d error\n", i+1, order[i] );
continue;
}
printf( "%02d %5d %4.2f %4.2f %4.2f %6.2f %4.2f %4.2f\n",
i+1, stu[j].id, stu[j].math, stu[j].english, stu[j].chinese, stu[j].computer,
addsum[j], addaver[j] );
}
}
int main()
{
input();
gen_order();
output();
}
温馨提示:答案为网友推荐,仅供参考