⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 employee.cpp

📁 简单工资系统,是学习C++的好例子
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//注
//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 + -