您的位置:首页 > 编程语言 > C语言/C++

C++面向对象,泛型编程 文件流综合应用-公司员工管理系统

2017-08-14 09:36 459 查看

前言

C++语言兼容了面向过程和面向对象的两种编程思想。STL泛型编程也大大简化了我们编写代码的难度,其中泛型编程的思想也是尤为重要,模板类大大简化了代码的容量。学完了面向对象程序射界,流的输入输出,STL泛型编程,模板等等一些编程语言的基本知识。巩固这些知识,我觉得应该写一个实质性的东西来强化一下,于是我写了一个简单的综合应用来纪念下。

代码

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>  //
#include <iomanip>
#include <vector>
#include <typeinfo>  //直接使用Sign,简化代码,通过Sign打开文件
using namespace std;
class Employee
{
public:
Employee();
virtual void show();       //输出成员信息
virtual void add() ;  //添加新成员
template<class T> friend class System;//
virtual void calculate(); //计算薪水
virtual void Load(istringstream&sstr, Employee&P)
{
sstr >> P.No >> P.Name >> P.G >> P.Sex >> P.EndS >> P.Tel >> P.Adr >> P.S >> P.Sign;
}
virtual void Save(ofstream&);
virtual void out()
{
cout << left;
cout << setw(12) << "编号" << setw(10) << "姓名" << setw(5) << "级别" << setw(5) << "性别" << setw(5)
<< "月薪" << setw(12) << "电话" << "\t" << setw(20) << "地址" << endl;
}
protected:
string No;           //编号
char G;            //级别
int S;             //底薪
string Name;       //姓名
string Sex;        //性别  1就是男 2就是女
string Tel;          //电话
string Adr;        //地址
int EndS;  // 最终薪水
string Sign;   //标志  方便以后的查询
};

class Technician:public Employee
{
public:
virtual void out()
{
cout << left;
cout << setw(12) << "编号" << setw(10) << "姓名" << setw(5) << "级别" << setw(5) << "性别"<<setw(8)
<< "底薪" << setw(10) << "时工资" << setw(15) << "工作时间" << setw(10)
<< "月薪" << setw(12) << "电话"  << setw(20) << "地址" << endl;
}
void Save(ofstream&out)
{
out << setw(12) << this->No << "\t" << this->Name << "\t" << this->G << "\t" << this->Sex << "\t" << this->EndS
<< "\t" << setw(12) << this->Tel << "\t" << setw(12) << this->Adr << "\t" << this->S << "\t" << this->Sign <<"\t"
<<this->Hours<<"\t"<<this->HoursWage<< endl;
}
void Load(istringstream&sstr,Technician&P)
{
sstr >> P.No >> P.Name >> P.G >> P.Sex >> P.EndS >> P.Tel >> P.Adr >> P.S >> P.Sign >> P.Hours >> P.HoursWage;
}
Technician();
virtual void show();       //输出成员信息
virtual void add();  //添加新成员
virtual void calculate(); //计算薪
private:
int Hours;//每月的工作总时
int HoursWage;  //每小时的工资
};

class Manager:virtual public Employee
{
public:
void Save(ofstream&out)
{
out << setw(12) << this->No << "\t" << this->Name << "\t" << this->G << "\t" << this->Sex << "\t" << this->EndS
<< "\t" << setw(12) << this->Tel << "\t" << setw(12) << this->Adr << "\t" << this->S << "\t" << this->Sign <<"\t"<<this->Part<< endl;
}
void Load(istringstream&sstr,Manager&t)
{
sstr >> t.No >> t.Name >> t.G >> t.Sex >> t.EndS >> t.Tel >> t.Adr >> t.S >> t.Sign >> t.Part;
}
virtual void out()
{
cout << left;
cout << setw(12) << "编号" << setw(10) << "姓名" << setw(5) << "级别" << setw(5) << "性别" <<setw(8)<<"部门"<< setw(8)
<< "月薪" << setw(12) << "电话" << setw(20) << "地址" << endl;
}
Manager();
void show();       //输出成员信息
void add();  //添加新成员
protected:
string Part;//部门
};

class Salesman :virtual public Employee//兼职销售人员
{
public:
void Save(ofstream&out)
{
out << setw(12) << this->No << "\t" << this->Name << "\t" << this->G << "\t" << this->Sex << "\t" << this->EndS
<< "\t" << setw(12) << this->Tel << "\t" << setw(12) << this->Adr << "\t" << this->S << "\t" << this->Sign<<"\t"<<this->Num<<"\t"<<this->NumWage << endl;
}
void Load(istringstream&sstr,Salesman&P)
{
sstr >> P.No >> P.Name >> P.G >> P.Sex >> P.EndS >> P.Tel >> P.Adr >> P.S >> P.Sign >> P.Num >> P.NumWage;
}
virtual void out()
{
cout << left;
cout << setw(12) << "编号" << setw(10) << "姓名" << setw(5) << "级别" << setw(5) << "性别" <<setw(5) << "底薪"<< setw(8)
<< "件利润" << setw(8) << "件数"<< setw(8)
<< "月薪" << setw(12) << "电话"<< setw(20) << "地址" << endl;
}
Salesman();
void show();       //输出成员信息
void add();  //添加新成员
void calculate(); //计算薪
private:
int Num;
int NumWage;
};
class Salesmanager:virtual public Manager,virtual public Salesman
{
public:
void Save(ofstream&out)
{
out << setw(12) << this->No << "\t" << this->Name << "\t" << this->G << "\t" << this->Sex << "\t" << this->EndS
<< "\t" << setw(12) << this->Tel << "\t" << setw(12) << this->Adr << "\t" << this->S
<< "\t" << this->Sign << "\t" << this->Part << endl;
}
void Load(istringstream&sstr,Salesmanager&t)
{
sstr >> t.No >> t.Name >> t.G >> t.Sex >> t.EndS >> t.Tel >> t.Adr >> t.S >> t.Sign >> t.Part;
}
void out()
{
Manager::out();
}
Salesmanager();
void add();
void show();
};
void Salesmanager::show()
{
Manager::show();
}
void Salesmanager::add()
{
Employee::add();
}
Salesmanager::Salesmanager()
{
this->Sign = "销售经理";
this->Part = "销售";
}
void Manager::add()
{
Employee::add();
cout << "请输入管理部门:";
cin >> this->Part;
}
void Manager::show()
{
this->calculate();
cout << left;
cout << setw(12) << this->No << setw(10) << this->Name << setw(5) << this->G << setw(5) << this->Sex <<setw(8) << this->Part << setw(8)
<< this->EndS << setw(12) << this->Tel  << setw(20) << this->Adr << endl;

}
Manager::Manager()
{
this->Sign = "经理";
}

void Salesman::calculate()
{
this->EndS = this->S + this->Num*this->NumWage;
}
void Salesman::add()
{
Employee::add();
while (true)
{
cout << "请输入件数:";
cin >> this->Num;
if (this->Num >= 0)
break;
}
while (true)
{
cout << "请输入每件的提成(元):";
cin >> this->NumWage;
if (this->NumWage >= 0)
break;
}
}
void Salesman::show()
{
this->calculate();
cout << left;
cout << setw(12) << this->No << setw(10) << this->Name << setw(5) << this->G << setw(5) << this->Sex << setw(5) << this->S << setw(8)
<< this->NumWage << setw(8) << this->Num  << setw(8)
<< this->EndS << setw(12) << this->Tel  << setw(20) << this->Adr << endl;
}
Salesman::Salesman()
{
this->Sign = "兼职销售人员";
}
template<class T>
class System
{
private:
T P;
vector <T> person;//建立静态的一个Person向量   读书文件  不可以建立static变量
typename vector <T>::iterator Iter;
void System<T>::modify()
{
cout << "请输入您要修改的" << P.Sign << "姓名或者编号:";
string str;
cin >> str;
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
if (this->Iter->Name == str || this->Iter->No == str)
{
cout << "您要修改的" << P.Sign << "信息如下:" << endl;
this->Iter->out();
this->Iter->show();
cout << "请您重新输入" << this->Iter->Name << P.Sign <<
b3f7
; "的信息" << endl;
this->Iter->add();
return;
}
}
cout << "无" << str << P.Sign << "!" << endl;
}
vector <Employee>::iterator System<T>::SearchIterator()
{
string str;
cin >> str;
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
if (this->Iter->Name == str || this->Iter->No == str)
{
return this->Iter;
}
}
return this->person.end();
}
void System<T>::detelte()
{
cout << "请输入您要删除的" << P.Sign << "姓名或者编号:";
string str;
cin >> str;
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
if (this->Iter->Name == str || this->Iter->No == str)
{
this->person.erase(this->Iter);
cout << P.Sign << "删除成功" << endl;
return;
}
}
cout << "无" << str << "的" << this->P.Sign << "人员信息" << "!" << endl;
}
void System<T>::search()
{
cout << "请输入您要查找的" << P.Sign << "姓名或者编号:";
string str;
cin >> str;
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
if (this->Iter->Name == str || this->Iter->No == str)
{
cout << "信息如下:" << endl;
this->Iter->show();
return;
}
}
cout << "无" << str << "的" << this->P.Sign << "人员信息" << "!" << endl;
}
void System<T>::add()
{
cout << "请输入" << P.Sign << "相关信息" << endl;
P.add();
this->person.push_back(P);
cout << P.Sign << "已输入成功!是否继续添加(Y继续,N退出):";
}
void System<T>::show()  //读文件
{
if (this->person.empty())
{
cout << "无" << P.Sign << "!请添加!" << endl;
return;
}
cout << "所有的" << P.Sign << "信息如下" << endl;
//cout << setfill('*') << setw(40) << endl;
P.out();
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
this->Iter->show();
}
}
public:
System()
{
string str;
string s = P.Sign + ".txt";
ifstream I;
I.open(s.c_str());
while (getline(I, str))
{
istringstream sstr(str);
P.Load(sstr, P);
this->person.push_back(P);
}
I.close();
}
~System()
{
string s = P.Sign + ".txt";
ofstream O;
O.open(s.c_str());
O.clear();
for (this->Iter = this->person.begin(); this->Iter != this->person.end(); this->Iter++)
{
this->Iter->Save(O);
};
O.close();
}

int System<T>::fun()
{
system("cls");
M(this->P.Sign);
int choose;
int num = 0;
char choice = 'Y';
cout << "请输入您的选择->";
cin >> choose;
if (choose == 5)
return 0;
switch (choose)
{
case 1:
num = 0;
while (true)
{
cout << "您在添加第" << ++num <<"个"<< P.Sign<<"信息" << endl;
this->add();
cin >> choice;
if (choice != 'Y'&&choice != 'y')
break;
}
break;
case 2:
this->show();
system("pause");
break;
case 3:
while (true)
{
this->detelte();
cout << "是否继续删除"<<this->P.Sign<<"(Y继续,N退出):";
cin >> choice;
if (choice != 'Y'&&choice != 'y')
break;
}
break;
case 4:
while (true)
{
this->modify();
cout << "是否继续修改"<<this->P.Sign<<"(Y继续,N退出):";
cin >> choice;
if (choice != 'Y'&&choice != 'y')
break;
}
break;
case 5:
break;
}
return 1;
}
};
void Technician::show()
{
this->calculate();
cout<<left;
cout << setw(12) << this->No << setw(10)<< this->Name << setw(5)<< this->G << setw(5)<< this->Sex << setw(8) <<this->S<<setw(10)
<<this->HoursWage<< setw(15)<<this->Hours<< setw(10)
<< this->EndS << setw(12) << this->Tel << setw(20) << this->Adr << endl;
}
void Technician::calculate()
{
this->EndS = this->S + this->Hours*this->HoursWage;
}
void Technician::add()
{
Employee::add();
while (true)
{
cout << "请输入每月的工作总时间(在0到360小时之间):";
cin >> this->Hours;
if (this->Hours >= 0 && this->Hours <= 360)
break;
else
cout<<"输入错误请重新输入! "<< endl;
}
while (true)
{
cout << "请输入每小时工资";
cin >> this->HoursWage;
if (this->HoursWage >= 0)
break;
else
cout << "输入错误请重新输入 " << endl;
}
}
Technician::Technician()
{
this->Sign="兼职技术人员";
}

void Employee::calculate()
{
this->EndS = S;
}
void Employee::Save(ofstream&out)
{
out << setw(12) << this->No << "\t" << this->Name << "\t" << this->G << "\t" << this->Sex << "\t" << this->EndS
<< "\t" << setw(12) << this->Tel << "\t" << setw(12) << this->Adr <<"\t"<<this->S<<"\t"<<this->Sign<< endl;
}

void Employee::show()
{
this->calculate();
cout<<left;
cout << setw(12) << this->No << setw(10) << this->Name << setw(5) << this->G << setw(5) << this->Sex << setw(5)
<< this->EndS << setw(12) << this->Tel << "\t" << setw(20) << this->Adr << endl;
}
void Employee::add()
{
cout << "请输入编号:";
cin >> this->No;
cout << "请输入姓名:";
cin >> this->Name;
while (true)
{
cout << "请输入级别:";
cin >> G;
if (G <= 'D'&&G >= 'A')
break;
else
cout << "此级别无效!请重新输入!" << endl;
}
while (true)
{
cout << "请输入性别(1为男,2为女):";
cin >> this->Sex;
if (this->Sex == "1")
{
this->Sex = "男";
break;
}
else if (this->Sex == "2")
{
this->Sex = "女";
break;
}
else if (this->Sex == "男" || this->Sex == "女")
break;
else
cout << "此性别无效!请重新输入!(1为男,2为女)" << endl;
}
while (true)
{
cout << "请输入底薪:";
cin >> this->S;
if (this->S >= 0)
break;
else
cout << "此工资无效!请重新输入!" << endl;
}
cout << "请输入电话号码" << endl;
cin >> this->Tel;
cout << "请输入地址" << endl;
cin >> this->Adr;
}
Employee::Employee()
{
this->Sign = "员工";
}
void menu()
{
cout << "********************************" << endl;
cout << "	1.公司员工管理" << endl;
cout << "	2.公司技工管理公司" << endl;
cout << "	3.公司销售人员管理" << endl;
cout << "	4.公司经理管理" << endl;
cout << "	5.公司销售经理管理" << endl;
cout << "	6.退出系统" << endl;
cout << "********************************" << endl;;
}
void M(string s)
{
cout << "********************************" << endl;
cout << "	1.增加公司" << s << "信息" << endl;
cout << "	2.显示公司" << s << "信息" << endl;
cout << "	3.删除公司" << s << "信息" << endl;
cout << "	4.更改公司" << s << "信息" << endl;
cout << "	5.退出当前菜单" << endl;
cout << "********************************" << endl;
}

void main()
{
while (true)
{
system("cls");
menu();
cout << "请输入您的选择->";
int choose;
cin >> choose;
switch (choose)
{
case 1:                   //初始化操作有“case”标签跳过  加上while循环就不会了
while (choose)
{
static System<Employee> PS;
choose=PS.fun();
}
break;
case 2:
while (choose)
{
static System<Technician> TS;
choose=TS.fun();
}
break;
case 3:
while (choose)
{
static System<Salesman> SS;
choose = SS.fun();
}
break;
case 4:
while (choose)
{
static System<Manager> MS;
choose=MS.fun();
}
break;
case 5:
while (choose)
{
static System<Salesmanager> SMS;
choose=SMS.fun();
}
break;
case 6:
exit(0);
default:
break;
}
}
}

后记

在写这个程序之前,我到网上找了很多类似的代码,功能有的较单一,于是我把网上的几个类似的源文件都看了一遍,并把功能也做了融合,代码做了模板处理。希望看到的大佬或者需要的小伙伴也可以提提意见哦。
编程之类关键就是要多看别人的代码,要多想,多自己编程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息