编辑代码

//1 创建数组结构体里面存放五个英雄
//2 怎么进行冒泡排序
#include <iostream>
#include <string>
using namespace std;
//创建结构体
struct Hero
{
   string name;
   int age;
   string sex; 
};
// //输入函数,进行初始化
// void foundh (struct Hero h[])
// {
//     for(int i=0;i<5;i++)
//     {  
//         //不能直接输入汉字是这个编译器的问题
//     cout<<"请输入姓名"<<endl;
//     cin>>h[i].name;
//     cout<<"请输入年龄"<<endl;
//     cin>>h[i].age;
//     cout<<"请输入性别"<<endl;
//     cin>>h[i].sex;
//     }
// }
//进行升序排列
void sortage (struct Hero h[])
{
    Hero l;
    for(int i=0;i<4;i++)
    {
        if(h[i].age>=h[i+1].age)
        {
          l=h[i+1];
          h[i+1]=h[i];
          h[i]=l;
        }
    }
}
//进行遍历输出
void printAll (struct Hero h[])
{
    for(int i=0;i<5;i++)
    {
        cout<<"姓名 "<<h[i].name;
        cout<<"年龄 "<<h[i].age;
        cout<<"性别 "<<h[i].sex<<endl;
    }
}
int main() {
    struct Hero h[5]=
{
   {"刘备",23,"男"},
   {"关羽",22,"男"},
   {"张飞",20,"男"},
   {"赵云",21,"男"},
   {"貂蝉",18,"女"},
}
    // foundh (h);
    sortage(h);
    printAll(h);
	return 0;
}