// main.cpp
#include "CList.h"
#include "CFrame.h"
int main()
{
CFrame frm;
return frm.Start();
}
// CFrame.h
#pragma once
#include "CList.h"
#include "CDelete.h"
#include "CModify.h"
#include "CFind.h"
#include "CSort.h"
class CFrame
{
public:
CFrame() : m_delete(m_list), m_modify(m_list), m_find(m_list), m_sort(m_list) { }
int Start();
private:
int Menu();
void Input();
void Output();
void Load();
void Save();
void Delete();
void Modify();
void Find();
void Sort();
inline CList& Entry() { return m_list; }
private:
CList m_list;
CDelete m_delete;
CModify m_modify;
CFind m_find;
CSort m_sort;
};
// CFrame.cpp
#include <sstream>
#include <cstdio>
#include <string>
#include "CFrame.h"
#include "CInput.h"
int CFrame::Start()
{
Load();
int n;
while (n = Menu())
{
switch (n)
{
case 1:
Output();
break;
case 2:
Input();
break;
case 3:
Delete();
break;
case 4:
Modify();
break;
case 5:
Find();
break;
case 6:
Sort();
}
}
Save();
return 0;
}
int CFrame::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***************************");
cout << "\t\t请选择:";
int i = 0;
scanf_s("%d", &i);
return i;
}
void CFrame::Output()
{
Entry().PrintAll();
}
void CFrame::Load()
{
Entry().Load();
}
void CFrame::Save()
{
Entry().Save();
}
void CFrame::Delete()
{
m_delete.Start();
}
void CFrame::Modify()
{
m_modify.Start();
}
void CFrame::Find()
{
m_find.Start();
}
void CFrame::Sort()
{
m_sort.Start();
}
void CFrame::Input()
{
CInput in;
SData* pData = new SData;
if (!pData) return;
pData->id = in.InputId(m_list, true);
char str[50];
in.InputName(str, 50);
strcpy_s(pData->str, sizeof(pData->str), str);
pData->pay = in.InputPay();
Entry().PushBack(pData);
}
// CList.h
#pragma once
#include <iostream>
#include <string>
const int NAME_LEN = 20;
using namespace std;
enum ECompare { less = -1, equal = 0, greate = 1 };
enum ECompType { nothing = 0, id = 1, name = 2, pay = 3 };
enum ESortType { inc = ECompare::greate, dec = ECompare::less };
struct SParam
{
SParam() { memset(this, 0, sizeof(SParam)); }
int id;
int idChange;
bool bCmpNameDim;
char str[NAME_LEN];
char strChange[NAME_LEN];
double payLow;
double payUp;
double pay;
double payChange;
bool bSortType;
};
struct SData
{
int id;
char str[NAME_LEN];
double pay;
};
class CCompare
{
public:
CCompare(SData* pData) : pInfo(pData) { }
~CCompare() { }
ECompare operator()(const SParam& param);
ECompType check(const SParam& param);
void set(SData* pData) { pInfo = pData; }
private:
SData* pInfo;
};
struct SNode
{
SNode() : data(NULL), pred(NULL), succ(NULL) { }
SNode(SData* const pPerson, SNode* pPred, SNode* pSucc)
: data(pPerson), pred(pPred), succ(pSucc) { }
SNode* AddAsPred(SData* d);
SNode* AddAsSucc(SData* d);
SData* data;
SNode* pred;
SNode* succ;
};
class CList
{
public:
CList() { Init(); } // 默认构造函数
CList(SNode* pNode, int n); // 部分复制的构造函数
CList(const CList& list, int r, int n); // 部分复制的构造函数
CList(const CList& list); // 全部复制的构造函数
~CList(); // 析构函数
public:
inline const bool Valid(SNode* pNode) const // 节点有效性判断函数
{ return (pNode && pNode != m_head && pNode != m_tail); }
inline const int Size() const { return m_size; } // 返回链表的节点个数
inline const bool Empty() const { return m_size <= 0; } // 链表判空
inline SNode* Head() const { return m_head; } // 获取表头的地址
inline SNode* Tail() const { return m_tail; } // 获取表位的地址
inline SNode* First() const; // 获取首个有效节点的地址
inline SNode* Last() const; // 获取末个有效节点的地址
inline void PrintTable() const; // 打印信息表头
inline void PrintInformaion(SNode* pNode) const; // 打印一条信息
void PrintAll() const; // 打印全部信息
inline SNode* PushFront(SData* pPerson); // 新节点从首位压入
inline SNode* PushBack(SData* pPerson); // 新节点从末尾压入
inline void PopFront();
inline void PopBack();
void Remove(const SParam& param, ECompare nRule = ECompare::equal, bool bAllSame = false); // 删除
int Find(const SParam& param, ECompare nRule = ECompare::equal, bool bAllSame = false) const; // 查找
void Modify(const SParam& param, ECompare nRule = ECompare::equal, bool bAllSame = false); // 修改
void Sort(ECompType nType, ESortType nRule);
SNode* operator[](int r) const; // 秩转地址
const CList& operator=(const CList& list); // 链表赋值
void Load();
void Save();
private:
inline void Init(); // 构造辅助函数
void CopyNodes(SNode* pNode, int n); // 复制辅助函数
inline void Remove(SNode* pNode); // 删除一个节点
void RemoveAll(); // 删除所有节点
void Modify(CCompare& comp, const SParam& param, SData& person); // 修改一个节点的信息
private:
int m_size; // 有效节点的总数
SNode* m_head; // 头指针,指向头部的哨兵节点
SNode* m_tail; // 尾指针,指向尾部的哨兵节点
};
// CList.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "CList.h"
#include <string>
/////////////////////////////////////////////
// 处理比较的函数对象
ECompare CCompare::operator()(const SParam& param)
{
if (check(param) == ECompType::id)
{
if (param.id < pInfo->id)
return ECompare::greate; // 找出比参数id大的节点
if (param.id == pInfo->id)
return ECompare::equal; // 找出与参数id相等的节点
if (param.id > pInfo->id)
return ECompare::less; // 找出比参数id小的节点
}
if (check(param) == ECompType::name)
{
const char* str1 = pInfo->str;
const char* str2 = param.str;
if (param.bCmpNameDim)
{
if (strstr(str1, str2) == NULL)
return ECompare::less;
return ECompare::equal;
}
else
{
if (strcmp(str1, str2) < 0) return ECompare::less;
else if (!strcmp(str1, str2)) return ECompare::equal;
else return ECompare::greate;
}
}
if (check(param) == ECompType::pay)
{
if (pInfo->pay < param.payLow)
return ECompare::greate;
if (pInfo->pay >= param.payLow && pInfo->pay <= param.payUp)
return ECompare::equal;
if (pInfo->pay > param.payUp)
return ECompare::less;
}
return ECompare::equal;
}
ECompType CCompare::check(const SParam& param)
{
if (param.id != 0)
return ECompType::id;
if (strlen(param.str) > 0)
return ECompType::name;
if (param.payLow > 0 && param.payUp > 0 && param.payLow <= param.payUp)
return ECompType::pay;
return ECompType::nothing;
}
//////////////////////////////////////
// 一个节点作为另一个节点的前驱
SNode* SNode::AddAsPred(SData* pData)
{
auto x = new SNode(pData, pred, this);
if (!x) return NULL;
pred->succ = x;
pred = x;
return x;
}
//////////////////////////////////////
// 一个节点作为另一个节点的后继
SNode* SNode::AddAsSucc(SData* pData)
{
auto x = new SNode(pData, this, succ);
if (!x) return NULL;
succ->pred = x;
succ = x;
return x;
}
// CList的默认构造函数
CList::CList(SNode* pNode, int n)
{
CopyNodes(pNode, n);
}
// 对另外一个链表进行复制构的造函数
CList::CList(const CList& list)
{
CopyNodes(list.Head(), list.Size());
}
// 对另外一个链表进行部分复制的构造函数
CList::CList(const CList& list, int r, int n)
{
CopyNodes(list[r], n);
}
// 构造辅助函数
void CList::Init()
{
m_head = new SNode;
m_tail = new SNode;
m_tail->succ = NULL;
m_tail->pred = m_head;
m_head->succ = m_tail;
m_head->pred = NULL;
m_size = 0;
}
// 复制构造辅助函数
void CList::CopyNodes(SNode* pNode, int n)
{
Init();
for (int i = 0; i < n; i++, pNode = pNode->succ)
PushBack(pNode->data);
}
CList::~CList()
{
RemoveAll();
delete m_head;
delete m_tail;
}
void CList::PrintTable() const
{
if (Empty())
{
puts("记录中没有任何信息");
return;
}
printf("%-10s %-10s %-10s\n", "编号", "姓名", "工资");
}
void CList::PrintInformaion(SNode* pNode) const
{
if (pNode->data == nullptr) return;
printf("%-10d %-10s %-8.2lf \n", pNode->data->id, pNode->data->str, pNode->data->pay);
}
void CList::PrintAll() const
{
PrintTable();
for (auto p = First(); p && p != Tail(); p = p->succ)
PrintInformaion(p);
system("pause");
}
inline SNode* CList::First() const
{
if (!Empty())
return m_head->succ;
return NULL;
}
inline SNode* CList::Last() const
{
if (!Empty())
return m_tail->pred;
return NULL;
}
inline SNode* CList::PushFront(SData* pData)
{
m_size++;
return m_head->AddAsSucc(pData);
}
inline SNode* CList::PushBack(SData* pData)
{
m_size++;
return m_tail->AddAsPred(pData);
}
inline void CList::PopFront()
{
if (m_size <= 0)
return;
Remove(First());
}
inline void CList::PopBack()
{
if (m_size <= 0)
return;
Remove(Last());
}
SNode* CList::operator[](int r) const
{
auto pNode = m_head->succ;
for (int i = 0; i < r; i++, pNode = pNode->succ);
return pNode;
}
const CList& CList::operator=(const CList& list)
{
if (this == &list) return *this; // 复制自己直接返回*this
RemoveAll(); // 清空节点
CopyNodes(list.First(), list.Size()); // 复制list的所有节点
return *this; // 复制完成返回*this
}
int CList::Find(const SParam& param, ECompare nRule, bool bAllSame) const
{
int cnt = 0;
PrintTable();
for (auto p = First(); p && p != Head() && p != Tail(); p = p->succ)
{
CCompare comp(p->data);
if (comp.operator()(param) == nRule)
{
PrintInformaion(p);
cnt++;
if (!bAllSame)
{
puts("找到一条符合条件的信息");
system("pause");
return 1;
}
}
}
printf("一共找到[ %d ]条符合条件的信息\n", cnt);
system("pause");
return cnt;
}
void CList::Modify(CCompare& comp, const SParam& param, SData& data)
{
if (comp.check(param) == ECompType::id)
data.id = param.idChange;
else if (comp.check(param) == ECompType::name)
strcpy_s(data.str, NAME_LEN, param.strChange);
else if (comp.check(param) == ECompType::pay)
data.pay = param.payChange;
else if (comp.check(param) == ECompType::nothing)
return;
}
void CList::Modify(const SParam& param, ECompare nRule, bool bAllSame)
{
int cnt = 0;
PrintTable();
for (auto p = m_head->succ; p && p != m_tail; p = p->succ)
{
CCompare comp(p->data);
if (comp.operator()(param) == nRule)
{
cnt++;
puts("修改前后的信息");
PrintInformaion(p);
Modify(comp, param, *p->data);
PrintInformaion(p);
puts("");
if (!bAllSame)
{
puts("修改一条符合条件的信息");
system("pause");
return;
}
}
}
printf("一共修改[ %d ]条符合条件的信息\n", cnt);
system("pause");
}
void CList::Remove(const SParam& param, ECompare nRule, bool bAllSame) {
if (!First()) return;
int nSize = Size();
PrintTable();
for (auto p = First(); p && p != Head() && p!= Tail(); p = p->succ) {
CCompare comp(p->data);
auto q = p->pred;
if (comp.operator()(param) == nRule) {
PrintInformaion(p); // 删除前对信息打印
Remove(p);
if (!bAllSame) return;
p = q;
}
}
printf("一共删除[ %d ]条符合条件的信息\n", nSize - Size());
system("pause");
}
void CList::Remove(SNode* pNode)
{
pNode->succ->pred = pNode->pred;
pNode->pred->succ = pNode->succ;
delete pNode->data;
delete pNode;
pNode = NULL;
m_size--;
}
void CList::RemoveAll()
{
while (!Empty())
Remove(m_head->succ);
}
void CList::Load()
{
FILE* fp;
errno_t err = fopen_s(&fp, "test.bin", "rb");
if (err || !fp) {
perror("\t错误");
return;
}
int n = 0;
int ans = fread(&n, sizeof(int), 1, fp);
if (ans < 1 || !n)
{
perror("\t错误");
return;
}
for (int i = 0; i < n; i++)
{
SData* pData = new SData;
if (!pData) return;
int n = fread(pData, sizeof(SData), 1, fp);
PushBack(pData);
}
fclose(fp);
}
void CList::Save()
{
if (Empty()) return;
FILE* fp;
errno_t err = fopen_s(&fp, "test.bin", "wb");
if (!fp || err)
{
perror("\t错误");
return;
}
int ans = fwrite((const void*)&m_size, sizeof(int), 1, fp);
if(ans < 1)
{
perror("\t错误");
return;
}
while (!Empty())
{
ans = fwrite((const void*)&(*(First()->data)), sizeof(SData), 1, fp);
if (ans < 1)
{
perror("\t错误");
return;
}
PopFront();
}
fclose(fp);
}
template<typename T>
bool compare(T a, T b, ECompare nRule)
{
if(ECompare::less == nRule) return a < b;
if (ECompare::equal == nRule) return a == b;
if (ECompare::greate == nRule) return a > b;
return false;
}
template<>
bool compare(char* a, char* b, ECompare nRule)
{
if (ECompare::less == nRule) return strcmp((const char*)a, (const char*)b) == -1;
if (ECompare::equal == nRule) return strcmp((const char*)a, (const char*)b) == 0;
if (ECompare::greate == nRule) return strcmp((const char*)a, (const char*)b) == 1;
return false;
}
void CList::Sort(ECompType nType, ESortType nRule)
{
for (auto p = First(); p != Tail(); p = p->succ) {
auto min = p;
for (auto q = First(); q != Tail(); q = q->succ) {
if (nType == ECompType::id) {
if (compare(q->data->id, min->data->id, (ECompare)nRule))
min = q;
}
else if (nType == ECompType::name) {
if (compare(q->data->str, min->data->str, (ECompare)nRule))
min = q;
}
else if (nType == ECompType::pay) {
if (compare(q->data->pay, min->data->pay, (ECompare)nRule))
min = q;
}
SData** x = &min->data;
SData** y = &p->data;
SData* t = *x;
*x = *y;
*y = t;
}
}
PrintAll();
}
// CInput.h
#pragma once
#include "CList.h"
const int MAX_LEN = 1024;
class CInput
{
public:
bool CheckId(int nId, CList& list, bool bCheck);
int InputId(CList& list, bool bCheck = false);
void InputName(char* str, int n);
double InputPay();
void CheckInput(char* str, int n, bool bCheckAlpha,
const char* buf1, const char* buf2);
void Str2Digit(const char* buf, bool bInt, int& nId, double& fPay);
};
// CInput.cpp
#include "CInput.h"
#include <sstream>
#include <cstdio>
#include "CFrame.h"
#include <string>
using namespace std;
bool CInput::CheckId(int nId, CList& list, bool bCheck)
{
if (!bCheck) return false;
for (auto p = list.First(); p && p != list.Tail(); p = p->succ)
if (p->data->id == nId) return true;
return false;
}
int CInput::InputId(CList& list, bool bCheck)
{
char buf[MAX_LEN];
Lab:
CheckInput(buf, MAX_LEN, true, "请输入员工的编号",
"请正确输入员工的编号,不含有字母与符号");
double x = 0.0f;
int nId = 0;
Str2Digit(buf, true, nId, x);
if (CheckId(nId, list, bCheck))
{
puts("请正确输入员工的编号,不能用相同的数字做编号");
goto Lab;
}
return nId;
}
void CInput::InputName(char* str, int n)
{
char buf[MAX_LEN];
Lab:
CheckInput(buf, MAX_LEN, false, "请输入员工的姓名:",
"请正确输入员工的姓名,不含有数字与符号");
if (strlen(buf) > 50)
{
puts("请输入员工姓名的字符数小于50个字符");
goto Lab;
}
strcpy_s(str, n, buf);
}
double CInput::InputPay()
{
char buf[MAX_LEN];
Lab:
CheckInput(buf, MAX_LEN, true, "请输入员工的工资",
"请正确输入员工的工资,不含有字母与符号");
double fPay = 0.0f;
int x = 0;
Str2Digit(buf, false, x, fPay);
if (fPay <= 0 || fPay > 1e8)
{
puts("请正确输入工资,金额非负");
goto Lab;
}
return fPay;
}
void CInput::CheckInput(char* str, int n, bool bCheckAlpha,
const char* buf1, const char* buf2)
{
cin.sync_with_stdio(false);
while (1)
{
puts(buf1);
rewind(stdin);
cin.getline(str, n, '\n'); // 读取一行数据到字符串
bool bValid = true; // 假定读入有效
for (int i = 0; str[i] != '\0'; i++)
{
if (bCheckAlpha)
{
if (isalpha(str[i]) || ispunct(str[i]))
{
puts(buf2);
bValid = false;
break;
}
}
else if (isdigit(str[i]) || ispunct(str[i]))
{
puts(buf2);
bValid = false;
break;
}
}
if (!bValid)
{
memset(str, 0, MAX_LEN);
continue;
}
break;
}
}
void CInput::Str2Digit(const char* buf, bool bInt, int& nId, double& fPay)
{
stringstream ss;
string str = buf;
ss << str;
if (bInt) ss >> nId;
else ss >> fPay;
ss.clear();
}
// CDelete.h
#pragma once
#include "CList.h"
class CDelete
{
public:
CDelete(CList& list) : m_list(list) { }
int Start();
private:
int Menu();
void RemoveId();
void RemoveName();
void RemovePay();
private:
CList& m_list;
};
// CDelete.cpp
#include "CDelete.h"
#include "CInput.h"
#include "CList.h"
#include <string.h>
int CDelete::Start()
{
int n;
while (n = Menu())
{
switch (n)
{
case 1:
RemoveId();
break;
case 2:
RemoveName();
break;
case 3:
RemovePay();
break;
case 0:
return 0;
}
}
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;
scanf_s("%d", &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;
scanf_s("%d", &i);
if (i == 0) return;
CInput in;
int id = in.InputId(m_list, false);
auto p = m_list.First();
for (; p && p != m_list.Tail(); p = p->succ)
if (p->data->id == id) break;
if (p == NULL) return;
SParam param;
param.id = id;
bool bAllSame;
ECompare comp;
if (i == 1) comp = ECompare::less, bAllSame = true;
else if (i == 2) comp = ECompare::equal, bAllSame = false;
else if (i == 3) comp = ECompare::greate, bAllSame = true;
m_list.Remove(param, comp, bAllSame);
}
}
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;
scanf_s("%d", &i);
if (i == 0) return;
CInput in;
char buf[MAX_LEN];
in.InputName(buf, NAME_LEN);
SParam param;
memset((void*)¶m, 0, sizeof(SParam));
strcpy_s(param.str, NAME_LEN, buf);
bool bAllSame;
if (i == 1) bAllSame = false;
else param.bCmpNameDim = true, bAllSame = true;
m_list.Remove(param, ECompare::equal, bAllSame);
}
}
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;
scanf_s("%d", &i);
if (i == 0) return;
CInput in;
SParam param;
memset((void*)¶m, 0, sizeof(SParam));
puts("输入要删除的工资范围");
double low = in.InputPay();
double up = in.InputPay();
if (low > up) swap(low, up);
param.payLow = low, param.payUp = up;
if (i == 2) param.pay = in.InputPay();
m_list.Remove(param, ECompare::equal, true);
}
}
//CFind.h
#pragma once
#include "CList.h"
#include "CInput.h"
class CFind
{
public:
CFind(CList& list) : m_list(list) { }
int Start();
int Find_Menu();
void FindId();
void FindName();
void FindPay();
private:
CList& m_list;
};
// CFind.cpp
#include "CFind.h"
int CFind::Start()
{
int n;
while (n = Find_Menu())
{
switch (n)
{
case 1:
FindId();
break;
case 2:
FindName();
break;
case 3:
FindPay();
break;
case 0:
return 0;
}
}
return 0;
}
int CFind::Find_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;
scanf_s("%d", &i);
return i;
}
void CFind::FindId()
{
CInput in;
int id = in.InputId(m_list, false);
SParam param;
param.id = id;
m_list.Find(param, ECompare::equal, false);
}
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;
scanf_s("%d", &i);
if (i == 0) return;
CInput in;
char buf[MAX_LEN];
in.InputName(buf, NAME_LEN);
SParam param;
strcpy_s(param.str, NAME_LEN, buf);
bool bAllSame;
if (i == 1) bAllSame = false;
else param.bCmpNameDim = true, bAllSame = true;
m_list.Find(param, ECompare::equal, bAllSame);
}
}
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 in;
SParam param;
memset((void*)¶m, 0, sizeof(SParam));
puts("输入要删除的工资范围");
double low = in.InputPay();
double up = in.InputPay();
if (low > up) swap(low, up);
param.payLow = low, param.payUp = up;
bool AllSame;
i == 1 ? AllSame = false : AllSame = true;
m_list.Find(param, ECompare::equal, AllSame);
}
}
// CModify.h
#pragma once
#include "CList.h"
#include "CInput.h"
class CModify
{
public:
CModify(CList& list) : m_list(list) { }
int Start();
int Menu();
void ModifyId();
void ModifyName();
void ModifyPay();
private:
CList& m_list;
};
// CModify.cpp
#include "CModify.h"
int CModify::Start()
{
int n;
while (n = Menu())
{
switch (n)
{
case 1:
ModifyId();
break;
case 2:
ModifyName();
break;
case 3:
ModifyPay();
break;
case 0:
return 0;
}
}
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***************************");
cout << "\t\t请选择:";
int i = 0;
scanf_s("%d", &i);
return i;
}
void CModify::ModifyId()
{
CInput in;
puts("输入要修改的员工原来的编号");
int id = in.InputId(m_list, false);
auto p = m_list.First();
for (; p && p != m_list.Tail(); p = p->succ)
if (p->data->id == id) break;
if (p == NULL) return;
puts("输入要修改的员工现在的编号");
int idChange = in.InputId(m_list, false);
SParam param;
param.id = id;
param.idChange = idChange;
m_list.Modify(param, ECompare::equal, false); // -1 大于 0 等于 1 小于
}
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;
scanf_s("%d", &i);
if (i == 0) return;
CInput in;
char bufBefore[MAX_LEN], bufAfter[MAX_LEN];
puts("输入想要修改的员工姓名");
in.InputName(bufBefore, NAME_LEN);
puts("输入想要改成的员工姓名");
in.InputName(bufAfter, NAME_LEN);
SParam param;
param.bCmpNameDim = false;
strcpy_s(param.str, NAME_LEN, bufBefore);
strcpy_s(param.strChange, NAME_LEN, bufAfter);
bool bAllSame;
if (i == 1) bAllSame = false;
else param.bCmpNameDim = true, bAllSame = true;
m_list.Modify(param, ECompare::equal, bAllSame);
}
}
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 in;
SParam param;
puts("输入要修改的工资范围的下限");
double low = in.InputPay();
puts("输入要修改的工资范围的上限");
double up = in.InputPay();
puts("输入要修改成的工资金额");
double payChange = in.InputPay();
if (low > up) swap(low, up);
param.bCmpNameDim = false, param.payLow = low;
param.payUp = up, param.payChange = payChange;
bool bAllSame;
i == 1 ? bAllSame = false : bAllSame = true;
m_list.Modify(param, ECompare::equal, bAllSame);
}
}
//CSort.h
#pragma once
#include "CList.h"
class CSort
{
public:
CSort(CList& list) : m_list(list) { }
int Start();
private:
int Menu();
void SortId();
void SortName();
void SortPay();
private:
CList& m_list;
};
//CSort.cpp
#include "CSort.h"
#include "CInput.h"
#include "CList.h"
int CSort::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;
scanf_s("%d", &i);
return i;
}
int CSort::Start()
{
int n;
while (n = Menu())
{
switch (n)
{
case 1:
SortId();
break;
case 2:
SortName();
break;
case 3:
SortPay();
break;
case 0:
return 0;
}
}
return 0;
}
void CSort::SortId()
{
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;
if (i == 1) m_list.Sort(ECompType::id, ESortType::inc);
else if(i == 2) m_list.Sort(ECompType::id, ESortType::dec);
}
}
void CSort::SortName()
{
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;
if (i == 1) m_list.Sort(ECompType::name, ESortType::inc);
else if (i == 2) m_list.Sort(ECompType::name, ESortType::dec);
}
}
void CSort::SortPay()
{
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;
if (i == 1) m_list.Sort(ECompType::pay, ESortType::inc);
else if (i == 2) m_list.Sort(ECompType::pay, ESortType::dec);
}
}