C++公司类镶嵌员工类

写得不好之处,希望多多指点

 运行效果如下:

 

功能栏可以无限循环

 

头文件:

#include<iostream>
#include<string>
using namespace std;

员工类:

//员工类 
class Employee
{
	private:
		string name;
		int age;
		string ID;
		float salary;
		class Employee* next;
	public:
		Employee():next(NULL){}//构造函数初始化 
		Employee* get_next(){return next;} 
		void create(class Employee* t);//创建结点 
		string get_name(class Employee* t);//get到姓名 
		int get_age(class Employee* t);//get到年龄 
		string get_ID(class Employee* t);//get到工号 
		float get_salary(class Employee* t);//get到工资 
};

 员工类函数实现如下:

 

//输入员工信息 
void Employee::create(class Employee* t)//尾插法 输入员工信息 
{
	while(t->next!=NULL)
	{
		t=t->next;
	}
	Employee* node=new Employee;
	t->next=node;
	node->next=NULL;
	cout << "请输入员工信息" << endl;
    cout << "员工姓名: " << endl;
    cin >>node->name;
    cout << "员工年龄: " << endl;
    cin >>node->age;
    cout << "员工工号: " << endl;
    cin >>node->ID;
    cout << "员工工资: " << endl;
    cin >>node->salary;
}


string Employee::get_name(class Employee* t)//get到每个员工的姓名 
{
	while(t->next!=NULL)
	{
		return t->next->name;
		t=t->next;
	}
	return 0; 
}

int Employee::get_age(class Employee* t)//get到每个员工的年龄 
{
	while(t->next!=NULL)
	{
		return t->next->age;
		t=t->next;
	}
	return 0; 
}

float Employee::get_salary(class Employee* t)//get到每个员工的工资
{
	while(t->next!=NULL)
	{
		return t->next->salary;
		t=t->next;
	}
	return 0; 
}

string Employee::get_ID(class Employee* t)//get到每个员工的工号 
{
	while(t->next!=NULL)
	{
		return t->next->ID;
		t=t->next;
	}
	return 0; 
}

 

 公司类:

//公司类 
class Company
{
	private:
		string comname;
		string comrelevant;
		float comexpend;
		float comincome;
		int number;
		Employee* emp;
	public:
		Company(string a,string b,float c,float d,int e,Employee* f);//构造函数
		~Company(){} //析构函数 
		void show_company();//显示公司信息 
		void show_employee(Employee* emp);//显示最开始number个员工的信息 
		void search_name(Employee* emp);//用输入姓名的方式搜索该员工信息 
		void delete_name(Employee* emp);//用输入姓名的方式删除该员工信息 
		void plus_salary(Employee* emp);//计算总工资 
};

 

公司类函数实现如下: 

Company::Company(string a,string b,float c,float d,int e,Employee* f):comname(a),comrelevant(b),
comexpend(c),comincome(d),number(e),emp(f){} //构造函数初始化 

void Company::show_company()//显示公司信息 
{
	cout<<"公司名字:"<<comname<<'\t'<<"公司相关信息:"<<comrelevant<<'\t'<<
	"公司员工工资总支出:"<<comexpend<<'\t'<<"公司收入:"<<comincome<<'\t'<<
	"公司员工个数:"<<number<<endl;
}


void Company::show_employee(Employee* emp)//显示员工信息 
{
	int i=0;
	while(emp->get_next()!=NULL)
	{
		i++;
		cout<<i<<"、"<<"姓名:"<<emp->get_next()->get_name(emp)<<'\t';
		cout<<"年龄:"<<emp->get_next()->get_age(emp)<<'\t';
		cout<<"工号:"<<emp->get_next()->get_ID(emp)<<'\t';
		cout<<"工资:"<<emp->get_next()->get_salary(emp)<<endl;
		emp=emp->get_next();
	}
}


void Company::search_name(Employee* emp)//以输入名字的方式查询该员工信息 
{
	string n;
	cout<<"请输入你想要查询的名字:";
	cin>>n;
	int count=0;
	while(emp->get_next()!=NULL)
	{
		if(emp->get_next()->get_name(emp)==n)
		{
			count=1;
			cout<<"姓名:"<<emp->get_next()->get_name(emp)<<'\t';
		cout<<"年龄:"<<emp->get_next()->get_age(emp)<<'\t';
		cout<<"工号:"<<emp->get_next()->get_ID(emp)<<'\t';
		cout<<"工资:"<<emp->get_next()->get_salary(emp)<<endl;
		break;
		}
		emp=emp->get_next();
	}
	if(count==0)
	{
		cout<<"对不起,查无此人!"<<endl;
		cout<<endl;
	}
}

void Company::delete_name(Employee* emp)//输入名字删除该员工相关信息 
{
	cout<<"请输入你想要删除的姓名:";
	string n;
	cin>>n;
	int count=0;
	while(emp->get_next()!=NULL)
	{
		if(emp->get_next()->get_name(emp)==n)
		{
			count=1;
			Employee* temp=emp->get_next();
			Employee* t=emp->get_next()->get_next();
			emp=t;
			delete temp;
			temp=NULL;
			cout<<"删除成功!"<<endl;
			cout<<endl;
			break;
		}
		emp=emp->get_next();
	}
	if(count==0)
	{
		cout<<"对不起,查无此人!"<<endl;
		cout<<endl;
	}
}


void Company::plus_salary(Employee* emp)//计算公司员工工资总支出 
{
	float sum=0;
	while(emp->get_next()!=NULL)
	{
		sum+=emp->get_next()->get_salary(emp);
		emp=emp->get_next();
	}
	cout<<"公司所有员工的工资支出总和为:"<<sum<<endl;
}

 main函数:

 

int main()
{
	Employee* t=new Employee;
	string cname;
	string crelevant;
	float cexpend;
	float cincome;
	int num;
	cout<<"********************"<<endl;
	cout<<"公司的相关信息"<<endl; 
	cout<<"********************"<<endl;
	cout<<"公司名:";
	cin>>cname;
	cout<<"公司信息:";
	cin>>crelevant;
	cout<<"公司员工工资支出:";
	cin>>cexpend;
	cout<<"公司收入:";
	cin>>cincome; 
	cout<<"公司员工个数:";
	cin>>num;
	Company com(cname,crelevant,cexpend,cincome,num,t);
	cout<<"************"<<endl;
	cout<<"公司系统如下"<<endl; 
	cout<<"************"<<endl;
	cout<<endl;
	cout<<"公司信息"<<endl;
	com.show_company();
	int i;
	for(i=0;i<num;i++)
	{
		t->create(t);
	}
	cout<<endl;
	cout<<"------------"<<endl;
	cout<<"显示所有员工的信息"<<endl;
	cout<<"------------"<<endl;
	com.show_employee(t);
	cout<<endl;
	cout<<"******************"<<endl; 
	cout<<"******************"<<endl; 
    while(1)
    {
    	cout << "1.按照姓名查询员工信息" << endl;
        cout << "2.按姓名删除员工信息" << endl;
        cout << "3.所有员工的工资总额"<<endl;
        cout << "4.增加新员工"<<endl; 
        cout << "0.退出" << endl;
        cout <<"---------------------"<< endl;
        cout << "请输入你想选择的功能:" << endl;
        int number;
        cin >> number;
        cout<<"--------------------"<<endl;
        if(number==0)
        break;
    switch(number)
   {
        case 1:
            com.search_name(t);break;
        case 2:
    	    com.delete_name(t);break;
        case 3:
    	    com.plus_salary(t);break;
    	case 4:
    		{
    			int n;
    			cout<<"请输入需要增加的成员个数:";
    			cin>>n;
    			for(i=0;i<n;i++)
    			{
    				t->create(t);
				}
				com.show_employee(t);
				cout<<endl<<"恭喜你成功地增加了"<<n<<"个成员"<<endl;
				break;
			}
        default: 
	        cout<<"请输入正确的操作指令"<<endl;
	        cout<<"**************************"<<endl; 
   }
   }
	return 0;
}

新手发博,希望大佬们多多指点!><!!!

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值