帮我用c++写一个奖学金评选系统
时间: 2023-12-31 13:06:44 浏览: 31
好的,下面是一个简单的奖学金评选系统的C++代码,供您参考:
```c++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生结构体
struct Student {
string name; // 姓名
int score; // 成绩
int rank; // 排名
};
// 定义比较函数,用于对学生成绩进行排序
bool cmp(Student a, Student b) {
return a.score > b.score;
}
int main() {
int n; // 学生人数
vector<Student> students; // 学生列表
// 输入学生人数
cout << "请输入学生人数:" << endl;
cin >> n;
// 输入每个学生的姓名和成绩,并将其加入学生列表中
for (int i = 0; i < n; i++) {
Student student;
cout << "请输入第" << i+1 << "个学生的姓名和成绩:" << endl;
cin >> student.name >> student.score;
students.push_back(student);
}
// 对学生列表按照成绩进行排序
sort(students.begin(), students.end(), cmp);
// 给每个学生排名并输出结果
int rank = 1;
for (int i = 0; i < n; i++) {
students[i].rank = rank;
cout << students[i].name << "的成绩为" << students[i].score << ",排名为第" << rank << "名。" << endl;
if (i < n-1 && students[i].score != students[i+1].score) {
rank++;
}
}
return 0;
}
```
这个奖学金评选系统的实现很简单,只需要输入学生人数和每个学生的姓名和成绩,然后对学生列表按照成绩进行排序,给每个学生排名并输出结果即可。您可以根据自己的需求对代码进行修改和扩展。