P159 作业第二题答案

#include <stdio.h>
#include <string.h>
struct stud_marks {char stud_name[20];
int marks;};
void main()
{
int i,j,temp;
char temp_name[20];
struct stud_marks s[5]={ {"ZhangSan",65},
{"LiSi",85},
{"WangWu",70},
{"ZhaoLiu",69},
{"ZhengQi",90} };
clrscr();
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
{ if(s[i].marks < s[j].marks)
{
temp=s[i].marks;
strcpy(temp_name, s[i].stud_name);
s[i].marks=s[j].marks;
strcpy(s[i].stud_name, s[j].stud_name);
s[j].marks=temp;
strcpy(s[j].stud_name, temp_name);
}
}
printf("/n TOP 3 Scores:");
for(i=0;i<3;i++)
printf("/n %s scored %d ", s[i].stud_name, s[i].marks);
getchar();
}

你可能感兴趣的:(作业)