已知学生结构体如下:
struct student
{
int num;
char name[8];
char sex;
int age;
float grade;
};
要求按照学生成绩进行排序,输出排序后的结果。
#include < stdio.h >
#include < conio.h >
#include < string .h >
#define N 10
extern unsigned _floatconvert; /* 防止floating point formats not linked 错误发生 */
#pragma extref _floatconvert
typedef struct student
{
int num;
char name[ 8 ];
char sex;
int age;
float grade;
}STU;
STU stu[N] = {{ 101 , " Zhang " , ' M ' , 19 , 95.6 },
{ 102 , " Wang " , ' F ' , 18 , 92.4 },
{ 103 , " Zhao " , ' M ' , 19 , 85.7 },
{ 104 , " Li " , ' M ' , 20 , 96.3 },
{ 105 , " Gao " , ' M ' , 19 , 96.4 },
{ 106 , " Lin " , ' M ' , 18 , 91.5 },
{ 107 , " Ma " , ' F ' , 17 , 98.7 },
{ 108 , " Zhen " , ' M ' , 21 , 90.1 },
{ 109 , " Xu " , ' M ' , 19 , 89.8 },
{ 110 , " Mao " , ' F ' , 18 , 94.9 }};
void print(STU * p[])
{
int i;
printf( " num\tname\tsex\tage\tgrade\n " );
for (i = 0 ;i < N;i ++ )
{
printf( " %d\t%s\t%c\t%d\t%5.1f\n " ,p[i] -> num,p[i] -> name,p[i] -> sex,p[i] -> age,p[i] -> grade);
}
}
void gradebub(STU * p[])
{
STU * temp;
int i,j,flag;
for (i = 0 ;i < N - 1 ;i ++ )
{
flag = 0 ;
for (j = 0 ;j < N - i - 1 ;j ++ )
if (p[j] -> grade > p[j + 1 ] -> grade)
{
temp = p[j];p[j] = p[j + 1 ];p[j + 1 ] = temp;
flag = 1 ;
}
if (flag == 0 ) break ;
}
}
void main()
{
int i;
STU * p[N];
for (i = 0 ;i < N;i ++ )
p[i] =& stu[i];
print(p);
printf( " \n " );
gradebub(p);
print(p);
getch();
}
#include < conio.h >
#include < string .h >
#define N 10
extern unsigned _floatconvert; /* 防止floating point formats not linked 错误发生 */
#pragma extref _floatconvert
typedef struct student
{
int num;
char name[ 8 ];
char sex;
int age;
float grade;
}STU;
STU stu[N] = {{ 101 , " Zhang " , ' M ' , 19 , 95.6 },
{ 102 , " Wang " , ' F ' , 18 , 92.4 },
{ 103 , " Zhao " , ' M ' , 19 , 85.7 },
{ 104 , " Li " , ' M ' , 20 , 96.3 },
{ 105 , " Gao " , ' M ' , 19 , 96.4 },
{ 106 , " Lin " , ' M ' , 18 , 91.5 },
{ 107 , " Ma " , ' F ' , 17 , 98.7 },
{ 108 , " Zhen " , ' M ' , 21 , 90.1 },
{ 109 , " Xu " , ' M ' , 19 , 89.8 },
{ 110 , " Mao " , ' F ' , 18 , 94.9 }};
void print(STU * p[])
{
int i;
printf( " num\tname\tsex\tage\tgrade\n " );
for (i = 0 ;i < N;i ++ )
{
printf( " %d\t%s\t%c\t%d\t%5.1f\n " ,p[i] -> num,p[i] -> name,p[i] -> sex,p[i] -> age,p[i] -> grade);
}
}
void gradebub(STU * p[])
{
STU * temp;
int i,j,flag;
for (i = 0 ;i < N - 1 ;i ++ )
{
flag = 0 ;
for (j = 0 ;j < N - i - 1 ;j ++ )
if (p[j] -> grade > p[j + 1 ] -> grade)
{
temp = p[j];p[j] = p[j + 1 ];p[j + 1 ] = temp;
flag = 1 ;
}
if (flag == 0 ) break ;
}
}
void main()
{
int i;
STU * p[N];
for (i = 0 ;i < N;i ++ )
p[i] =& stu[i];
print(p);
printf( " \n " );
gradebub(p);
print(p);
getch();
}