#include "stdafx.h"
#include<iostream>
#include<cstdlib>
using namespace std;
//建立一个指针指向结构体
struct str
{
int num;
char name[50];
};
int main()
{
str STU[5];
STU[0].num = 100;
strcpy(STU[0].name,"张三");
STU[1].num = 99;
strcpy(STU[1].name,"李四");
STU[2].num = 98;
strcpy(STU[2].name,"王五");
STU[3].num = 89;
strcpy(STU[3].name,"老六");
STU[4].num = 1;
strcpy(STU[4].name,"王九");
//建立一个指针指向结构体
str *pstu[5] = {&STU[0],&STU[1],&STU[2],&STU[3],&STU[4]};
for (int i=0;i<5;i++)
{
for (int j=0;j<5-i-1;j++)
{
if (pstu[j]->num >pstu[j+1]->num)
{
str* temp = pstu[j];
pstu[j] = pstu[j+1];
pstu[j+1] = temp;
}
}
}
//数据显示
for (int i=0; i<5;i++)
{
cout<<pstu[i]->num<<" "<<pstu[i]->name<<endl;
}
system("pause");
return 0;
}
输出 结果:
1 王九
89 老六
98 王五
99 李四
100 张三
请按任意键继续. . .