// 定义被管理的结构体
// define.h
#pragma once
#include <memory>
#include <string>
#include <iostream>
using namespace std;
const int NAME_MAX_LEN = 50;
struct SData;
struct SData {
SData(int nId = 0, const char* strName = "\0", double fPay = 0.00f)
: id(nId), str("\0"), pay(fPay) { strcpy_s(str, NAME_MAX_LEN, strName); }
SData(const SData* pData)
:id(pData->id), str("\0"), pay(pData->pay) { strcpy_s(str, NAME_MAX_LEN, pData->str); }
~SData() {
printf("auto release the data that alloced on heap\n%-10d %-10s %-10.2lf\n",
this->id, this->str, this->pay);
}
static void print(const shared_ptr<SData>& pData) {
printf("%-10d %-10s %-10.2lf\n", pData.get()->id, pData.get()->str, pData.get()->pay);
}
int id;
char str[NAME_MAX_LEN];
double pay;
};
// 定义管理结构体的对象
// CManager.h
#pragma once
#include <list>
#include <string>
#include <string.h>
#include <memory>
#include <algorithm>
#include "define.h"
#include "CDelete.h"
#include "CFind.h"
#include "CModify.h"
#include "CSort.h"
using namespace std;
class CManager {
public:
CManager();
list<shared_ptr<SData>>& GetEntry() { return m_list; }
int Start();
void print() const { PrintTable(), PrintAll(); }
private:
void PrintTable() const;
void PrintAll() const { for_each(m_list.begin(), m_list.end(), SData::print); system("pause"); }
int Menu();
// static void ReleaseInfo(shared_ptr<SData> pData) { }
// void RemoveAll() const { for_each(m_list.begin(), m_list.end(), ReleaseInfo); }
void Load();
void Save();
void Input();
void Output();
void Remove();
void Find();
void Modify();
void Sort();
list<shared_ptr<SData>> m_list;
CDelete m_delete;
CFind m_find;
CModify m_modify;
CSort m_sort;
};
// CManager.cpp
#include "CManager.h"
#include "CInput.h"
#include <iostream>
using namespace std;
CManager::CManager()
: m_delete(this, GetEntry()), m_find(this, GetEntry()),
m_modify(this, GetEntry()), m_sort(this, GetEntry())
{
}
int CManager::Start()
{
Load();
int n;
while (n = Menu()) {
switch (n) {
case 1:
Output();
break;
case 2:
Input();
break;
case 3:
Remove();
break;
case 4:
Modify();
break;
case 5:
Find();
break;
case 6:
Sort();
break;
}
}
Save();
return 0;
}
int CManager::Menu()
{
system("cls");
puts("\t***************************");
puts("\t* 1、浏览所有信息 *");
puts("\t* 2、添加信息 *");
puts("\t* 3、删除信息 *");
puts("\t* 4、修改信息 *");
puts("\t* 5、查找信息 *");
puts("\t* 6、排序 *");
puts("\t* 0、退出 *");
puts("\t***************************");
std::cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
return i;
}
void CManager::PrintTable() const
{
if (m_list.empty()) {
puts("记录中没有任何信息");
system("pause");
return;
}
printf("%-10s %-10s %-10s\n", "编号", "姓名", "工资");
}
void CManager::Load()
{
FILE* fp;
errno_t err = fopen_s(&fp, "test.bin", "rb");
if (err != 0 || fp == nullptr) {
perror("错误: ");
return;
}
int n = 0;
if (fread(&n, sizeof(int), 1, fp) < 1 || n == 0) {
perror("\t错误");
fclose(fp);
return;
}
SData data;
for (int i = 0; i < n; i++) {
if (fread((void*)&data, sizeof(SData), 1, fp) < 1) {
perror("\t错误");
fclose(fp);
return;
}
m_list.emplace_back(make_shared<SData>(new SData(&data)));
}
fclose(fp);
}
void CManager::Save()
{
FILE* fp = nullptr;
errno_t err = fopen_s(&fp, "test.bin", "wb");
if (err != 0 || fp == nullptr) {
perror("\t错误");
return;
}
if (m_list.empty()) return;
int n = m_list.size();
if (fwrite((void*)&n, sizeof(int), 1, fp) < 1) {
perror("\t错误");
fclose(fp);
return;
}
for (auto i : m_list) {
if (i == nullptr) continue;
if (fwrite((void*)(&(*i)), sizeof(SData), 1, fp) < 1) {
perror("\t错误");
fclose(fp);
return;
}
}
fclose(fp);
}
void CManager::Input()
{
CInput in;
int nId = in.InputId(m_list, true);
string str = in.InputName(m_list);
double fPay = in.InputPay(m_list);
SData data(nId, str.c_str(), fPay);
m_list.emplace_back(make_shared<SData>(new SData(&data)));
}
void CManager::Output()
{
PrintTable();
PrintAll();
}
void CManager::Remove()
{
m_delete.Start();
}
void CManager::Find()
{
m_find.Start();
}
void CManager::Modify()
{
m_modify.Start();
}
void CManager::Sort()
{
m_sort.Start();
}
// 增加模块
// CInput.h
#pragma once
#include <string>
#include "CManager.h"
using namespace std;
class CInput
{
public:
bool CheckIdExist(list<shared_ptr<SData>>& list, int nId);
int InputId(list<shared_ptr<SData>>& list, bool bCheckId = false);
string InputName(list<shared_ptr<SData>>& list);
double InputPay(list<shared_ptr<SData>>& list);
};
// CInput.cpp
#include "CInput.h"
#include "CManager.h"
#include <list>
#include <iostream>
#include <string>
bool CInput::CheckIdExist(list<shared_ptr<SData>>& list, int nId)
{
for (auto i : list)
if (i->id == nId) return true;
return false;
}
int CInput::InputId(list<shared_ptr<SData>>& list, bool bCheckId)
{
bool valid = true;
cout << "Please, enter staff's id: ";
string strId;
LOOP:
rewind(stdin);
getline(std::cin, strId);
for (string::size_type i = 0; i < strId.size(); i++) {
if (isalpha(strId[i]) || ispunct(strId[i])) {
cout << "Enter staff's id whith alpha or punctuations" << std::endl;
valid = false;
goto LOOP;
}
}
if (bCheckId == true && CheckIdExist(list, stoi(strId)) == true)
goto LOOP;
if (valid == false) {
valid = true;
strId.clear();
goto LOOP;
}
return stoi(strId);
}
string CInput::InputName(list<shared_ptr<SData>>& list)
{
bool valid = true;
cout << "Please, enter staff's name: ";
string strName;
LOOP:
rewind(stdin);
getline(std::cin, strName);
for (string::size_type i = 0; i < strName.size(); i++) {
if (isdigit(strName[i]) || ispunct(strName[i])) {
std::cout << "Enter staff's name whith number or punctuations" << std::endl;
valid = false;
goto LOOP;
}
}
if (valid == false) {
valid = true;
strName.clear();
goto LOOP;
}
return strName;
}
double CInput::InputPay(list<shared_ptr<SData>>& list)
{
cout << "Please, enter staff's pay: (range >= 5000.00 and <= 1000000.00)" << std::endl;
double fPay;
LOOP:
cin >> fPay;
rewind(stdin);
if (fPay < 5000.00f || fPay > 1000000.00f) {
cout << "Enter staff's pay whith out of range" << std::endl;
goto LOOP;
}
return fPay;
}
// 删除模块
// CDelete.h
#pragma once
#include <list>
#include <memory>
#include "define.h"
#include "CFind.h"
using namespace std;
class CManager;
class CDelete
{
public:
CDelete(const CManager* pCtrl, list<shared_ptr<SData>>& list)
: m_pCtrl(pCtrl), m_list(list) { }
int Start();
private:
int Menu();
void RemoveId();
void RemoveName();
void RemovePay();
private:
const CManager* m_pCtrl;
list<shared_ptr<SData>>& m_list;
};
// CDelete.cpp
#include "CInput.h"
#include "CDelete.h"
#include "CManager.h"
#include <iostream>
#include <string.h>
using namespace std;
int CDelete::Start()
{
int n;
while ((n = Menu()) != 0) {
switch (n)
{
case 1:
RemoveId();
break;
case 2:
RemoveName();
break;
case 3:
RemovePay();
break;
}
}
return 0;
}
int CDelete::Menu()
{
system("cls");
puts("\t***************************");
puts("\t* 删 除 菜 单 *");
puts("\t* 1、按员工编号 *");
puts("\t* 2、按姓名 *");
puts("\t* 3、按工资范围 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
return i;
}
void CDelete::RemoveId()
{
while (1)
{
system("cls");
puts("\t***********************************");
puts("\t* 编 号 删 除 菜 单 *");
puts("\t* 1、删除小于输入编号的信息 *");
puts("\t* 2、删除等于输入编号的信息 *");
puts("\t* 3、删除大于输入编号的信息 *");
puts("\t* 0、退出 *");
puts("\t***********************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
if (i == 0) return;
CInput in;
if (i == 1) {
Less<int> lessId(in.InputId(m_list));
m_list.remove_if(lessId);
}
else if (i == 2) {
Equal<int> equalId(in.InputId(m_list));
m_list.remove_if(equalId);
}
else if (i == 3) {
Greater<int> greaterId(in.InputId(m_list));
m_list.remove_if(greaterId);
}
m_pCtrl->print();
}
}
void CDelete::RemoveName()
{
while (1)
{
system("cls");
puts("\t***************************");
puts("\t* 姓 名 删 除 菜 单 *");
puts("\t* 1、按姓名精确删除 *");
puts("\t* 2、按姓名模糊删除 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
if (i == 0) return;
CInput in;
if (i == 1) {
Equal<const string&> equalName(in.InputName(m_list));
m_list.remove_if(equalName);
}
else if (i == 2) {
Equal<const string&> equalNameDim(in.InputName(m_list), EVarDim::dim);
m_list.remove_if(equalNameDim);
}
m_pCtrl->print();
}
}
void CDelete::RemovePay()
{
while (1)
{
system("cls");
puts("\t*******************************");
puts("\t* 工 资 删 除 菜 单 *");
puts("\t* 1、精确工资删除一人 *");
puts("\t* 2、删除范围内的所有人 *");
puts("\t* 0、退出 *");
puts("\t*******************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
if (i == 0) return;
CInput low, up;
if (i == 1) {
Equal<double> equalPay(low.InputPay(m_list), EVarType::pay);
m_list.remove_if(equalPay);
}
else if (i == 2) {
Range<double> rangePay(low.InputPay(m_list), up.InputPay(m_list), EVarType::pay);
m_list.remove_if(rangePay);
}
m_pCtrl->print();
}
}
// 查找模块
// CFind.h
#pragma once
#include <list>
#include <memory>
#include <string>
#include <string.h>
#include "define.h"
using namespace std;
class CManager;
enum class EVarType { nothing = 0, id = 1, name = 2, pay = 3 };
enum class EVarDim { nothing = 0, exact = 1, dim = 2 };
enum class EFindOrModify { find = 0, modify = 1 };
template<typename T> class Less {
public:
Less(const T& data, EVarType var = EVarType::id)
: m_data(data), m_var(var) { }
bool operator()(const shared_ptr<SData>& rData) {
if (m_var == EVarType::id) return (*rData).id < m_data;
else if (m_var == EVarType::pay) return (*rData).pay < m_data;
return false;
}
private:
T m_data;
EVarType m_var;
};
template<typename T> class Equal {
public:
Equal(const T& data, EVarType var = EVarType::id)
: m_data(data), m_var(var) { }
bool operator()(const shared_ptr<SData>& rData) {
if (m_var == EVarType::id) return (*rData).id == m_data;
else if (m_var == EVarType::pay) return (*rData).pay == m_data;
return false;
}
private:
T m_data;
EVarType m_var;
};
// 错误的谓词实现方式
/*template<> class Equal<const char*> {
public:
Equal(const char* data, EVarDim dim = EVarDim::exact)
: m_data(nullptr), m_dim(dim) {
m_data = new char[strlen(data) + 1];
strcpy_s(m_data, strlen(data) + 1, data);
}
~Equal() { delete[]m_data; }
bool operator()(const shared_ptr<SData>& rData) {
if (m_dim == EVarDim::exact) return strcmp((const char*)(*rData).str, (const char*)m_data) == 0;
else return strstr((const char*)(*rData).str, (const char*)m_data) != nullptr;
return false;
}
private:
char* m_data;
EVarDim m_dim;
}; */
/* 不提供深拷贝函数的实现方式
template<> class Equal<const char*> {
public:
Equal(const char* data, EVarDim dim = EVarDim::exact)
: m_data(nullptr), m_dim(dim), m_iscopy(false) {
m_data = new char[strlen(data) + 1];
strcpy_s(m_data, strlen(data) + 1, data);
}
Equal(const Equal& copy)
: m_data(copy.m_data), m_dim(copy.m_dim), m_iscopy(true) { }
~Equal() {
if (m_iscopy == false && m_data != nullptr) {
delete[]m_data;
m_data = nullptr;
}
}
bool operator()(const shared_ptr<SData>& rData) {
if (m_dim == EVarDim::exact)
return strcmp((const char*)(*rData).str, (const char*)m_data) == 0;
else
return strstr((const char*)(*rData).str, (const char*)m_data) != nullptr;
return false;
}
private:
char* m_data;
EVarDim m_dim;
bool m_iscopy;
}; */
template<> class Equal<const string&> {
public:
Equal(const string& str, EVarDim dim = EVarDim::exact)
: m_str(str), m_dim(dim) {
}
bool operator()(const shared_ptr<SData>& rData) {
if (m_dim == EVarDim::exact) return strcmp((const char*)(*rData).str, m_str.c_str()) == 0;
else return strstr((const char*)(*rData).str, (const char*)m_str.c_str()) != nullptr;
return false;
}
private:
const string& m_str;
EVarDim m_dim;
};
template<typename T> class Greater {
public:
Greater(const T& data, EVarType var = EVarType::id)
: m_data(data), m_var(var) { }
bool operator()(const shared_ptr<SData>& rData) {
if (m_var == EVarType::id) return (*rData).id > m_data;
else if (m_var == EVarType::pay) return (*rData).pay > m_data;
return false;
}
private:
T m_data;
EVarType m_var;
};
template<typename T> class Range { // 暂时不添加姓名的范围查找以及范围内的模糊查找
public:
Range(const T& low, const T& up, EVarType var = EVarType::id)
: m_low(low), m_up(up), m_var(var) { }
bool operator()(const shared_ptr<SData>& rData) {
if(m_var == EVarType::id) return ((*rData).id >= m_low) && ((*rData).id <= m_up) == true;
else if(m_var == EVarType::pay) return ((*rData).pay >= m_low) && ((*rData).pay <= m_up) == true;
return false;
}
private:
T m_low, m_up;
EVarType m_var;
};
class CFind
{
public:
CFind(const CManager* pCtrl, list<shared_ptr<SData>>& list)
: m_pCtrl(pCtrl), m_list(list) { }
public:
int Start();
int Menu();
void FindId();
void FindName();
void FindPay();
private:
const CManager* m_pCtrl;
list<shared_ptr<SData>>& m_list;
};
// CFind.cpp
#include "CFind.h"
#include "CInput.h"
#include <iostream>
int CFind::Start()
{
int n;
while ((n = Menu()) != 0) {
switch (n)
{
case 1:
FindId();
break;
case 2:
FindName();
break;
case 3:
FindPay();
break;
}
}
return 0;
}
int CFind::Menu()
{
system("cls");
puts("\t***************************");
puts("\t* 查 找 菜 单 *");
puts("\t* 1、按员工编号 *");
puts("\t* 2、按姓名 *");
puts("\t* 3、按工资范围 *");
puts("\t* 0、退出 *");
puts("\t***************************");
std::cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
return i;
}
void CFind::FindId()
{
while (1) {
system("cls");
puts("\t***************************");
puts("\t* 查 找 菜 单 *");
puts("\t* 1、按编号精确查找 *");
puts("\t* 2、按编号范围查找 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
if (i == 0) return;
CInput in;
if (i == 1) {
int nId = in.InputId(m_list);
Equal<int> equal(nId);
auto it = find_if(m_list.begin(), m_list.end(), equal);
if (it == m_list.end()) cout << "没有找到编号为[ " << nId << " ]的员工信息" << endl;
else {
(*it)->print((*it));
}
system("pause");
}
else if (i == 2) {
int lowId = in.InputId(m_list);
int upId = in.InputId(m_list);
if (lowId > upId) swap(lowId, upId);
Range<int> range(lowId, upId);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), range);
if (x == m_list.end()) break;
else if (x == it) {
(*it)->print((*it));
++cnt;
}
}
cout << "一共找到[" << cnt << "]条符合条件的员工信息" << endl;
system("pause");
}
}
}
void CFind::FindName()
{
while (1)
{
system("cls");
puts("\t***************************");
puts("\t* 姓 名 查 找 菜 单 *");
puts("\t* 1、按姓名精确查找 *");
puts("\t* 2、按姓名模糊查找 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
if (i == 0) return;
CInput in;
string str = in.InputName(m_list);
if (i == 1) {
Equal<const string&> equal(str, EVarDim::dim);
auto x = find_if(m_list.begin(), m_list.end(), equal);
if (x == m_list.end()) cout << "没有找到姓名为[ " << str << " ]的员工信息" << endl;
else (*x)->print((*x));
system("pause");
}
else if (i == 2) {
Equal<const string&> equal(str, EVarDim::dim);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), equal);
if (x == m_list.end()) break;
else if (x == it) {
(*it)->print((*it));
++cnt;
}
}
cout << "一共找到[" << cnt << "]条符合条件的员工信息" << endl;
system("pause");
}
}
}
void CFind::FindPay()
{
while (1)
{
system("cls");
puts("\t*******************************");
puts("\t* 工 资 查 找 菜 单 *");
puts("\t* 1、精确查找一人 *");
puts("\t* 2、查找范围内的所有人 *");
puts("\t* 0、退出 *");
puts("\t*******************************");
cout << "\t\t请选择:";
int i = 0;
scanf_s("%d", &i);
if (i == 0) return;
CInput low, up;
double fLow = low.InputPay(m_list);
if (i == 1) {
Equal<double> equal(fLow, EVarType::pay);
auto it = find_if(m_list.begin(), m_list.end(), equal);
if (it == m_list.end()) cout << "没有找到工资为[ " << fLow << " ]的员工信息" << endl;
else (*it)->print((*it));
system("pause");
}
else if (i == 2) {
double fUp = up.InputPay(m_list);
if (fLow > fUp) swap(fLow, fUp);
Range<double> range(fLow, fUp, EVarType::pay);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), range);
if (x == m_list.end()) break;
else if (x == it) {
(*it)->print((*it));
++cnt;
}
}
cout << "一共找到[ " << cnt << " ]条符合条件的员工信息" << endl;
system("pause");
}
}
}
// 修改模块
// CModify.h
#pragma once
#include <list>
#include <memory>
#include "define.h"
class CManager;
class CModify
{
public:
CModify(const CManager* pCtrl, std::list<shared_ptr<SData>>& list)
: m_pCtrl(pCtrl), m_list(list) { }
public:
int Start();
int Menu();
void ModifyId();
void ModifyName();
void ModifyPay();
private:
const CManager* m_pCtrl;
std::list<std::shared_ptr<SData>>& m_list;
};
// CModify.cpp
#include "CModify.h"
#include "CFind.h"
#include "CInput.h"
#include <iostream>
int CModify::Start()
{
int n;
while ((n = Menu()) != 0) {
switch (n)
{
case 1:
ModifyId();
break;
case 2:
ModifyName();
break;
case 3:
ModifyPay();
break;
}
}
return 0;
}
int CModify::Menu()
{
system("cls");
puts("\t***************************");
puts("\t* 修 改 菜 单 *");
puts("\t* 1、按员工编号 *");
puts("\t* 2、按姓名 *");
puts("\t* 3、按工资范围 *");
puts("\t* 0、退出 *");
puts("\t***************************");
std::cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
return i;
}
void CModify::ModifyId()
{
while (1) {
system("cls");
puts("\t***************************");
puts("\t* 修 改 菜 单 *");
puts("\t* 1、按编号精确修改 *");
puts("\t* 2、按编号范围修改 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
if (i == 0) return;
CInput in;
if (i == 1) {
puts("输入你要查找并替换的员工的编号");
int nId = in.InputId(m_list);
puts("输入用于替换之前编号的编号");
int id = in.InputId(m_list);
Equal<int> equal(nId);
auto it = find_if(m_list.begin(), m_list.end(), equal);
if (it == m_list.end()) cout << "没有找到编号为[ " << nId << " ]的员工信息" << endl;
else {
(*it)->id = id;
(*it)->print((*it));
}
system("pause");
}
else if (i == 2) {
puts("输入你要查找并替换的员工的编号范围");
int lowId = in.InputId(m_list);
int upId = in.InputId(m_list);
if (lowId > upId) swap(lowId, upId);
puts("输入用于替换之前范围内所有编号的编号");
int id = in.InputId(m_list);
Range<int> range(lowId, upId);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), range);
if (x == m_list.end()) break;
else if (x == it) {
(*it)->id = id;
(*it)->print((*it));
++cnt;
}
}
cout << "一共修改[" << cnt << "]条符合条件的员工信息" << endl;
system("pause");
}
}
}
void CModify::ModifyName()
{
while (1)
{
system("cls");
puts("\t***************************");
puts("\t* 姓 名 查 找 菜 单 *");
puts("\t* 1、按姓名精确查找 *");
puts("\t* 2、按姓名模糊查找 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
cin >> i;
if (i == 0) return;
CInput in;
puts("输入你要查找并替换的员工的姓名");
string str = in.InputName(m_list);
puts("输入用于替换之前姓名的名字");
string s = in.InputName(m_list);
if (i == 1) {
Equal<const string&> equal(str, EVarDim::dim);
auto x = find_if(m_list.begin(), m_list.end(), equal);
if (x == m_list.end()) cout << "没有找到姓名为[ " << str << " ]的员工信息" << endl;
else {
strcpy_s((*x)->str, NAME_MAX_LEN, s.c_str());
(*x)->print((*x));
}
system("pause");
}
else if (i == 2) {
Equal<const string&> equal(str, EVarDim::dim);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), equal);
if (x == m_list.end()) break;
else if (x == it) {
strcpy_s((*x)->str, NAME_MAX_LEN, s.c_str());
(*it)->print((*it));
++cnt;
}
}
cout << "一共修改[" << cnt << "]条符合条件的员工信息" << endl;
system("pause");
}
}
}
void CModify::ModifyPay()
{
while (1)
{
system("cls");
puts("\t*******************************");
puts("\t* 工 资 查 找 菜 单 *");
puts("\t* 1、精确查找一人 *");
puts("\t* 2、查找范围内的所有人 *");
puts("\t* 0、退出 *");
puts("\t*******************************");
cout << "\t\t请选择:";
int i = 0;
scanf_s("%d", &i);
if (i == 0) return;
CInput low, up;
if (i == 1) {
puts("输入你要修改的工资当前金额");
double fLow = low.InputPay(m_list);
puts("输入你想要修改的工资金额");
double pay = low.InputPay(m_list);
Equal<double> equal(fLow, EVarType::pay);
auto it = find_if(m_list.begin(), m_list.end(), equal);
if (it == m_list.end()) cout << "没有找到工资为[ " << fLow << " ]的员工信息" << endl;
else {
(*it)->pay = pay;
(*it)->print((*it));
}
system("pause");
}
else if (i == 2) {
puts("输入你要修改的工资当前金额范围");
double fLow = low.InputPay(m_list);
double fUp = up.InputPay(m_list);
puts("输入你想要修改的工资金额");
double pay = low.InputPay(m_list);
if (fLow > fUp) swap(fLow, fUp);
Range<double> range(fLow, fUp, EVarType::pay);
int cnt = 0;
for (auto it = m_list.begin(); ; ++it) {
auto x = find_if(it, m_list.end(), range);
if (x == m_list.end()) break;
else if (x == it) {
(*it)->pay = pay;
(*it)->print((*it));
++cnt;
}
}
cout << "一共修改[ " << cnt << " ]条符合条件的员工信息" << endl;
system("pause");
}
}
}
// 排序模块
// CSort.h
#pragma once
#include "define.h"
#include <list>
#include <memory>
using namespace std;
typedef bool (*Compare)(shared_ptr<SData>&, shared_ptr<SData>&);
class CManager;
class CSort
{
public:
CSort(const CManager* pCtrl, list<shared_ptr<SData>>& list)
: m_pCtrl(pCtrl), m_list(list) { }
int Start();
private:
static bool id_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b);
static bool id_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b);
static bool name_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b);
static bool name_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b);
static bool pay_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b);
static bool pay_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b);
private:
int Menu();
void SortById();
void SortByName();
void SortByPay();
void Sort(int n = 0);
private:
const CManager* m_pCtrl;
list<shared_ptr<SData>>& m_list;
};
// CSort.cpp
#include "CSort.h"
#include "CInput.h"
#include "CManager.h"
#include <string.h>
#include <iostream>
using namespace std;
bool CSort::id_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return (*a).id > (*b).id;
}
bool CSort::id_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return (*a).id > (*b).id;
}
bool CSort::name_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return strcmp((const char*)(*a).str, (const char*)(*b).str) < 0;
}
bool CSort::name_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return strcmp((const char*)(*a).str, (const char*)(*b).str) > 0;
}
bool CSort::pay_sort_less(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return (*a).pay < (*b).pay;
}
bool CSort::pay_sort_greater(shared_ptr<SData>& a, shared_ptr<SData>& b)
{
return (*a).pay > (*b).pay;
}
int CSort::Start()
{
int n;
while ((n = Menu()) != 0) {
switch (n)
{
case 1:
SortById();
break;
case 2:
SortByName();
break;
case 3:
SortByPay();
break;
}
}
return 0;
}
int CSort::Menu()
{
system("cls");
puts("\t***************************");
puts("\t* 排 序 菜 单 *");
puts("\t* 1、按员工编号 *");
puts("\t* 2、按员工姓名 *");
puts("\t* 3、按员工工资 *");
puts("\t* 0、退出 *");
puts("\t***************************");
std::cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
return i;
}
void CSort::SortById()
{
while (1) {
system("cls");
puts("\t***************************");
puts("\t* 编 号 排 序 菜 单 *");
puts("\t* 1、按编号升序 *");
puts("\t* 2、按编号降序 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
if (i == 0) return;
m_list.sort(id_sort_less);
i == 1 ? m_list.sort(id_sort_less) : m_list.sort(id_sort_greater);
m_pCtrl->print();
}
}
void CSort::SortByName()
{
while (1) {
system("cls");
puts("\t****************************");
puts("\t* 姓 名 排 序 菜 单 *");
puts("\t* 1、按姓名升序 *");
puts("\t* 2、按姓名降序 *");
puts("\t* 0、退出 *");
puts("\t****************************");
cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
if (i == 0) return;
i == 1 ? m_list.sort(name_sort_less) : m_list.sort(name_sort_greater);
m_pCtrl->print();
}
}
void CSort::SortByPay()
{
while (1) {
system("cls");
puts("\t***************************");
puts("\t* 工 资 排 序 菜 单 *");
puts("\t* 1、按工资升序 *");
puts("\t* 2、按工资降序 *");
puts("\t* 0、退出 *");
puts("\t***************************");
cout << "\t\t请选择:";
int i = 0;
std::cin >> i;
if (i == 0) return;
i == 1 ? m_list.sort(pay_sort_less) : m_list.sort(pay_sort_greater);
m_pCtrl->print();
}
}
自动析构结果