142 lines
3.1 KiB
C++
142 lines
3.1 KiB
C++
|
#include <iostream>
|
||
|
#include <stack>
|
||
|
#include <vector>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
class Date
|
||
|
{
|
||
|
// 重载 << 以显示日期
|
||
|
friend ostream &operator<<(ostream &out, const Date &date)
|
||
|
{
|
||
|
out << date.year << "年" << date.month << "月" << date.day << "日";
|
||
|
return out;
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
int year;
|
||
|
int month;
|
||
|
int day;
|
||
|
|
||
|
public:
|
||
|
Date(int y, int m, int d) : year(y), month(m), day(d) {}
|
||
|
|
||
|
public:
|
||
|
bool operator<(const Date &date) const
|
||
|
{
|
||
|
// if (year < date.year)
|
||
|
// {
|
||
|
// return true;
|
||
|
// }
|
||
|
// else if (year == date.year)
|
||
|
// {
|
||
|
// if (month < date.month)
|
||
|
// {
|
||
|
// return true;
|
||
|
// }
|
||
|
// else if (month == date.month)
|
||
|
// {
|
||
|
// if (day < date.day)
|
||
|
// {
|
||
|
// return true;
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// return false;
|
||
|
return year < date.year ||
|
||
|
(year == date.year && month < date.month) ||
|
||
|
(year == date.year && month == date.month && day < date.day);
|
||
|
}
|
||
|
bool operator>(const Date &date) const
|
||
|
{
|
||
|
return year > date.year && month > date.month && day > date.day;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
class Worker
|
||
|
{
|
||
|
friend Worker &operator=(const Worker &other);
|
||
|
// {
|
||
|
// if (this != &other)
|
||
|
// {
|
||
|
// name = other.name;
|
||
|
// hire_date = other.hire_date;
|
||
|
// volume = other.volume;
|
||
|
// }
|
||
|
// return *this;
|
||
|
// }
|
||
|
|
||
|
private:
|
||
|
string name; // 姓名
|
||
|
Date hire_date; // 入职日期
|
||
|
double volume; // 业绩
|
||
|
|
||
|
public:
|
||
|
Worker(const string &name, const Date &hire_date, double volume)
|
||
|
: name(name), hire_date(hire_date), volume(volume) {}
|
||
|
Worker(const Worker &other)
|
||
|
{
|
||
|
name = other.name;
|
||
|
hire_date = other.hire_date;
|
||
|
volume = other.volume;
|
||
|
}
|
||
|
~Worker()
|
||
|
{
|
||
|
cout << "析构函数被调用" << endl;
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
void show()
|
||
|
{
|
||
|
cout << "name = " << name << ", hire_date = " << hire_date << ", volume = " << volume << endl;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
Worker &operator=(const Worker &other)
|
||
|
{
|
||
|
name = other.name;
|
||
|
hire_date = other.hire_date;
|
||
|
volume = other.volume;
|
||
|
}
|
||
|
|
||
|
void sortByHireDate(Worker *p, int size)
|
||
|
{
|
||
|
for (int i = 0; i < size - 1; i++)
|
||
|
int min = i;
|
||
|
for (int j = i + 1; j < size; i++)
|
||
|
{
|
||
|
if (p[min].hire_date > p[j].hire_date)
|
||
|
{
|
||
|
min = j;
|
||
|
}
|
||
|
}
|
||
|
if (min != i)
|
||
|
{
|
||
|
swap(p[min], p[i]);
|
||
|
// Worker tmp = p[i]; // 拷贝构造
|
||
|
// p[i] = p[min]; // operator= 重载
|
||
|
// p[min] = tmp;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void sortByVolume(Worker *p, int size)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
Worker w1("disen", Date(2020, 1, 15), 1000);
|
||
|
Worker w2("zhangsan", Date(2020, 1, 16), 200);
|
||
|
Worker w3("lisi", Date(2020, 2, 17), 400);
|
||
|
Worker w4("wangwu", Date(2020, 2, 18), 900);
|
||
|
Worker w5("zhaoliu", Date(2020, 2, 18), 100);
|
||
|
|
||
|
Worker wrks[] = {w1, w2, w3, w4, w5};
|
||
|
|
||
|
sortByHireDate(wrks, 5); // 先排序
|
||
|
|
||
|
// vector<Worker> vec;
|
||
|
// vec.assign(wrks, wrks + 5);
|
||
|
|
||
|
w1.show();
|
||
|
}
|