qfedu-cpp-level/day9/stl_stack_demo/d2_2.cpp

158 lines
3.9 KiB
C++
Raw Permalink Normal View History

#include <iostream>
#include <vector>
#include <stack>
using namespace std;
class Worker;
class Date
{
friend Worker;
friend ostream &operator<<(ostream &cout, Date &date)
{
cout << date.year << "" << date.month << "" << date.day << "";
return cout;
}
private:
int year;
int month;
int day;
public:
Date(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
bool operator>(Date &other)
{
// cout << *this << " pk " << other << endl;
return year > other.year || (year == other.year && month > other.month) || (year == other.year && month == other.month && day > other.day);
}
bool operator<=(Date &other)
{
return year <= other.year && month <= other.month && day <= other.day;
}
};
class Worker
{
public:
string name; // 姓名
Date *hire_date; // 入职时间
double volume; // 业绩值
public:
Worker(const string &name, Date *hire_date, double volume)
{
this->name = name;
this->hire_date = hire_date;
this->volume = volume;
}
Worker(const Worker &other)
{
Date *hire_date = new Date(other.hire_date->year, other.hire_date->month, other.hire_date->day);
this->hire_date = hire_date;
this->name = other.name;
this->volume = other.volume;
}
Worker &operator=(const Worker &other)
{
this->hire_date->year = other.hire_date->year;
this->hire_date->month = other.hire_date->month;
this->hire_date->day = other.hire_date->day;
this->name = other.name;
this->volume = other.volume;
return *this;
}
~Worker()
{
if (hire_date)
delete hire_date;
hire_date = NULL;
}
void show()
{
cout << "name=" << name << ",hire_date=" << *hire_date << ",volume=" << volume << endl;
}
};
void sortByHireDate(Worker *p, int size)
{
for (int i = 0; i < size - 1; i++)
{
int min = i;
for (int j = i + 1; j < size; j++)
{
if (*(p[min].hire_date) > *(p[j].hire_date))
{
min = j;
}
}
if (min != i)
{
// cout << p[i].name << " 和 " << p[min].name << " 交换数据" << endl;
Worker tmp = p[i]; // 拷贝构造函数
p[i] = p[min]; // operator=重载
p[min] = tmp;
}
}
}
void sortByVolume(Worker *p, int size)
{
for (int i = 0; i < size - 1; i++)
{
int min = i;
for (int j = i + 1; j < size; j++)
{
if (p[min].volume < p[j].volume)
{
min = j;
}
}
if (min != i)
{
// cout << p[i].name << " 和 " << p[min].name << " 交换数据" << endl;
Worker tmp = p[i]; // 拷贝构造函数
p[i] = p[min]; // operator=重载
p[min] = tmp;
}
}
}
int main(int argc, char const *argv[])
{
Worker w1("disen", new Date(2020, 1, 15), 1000);
Worker w2("jack", new Date(2021, 1, 15), 100);
Worker w3("lucy", new Date(2020, 2, 15), 500);
Worker w4("mack", new Date(2020, 3, 25), 300);
Worker w5("judy", new Date(2022, 5, 18), 400);
Worker w6("rose", new Date(2023, 1, 10), 1200);
Worker m[] = {w1, w2, w3, w4, w5, w6};
// sortByHireDate(m, 6);
sortByVolume(m, 6);
vector<Worker> v1(m, m + 6);
stack<Worker> s1;
vector<Worker>::iterator it = v1.begin();
while (it != v1.end())
{
(*it).show();
s1.push(*it);
it++;
}
// 辞退2位新来的员工
// cout << "-----辞退2位新来的员工----" << endl;
cout << "-----业绩最差的2位员工----" << endl;
for (int i = 0; i < 2; i++)
{
s1.top().show();
s1.pop();
}
return 0;
}