程序代码:
#include<stdio.h>
#include<stdlib.h>
//void menu();
int ranking();
struct stu
{
int id; //学号
int total; //总分
int yuwen; //语文成绩
int math; //数学成绩
int English; //英语成绩
}students[500];
int main()
{
//menu();
ranking();
getchar();
system("pause");
return 0;
}
/****
void menu()
{
int num; //学生总人数
int i;
printf("输入学生总人数:");
scanf("%d",&num);
printf("\nPlease input students information:\n");
for(i=0;i<num;i++) //输入学生信息
{
printf("学号:");
scanf("%d",&students[i].id);
printf("总分");
scanf("%d",&students[i].total);
printf("语文成绩");
scanf("%d",&students[i].yuwen);
printf("数学成绩");
scanf("%d",&students[i].math);
printf("英语成绩");
scanf("%d",&students[i].English);
printf("\n");
}
ranking();
}
****/
int ranking()
{
struct stu temp;
int num; //学生总人数
int i,j,max;
printf("输入学生总人数:");
scanf("%d",&num);
printf("\nPlease input students information:\n");
for(i=0;i<num;i++) //输入学生信息
{
printf("学号:");
scanf("%d",&students[i].id);
printf("总分");
scanf("%d",&students[i].total);
printf("语文成绩");
scanf("%d",&students[i].yuwen);
printf("数学成绩");
scanf("%d",&students[i].math);
printf("英语成绩");
scanf("%d",&students[i].English);
printf("\n");
}
//通过总分来排序
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(students[i].total<students[j].total)
{
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
if(students[i].total=students[j].total) //总分相等情况,判断语文分数
{
if(students[i].yuwen<students[j].yuwen)
{
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
if(students[i].yuwen=students[j].yuwen) //语文分一样,判断数学分数
{
if(students[i].math<students[j].math)
{
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
if(students[i].English<students[j].English) //数学分一样,判断英语分数
{
temp=students[i];
students[i]=students[j];
students[j]=temp;
}
}
}
}
}
printf("排序之后的结果:\n");
for(i=0;i<num;i++)
printf("%d\t%d\t%d\t%d\t%d\t",students[i].id,students[i].total,students[i].yuwen,students[i].math,students[i].English);
return 0;
}