c++定义结构体对学生成绩排序
#include <iostream>
#include<bits/stdc++.h>
#include<cstdlib>
using namespace std;
struct Student
{
char name[20];
int number;
int age;
char sex;
float score;
};
void print(Student *p[])
{
int i;
cout<<"姓名 学号 年龄 性别 分数"<<endl;
for(i=0;i<5;i++)
{
cout<<p[i]->name<<" "<<p[i]->number<<" "<<p[i]->age<<" "<<p[i]->sex<<" "<<p[i]->score<<endl;
}
cout<<endl;
}
void sort(Student *p[])
{
Student *temp;
int i,j,flag;
for(i=0;i<4;i++)
{
flag=0;
for(j=0;j<4-i;j++)
{
if(p[j]->score>p[j+1]->score)
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
flag=1;
}
}
if(flag==0)
break;
}
}
int main()
{
struct Student stu[5]={
{"aa",123,455,'x',17},
{"dd",123,455,'x',13},
{"cc",123,455,'x',14},
{"bb",123,455,'x',15},
{"ee",123,455,'x',16},
};
int i;
Student *p[5];
for(i=0;i<5;i++)
{
p[i]=&stu[i];
}
cout<<"排序前:"<<endl;
print(p);
cout<<"排序后:"<<endl;
sort(p);
print(p);
}