?? employee.cpp
字號:
//注
//vc++6.0下編譯通過
//c-free不能通過。原因是iso_base::left在c-free里沒有.也許要寫成 iso::left才行.
//如果改進的話
//1.可以寫函數類,比如比較大小的。然后傳給通用算法,這樣就省去了寫sort的麻煩。--7月5日
//
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include <stdlib.h>
#include <exception>
#include <list>
#include <algorithm>
using namespace std;
/**********************************************/
/**********************************************/
class MyException:public exception{
private:
string _ErrorCode;
protected:
void setErrorCode(string code){_ErrorCode=code;};
public:
void printErrorCode(){cout<<_ErrorCode;}
};
/**********************************************/
class YeahException:public MyException{
public:
YeahException(){setErrorCode("年份錯誤\n");};
} ;
/**********************************************/
class MonthException:public MyException{
public:
MonthException(){setErrorCode("月份錯誤\n");};
} ;
/**********************************************/
class DayException:public MyException{
public:
DayException(){setErrorCode("這個世界沒有出現過這一天\n");};
} ;
/**********************************************/
class SexException:public MyException{
public:
SexException(){setErrorCode("性別錯誤\n");};
} ;
/**********************************************/
/**********************************************/
/**********************************************/
/**********************************************/
class BDay
{
private:
int _year;
int _month;
int _day;
string inttostr(int i)
{
int c;
char ch;
string str;
if (i==0) str="0";
while (i!=0)
{
c=i%10;
ch='0'+c;
str=str+ch;
i=i/10;
}
return reverse(str);
}
string& reverse(string& str)
{
char ch;
int i=0;
while (i!=str.length()/2){
ch=str[str.length()-1-i];
str[str.length()-1-i]=str[i];
str[i]=ch;
i++;
}
return str;
}
public:
int getYear(){return _year;}
int getMonth(){return _month;}
int getDay(){return _day;}
BDay(const BDay &o)
{
_year=o._year;
_month=o._month;
_day=o._day;
}
void setYear(int year)
{
if (year>9999||year<-9999){
YeahException e;
throw e;
}
_year=year;
}
void setMonth(int month)
{
if (month>12||month<1){
MonthException e;
throw e;
}
_month=month;
}
void setDay(int day)
{
if (day<1||day>31) {
DayException e;
throw e;
}
if ((_month==2)&&(day==29)&&(!((_year%4==0&&_year%100!=0)||(_year&400==0)))){
DayException e;
throw e;
}
switch(_month){
case 2:
if (day>28){
DayException e;
throw e;
}
break;
case 4: case 6: case 9: case 11:
if(day>30){
DayException e;
throw e;
}
break;
}
_day=day;
}
BDay(){_year=1983;_month=6;_day=30;}
BDay(int year,int month,int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
BDay(string day)
{
int i=day.find("-",0);
/*
if (i>=4)
{
DayException e;
throw e;
}
*/
char chyear[5];
day.copy(chyear,i,0);
setYear(atoi(chyear));
int j=day.find("-",i+1);
char chmonth[3];
day.copy(chmonth,j-i-1,i+1);
setMonth(atoi(chmonth));
i=day.find("-",j+1);
char chday[3];
day.copy(chday,i-j-1,j+1);
setDay(atoi(chday));
}
//////////////////////////
string getBirthday()
{
return inttostr(_year)+"-"+inttostr(_month)+"-"+inttostr(_day);
}
BDay operator=(const BDay &o)
{
_year=o._year;
_month=o._month;
_day=o._day;
return *this;
}
} ;
/**********************************************/
/**********************************************/
class CEmployee {
private:
static int Class_id;
int _id;//存放編號
string _name;
string _sex;
BDay _birthday; //顯示格式:1980-1-1,請自己設計并實現類BDay.
//float pay;
public:
static void setClass_id(int i){Class_id =i;}
string getSex(){return _sex;};
string getBirthday(){return _birthday.getBirthday();};
int operator<(CEmployee& o)
{
if (computePay()>o.computePay()){return -1;}
else
{
if(computePay()<o.computePay()){return 1;}
return 0;
}
};
int operator>(CEmployee& o)
{
if (computePay()>o.computePay()){return -1;}
else
{
if(computePay()>o.computePay()){return 1;}
return 0;
}
};
int getid()
{
return _id;
}
CEmployee(const string& nm, const string& sex, const BDay& bd);//構造函數
string EmpolyeeName();//返回雇員名字
virtual float computePay(){return 0;};//計算雇員薪水
virtual string getPosition(){return "雇員";}
virtual void printInfo(ostream& stream=cout)//初始值的作用就是寫了一堆東西,發現少了個參數,然后加進去,給它個默認的值
{
stream
<<setiosflags(ios_base::left)
<<setw(10)<<getid()
<<setw(15)
<<EmpolyeeName()
<<setw(10)<<getPosition()
<<setw(10)<<getSex()
<<setw(15)<<getBirthday()
<<setiosflags(ios_base::left)<<setw(10)<<computePay()
<<endl;
}
static void printInfoHint(ostream& stream=cout)
{
stream
<<setiosflags(ios_base::left)
<<setw(10)<<"編號"
<<setw(15)
<<"姓名"
<<setw(10)<<"職位"
<<setw(10)<<"性別"
<<setw(15)<<"生日"
<<setiosflags(ios_base::left)<<setw(10)<<"薪金"<<endl;
}
};
int CEmployee::Class_id=1;
CEmployee::CEmployee(const string& nm, const string& sex, const BDay& bd)
{
_name=nm;
if (sex!="男"&&sex!="女")
{
SexException e;
throw e;
}
_sex=sex;
_birthday=bd;
_id=CEmployee::Class_id;
CEmployee::Class_id++;
}
/*
CEmployee::CEmployee(const CEmployee& o)
{
_name=o._name;
_sex=o._sex;
_birthday=o._birthday;
}
*/
string CEmployee::EmpolyeeName()
{
return _name;
}
/**********************************************/
class CManager : public CEmployee{
private :
float _salary;//固定月薪
public :
CManager(const string& nm, const string& sex, const BDay& bd):CEmployee(nm,sex,bd){_salary=0.0;}; //初始化父類屬性值,并初始化月薪為0.0
void setSalary(float salary){_salary=salary;}; //修改CManager月薪值
virtual float computePay(){return _salary;};
virtual string getPosition(){return "經理";}
};
/**********************************************/
class CWage : public CEmployee {
private :
float _wage;//時薪
float _hours;//小時
public :
CWage(const string& nm, const string& sex, const BDay& bd):CEmployee(nm,sex,bd){_wage=0; _hours=0;};//自己編寫CWage的恰當的構造函數。注意在生成一個CWage對象時構造函數必須初始化該對象的_wage, _hours屬性值為0.0,0.0。
void setWage(float wg){_wage=wg;};// 修改對象的時薪值
void setHours(float hrs){_hours=hrs;};// 修改對象的工作時間
virtual float computePay(){return _wage*_hours;};// 計算薪水,Cwage的薪水= 工作時間*時薪
virtual string getPosition(){return "時工";}
};
/**********************************************/
class CSales : public CWage{
private :
float _comm; //每件提成
float _sale; //銷售數量
public :
CSales(const string& nm, const string& sex, const BDay& bd):CWage(nm,sex,bd){_comm=0;_sale=0;};//初始化其父類的所有屬性值,并初始化_comm, _sale為0.0,0.0
void setCommission(float comm){_comm=comm;}; //修改對象的提成
void setSales(float sale){_sale=sale;}; //修改對象的銷售數量
virtual float computePay(){return CWage::computePay()+_comm*_sale;};// 計算薪水,CSales薪水= 工作時間*時薪+ 每件提成*銷售數量
virtual string getPosition(){return "銷售";}
};
/**********************************************/
/**********************************************/
class IdoIt // 接口
{
public:
virtual void doIt()=0;
};
/**********************************************/
class Menu:public IdoIt{
private:
int id;
string _title;
protected:
vector<IdoIt *> _op ;
vector<string> _hint;
public:
Menu(){id=0;}
void addTitle(string str){_title=str;};
void addMenu(string hint,IdoIt *op)
{
_op.push_back(op);
_hint.push_back(hint);
++id;
}
void show()
{
system("cls");
//為了菜單的美觀,所以打回車
for (int spa=0;spa<(15-id)/2;++spa){
cout<<endl;
}
cout<<" "<<_title<<endl<<endl;//輸出標題
for(int i=0;i<_hint.size();++i){
cout<<" "<<i+1<<"."<<_hint[i]<<endl;//i+1的原因在于atoi()出錯返回0,所以不應該有0的菜單
}
//打印請選擇
if (id>0){
cout<<endl<<" "<<"請選擇:"<<1<<"-"<<id<<" " ;
}
else
{
cout<<" "<<"沒有菜單"<<endl ;
}
//對選擇進行操作
int choice=0;
string str;
cin>>str;
//choice=cin.get()-'0';
choice=atoi(str.c_str());
if(choice>id||choice<1){
// (*this).show();
}
else
{
_op[choice-1]->doIt();//choice-1的原因在于id與顯示的序號的不同,
}
(*this).show();
}
virtual void doIt()
{
(*this).show();
}
} ;
/**********************************************/
//功能模塊類
class CFunction:public IdoIt
{
private:
void (*_p)();//函數指針聲明
static list<CEmployee *> _emplyeelist;
static const string TOP;
static const string LEFT;
//print,save,report可以合并,不過在private里,所以無所謂
static void print()
{
system("cls");
cout<<endl;//為了美觀
CEmployee::printInfoHint();//列表名
list<CEmployee *>::iterator p=CFunction::_emplyeelist.begin();
int pagectrl=0; //用來控制換頁
while(p!=_emplyeelist.end())
{
if (pagectrl==20){
cout<<"下一頁";
system("pause");
system("cls");
pagectrl=0;
cout<<endl;//為了美觀
CEmployee::printInfoHint();//列表名
}
pagectrl++;
(*p)->printInfo();
p++;
}
system("pause");
};
static void report()
{
ofstream salarydata;
salarydata.open(CFunction::salaryfilename.c_str());
CEmployee::printInfoHint(salarydata);//列表名
list<CEmployee *>::iterator p=CFunction::_emplyeelist.begin();
while(p!=_emplyeelist.end())
{
(*p)->printInfo(salarydata);
p++;
}
salarydata.close();
}
static string getnameformline(char * line)
{
string str(line);
return str.substr(10,10);
}
static string getsexformline(char * line)
{
string str(line);
return str.substr(35,2);
}
static string getbirthformline(char * line)
{
string str(line);
return str.substr(45,10);
}
static float getpayformline(char * line)
{
string str(line);
return atof(str.substr(60,10).c_str());
}
static string getpositionformline(char * line)
{
string str(line);
return str.substr(25,4);
}
static void load()
{
ifstream f;
char line[101];
f.open(CFunction::employeefilename.c_str(),ios::in);
while(f.good())
{
f.getline(line,100);
string str(line);
if (f.good()) {
if (CFunction::getpositionformline(line)=="經理")
{
CManager *m=new CManager(CFunction::getnameformline(line),CFunction::getsexformline(line),CFunction::getbirthformline(line));
m->setSalary(getpayformline(line));
CFunction::push_back(m);
}
if (CFunction::getpositionformline(line)=="時工")
{
CWage *m=new CWage(CFunction::getnameformline(line),CFunction::getsexformline(line),CFunction::getbirthformline(line));
//因為只關心薪金,所以下邊的實現是可行的
m->setHours(1);
m->setWage(getpayformline(line));
CFunction::push_back(m);
}
if (CFunction::getpositionformline(line)=="銷售")
{
CSales *m=new CSales(CFunction::getnameformline(line),CFunction::getsexformline(line),CFunction::getbirthformline(line));
//因為只關心薪金,所以下邊的實現是可行的
m->setHours(1);
m->setWage(getpayformline(line));
CFunction::push_back(m);
}
}
// if (f.good()) cout<<line<<endl<<<<endl;
}
f.close();
}
static void save()
{
ofstream salarydata;
salarydata.open(CFunction::employeefilename.c_str());
// CEmployee::printInfoHint(salarydata);//列表名
list<CEmployee *>::iterator p=CFunction::_emplyeelist.begin();
while(p!=_emplyeelist.end())
{
(*p)->printInfo(salarydata);
p++;
}
salarydata.close();
}
static void sortbyid()
{
list<CEmployee *>::iterator begin,end,p1,p2,temp;
begin=CFunction::_emplyeelist.begin();
end=CFunction::_emplyeelist.end();
//因為用迭代器,有些指針有的操作迭代器沒有,所以下邊的代碼看起來可能會有些奇怪
//而且即使用for,判斷停止的時候一樣是用!=。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -