//20221017 结构体学习记录2-成绩单排序
#include<iostream>
#include<string> //字符串处理
//#include<ctime> //随机数生成
using namespace std;
struct student //制定一个student结构体,此处struct不可省略,下面可省
{
string name;
int score;
};
void sort(student* arr,int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - 1 - i; j++)
{
if (arr[j].score>arr[j+1].score)
{
student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
student list[5] =
{
{"AA",75},
{"BB",62},
{"CC",60},
{"DD",85},
{"EE",90}
};
int len = sizeof(list) / sizeof(list[0]);
cout << "排序前分数:" << endl;
for (int i = 0; i < len; i++)
{
cout << list[i].name << " " << list[i].score << endl;
}
sort(list, len);
cout << endl<< "排序后分数:" << endl;
for (int i = 0; i < len; i++)
{
cout << list[i].name << " " << list[i].score << endl;
}
system("pause");
return 0;
}
运行结果:
有问题欢迎一起学习交流,邮箱:bglei@foxmail.com