哈工大威海資料結構實驗5
阿新 • • 發佈:2020-12-15
實驗五 排序
模擬EXCEL排序
直接呼叫了cpp庫函式sort的解法
la5.cpp
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
struct Student
{
long int number;
string name;
int score;
};
vector< Student> students;
//初始化
void initdata(int N)
{
for (int i = 0; i < N; i++)
{
Student temp;
cin >> temp.number;
cin >> temp.name;
cin >> temp.score;
students.push_back(temp);
}
}
//輸出結果
void OutPut()
{
cout << "output:" << endl;
for (int i = 0; i < students.size(); i++)
{
cout << setw(6) << setfill('0') << students[i].number << " ";
cout << students[i].name << " ";
cout << students[i].score << " " << endl;
}
}
//3種比較模式
//學號遞增
bool cmp_1(Student std_one, Student std_two)
{
return std_one.number < std_two.number;
}
//姓名非遞減排序
bool cmp_2(Student std_one, Student std_two)
{
if (std_one.name < std_two.name)
return std_one.name < std_two.name;
else if (std_one.name == std_two.name)
return std_one.number < std_two.number;
else
return 0;
}
//成績非遞減排序
bool cmp_3(Student std_one, Student std_two)
{
if (std_one.score < std_two.score)
return std_one.score < std_two.score;
else if (std_one.score == std_two.score)
return std_one.number < std_two.number;
else
return 0;
}
void QuickSort(int C)
{
if (C == 1)
sort(students.begin(), students.end(), cmp_1);
else if (C == 2)
sort(students.begin(), students.end(), cmp_2);
else if (C == 3)
sort(students.begin(), students.end(), cmp_3);
}
int main()
{
cout << "enter:" << endl;
int N, C;
cin >> N;
cin >> C;
initdata(N);
QuickSort(C);
OutPut();
}