c++管理系统实现排序
时间: 2023-08-02 17:06:22 浏览: 73
C++中实现排序可以使用STL库中的sort函数,也可以自行实现排序算法。以下是使用STL库中sort函数进行排序的示例代码:
```c++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {5, 2, 8, 3, 1, 9, 4, 6, 7};
int n = sizeof(arr) / sizeof(int);
sort(arr, arr + n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
该程序中,我们定义了一个整数数组arr,并使用sort函数对其进行升序排序。最终输出排序后的数组。
如果需要实现其他排序算法,可以自行实现,例如冒泡排序、快速排序、归并排序等。
相关问题
c++工资管理系统员工排序
可以使用C++中的结构体和排序算法来实现员工排序。首先,定义一个结构体来存储员工信息,包括姓名、工号、工资等属性。然后,使用数组来存储所有员工的信息,并根据需要实现不同的排序算法。例如,可以使用快速排序、归并排序或插入排序等算法进行排序。排序时,按照不同的属性进行排序,例如按照工资从高到低排序,或者按照姓名的字母顺序排序等。最后,将排序后的员工信息输出即可。
以下是示例代码:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Employee {
string name;
int id;
double salary;
};
bool compareSalary(Employee e1, Employee e2) {
return e1.salary > e2.salary;
}
bool compareName(Employee e1, Employee e2) {
return e1.name < e2.name;
}
int main() {
const int size = 5;
Employee employees[size];
// 读入员工信息
for (int i = 0; i < size; i++) {
cout << "请输入第" << i + 1 << "个员工的姓名、工号和工资:";
cin >> employees[i].name >> employees[i].id >> employees[i].salary;
}
// 按照工资排序
sort(employees, employees + size, compareSalary);
// 输出排序结果
cout << "按照工资从高到低排序的员工信息:" << endl;
for (int i = 0; i < size; i++) {
cout << employees[i].name << " " << employees[i].id << " " << employees[i].salary << endl;
}
// 按照姓名排序
sort(employees, employees + size, compareName);
// 输出排序结果
cout << "按照姓名的字母顺序排序的员工信息:" << endl;
for (int i = 0; i < size; i++) {
cout << employees[i].name << " " << employees[i].id << " " << employees[i].salary << endl;
}
return 0;
}
```
这个例子中,我们定义了一个`Employee`结构体来存储员工信息。然后,我们定义了两个比较函数`compareSalary`和`compareName`,分别用于按照工资和姓名排序。在`main`函数中,我们读入员工信息,然后按照工资和姓名分别进行排序,并输出排序结果。
实现C++学生成绩管理系统
下面是一个简单的C++学生成绩管理系统的实现,包括学生信息的录入和查询,以及成绩的统计和排序:
```C++
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name; // 学生姓名
int id; // 学生学号
int score; // 学生分数
};
vector<Student> students; // 学生信息列表
// 录入学生信息
void input() {
while (true) {
cout << "请输入学生信息(姓名 学号 分数),输入 q 退出录入:" << endl;
string name;
int id, score;
cin >> name;
if (name == "q") {
break;
}
cin >> id >> score;
students.push_back({name, id, score});
}
}
// 查询学生信息
void query() {
while (true) {
cout << "请输入要查询的学生学号,输入 0 退出查询:" << endl;
int id;
cin >> id;
if (id == 0) {
break;
}
auto it = find_if(students.begin(), students.end(), [id](const Student& s) { return s.id == id; });
if (it == students.end()) {
cout << "未找到该学生信息!" << endl;
} else {
cout << "姓名:" << it->name << " 学号:" << it->id << " 分数:" << it->score << endl;
}
}
}
// 统计学生信息
void statistics() {
int total_score = 0;
for (const auto& s : students) {
total_score += s.score;
}
cout << "总人数:" << students.size() << " 平均分数:" << total_score / students.size() << endl;
}
// 排序学生信息
void sort() {
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { return a.score > b.score; });
cout << "按分数排序结果:" << endl;
for (const auto& s : students) {
cout << "姓名:" << s.name << " 学号:" << s.id << " 分数:" << s.score << endl;
}
}
int main() {
input();
query();
statistics();
sort();
return 0;
}
```
以上代码实现了一个简单的学生成绩管理系统,可以根据需要进行修改和扩展。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)