有两个程序 不知道该怎么写 求帮忙解决 图一和图二.!帮忙看看吧 谢谢大家
先定义一个结构体,再定义一个函数用于排序就行
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
struct Student
{
char *name;
float source=0.0f;
};
int main()
{
vector<Student> res;
Student stu1;
stu1.name="No1";
stu1.source=56.5f;
Student stu2;
stu2.name="No2";
stu2.source=78.9f;
Student stu3;
stu3.name="No3";
stu3.source=45.0f;
Student stu4;
stu4.name="No4";
stu4.source=69.8f;
Student stu5;
stu5.name="No5";
stu5.source=89.0f;
Student stu6;
stu6.name="No6";
stu6.source=94.5f;
res.push_back(stu1);
res.push_back(stu2);
res.push_back(stu3);
res.push_back(stu4);
res.push_back(stu5);
res.push_back(stu6);
for(int i=0;i<res.size();++i)
{
for(int j=i+1;j<res.size();++j)
{
if(res[i].source<res[j].source)
{
swap(res[i],res[j]);
}
}
}
printf("排序后:\n");
for(int i=0;i<res.size();++i)
{
printf("第%d名:%s %.1f\n",i+1,res[i].name,res[i].source);
}
return 0;
}