类指针 公司职工的信息管理程序 c++

实验 4 :公司职工的信息管理程序

一、实验内容:

使用面向对象的程序设计方法设计一个程序,实现对公司职工信息的管理。对公司职工信息的管理包括:增加职工信息、查找职工信息、显示所有职工信息、删除职工信息和退出等功能。

二、实验要求:

   1、程序要实现的功能:

    ①增加职工信息;

②查找职工信息;

③显示所有职工信息;

④删除职工信息;

⑤退出。

2、完成各成员函数。

①定义一个员工类(Staff)

     类的声明为:

class Staff

{

public:

          char name[10];//姓名

          char no[5];//职员号

          char department[10];职员所在的部门

          int wage;//工资

          char position[10];//职位

   Staff();

          Staff(char *name,char *no,char* dep,intwage,char* posi);

          ~Staff();

};

②定义一个公司类,类定义为

class Company

{

public :

          int count;

          Staff* add[30];//对象指针数组

          Staff *Sta;//临时对象指针

    Company();

          ~Company();

          bool AddStaff(char *name,char *no,char*dep,int wage,char* posi);//添加职工

          bool DeleteStaff(char *no);//删除职工

          bool FindStaff(char *no);//查找职工

          void DispAll();//显示所有职工

      };

添加时,若职工信息的工号相同则添加不成功。

功能2为查找职工信息,以职工号检索输入3时,显示所有的员工信息4,删除职工信息按0退出系统



建议用string写,毕竟简单太多了。

/*
	Name: company staff management program 
	Copyright: 
	Author: Jia daihua 
	Description: don't copy my work
*/

#include<iostream>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;
static int all = 0;
class Staff
{
public:
	string name;
    string no;
    string department;
    string wage;
    string position;
   	Staff();
    Staff(string name1,string no1,string department1,string wage1,string position1);
    ~Staff()
    {
	}
};

Staff::Staff()
{
	name = '0';
	no = '0';
	department = '0';
	wage = '0';
	position = '0';
}

Staff::Staff(string name1,string no1,string department1,string wage1,string position1)
{
	name =  name1;
	no = no1;
	department = department1;
	wage = wage1;
	position = position1;
}
class Company
{
public :
    int count;
    Staff* add[30];
    Staff *Sta;
    Company()
    {
    	memset(add, 0, sizeof(add));
    	Sta = 0;
    	count = 0;
	}
    ~Company()
    {
    	delete []Sta;
	}
	bool AddStaff(string name,string no,string dep,string wage,string posi);//添加职工
	bool DeleteStaff(string no);//删除职工
    bool FindStaff(string no);//查找职工
	void DispAll();//显示所有职工
};

//添加员工函数 
bool Company::AddStaff(string name,string no,string dep,string wage,string posi)
{
	int i = 0;
	Staff *example;
	example = new Staff(name,no,dep,wage,posi);
	for(i = 0; i < all; i ++)
	{
		Sta = (Staff*)add[i];
		if( Sta->no == no)
		{
			cout<<"添加失败,该员工已存在!"<<endl;
			return 0;
		}
	}
	if(all <30)
	{
		add[all] = example;
		cout<<"添加成功。"<<endl;
		all++;
		return 1;
	}
	return 1;
}

//通过员工号找到员工并输出员工信息 
bool Company::FindStaff(string no)
{
	int i,ju=0;
	for(i = 0; i < all; i ++)
	if(add[i]->no == no)
	{
		cout<<"该员工已找到:"<<endl;
		cout<<"姓名"<<setw(16)<<"员工号"<<setw(16)<<"部门"<<setw(16)<<"工资"<<setw(16)<<"职位"<<endl;
		cout<<add[i]->name<<setw(16)<<add[i]->no<<setw(16)<<add[i]->department<<setw(16)<<add[i]->wage<<setw(16)<<add[i]->position<<endl;
		ju = 1;
		return 1;
	}
	if(ju == 0)
	{
		cout<<"您是不是记错了工号或者是输错了!"<<endl;
		return 0;
	}
	return 1;
}

//对制定员工号所对应的员工信息进行删除 
bool Company::DeleteStaff(string no)
{
	int i,ju = 0;
	for(i = 0; i < all; i++)
	{
		if(add[i]->no == no)
		{
			for(int j = i;j < all; j++)
				add[j] = add[j + 1];
			all--;
			cout<<"删除成功!"<<endl;
			return 1;
		}
	}
	if(ju == 0)
	{
		cout<<"您是不是记错了工号或者是输错了!"<<endl;
		return 0;
	}
	return 1;
}
//输出目前所有的员工信息 
void Company::DispAll()
{
	int i;
	cout<<"姓名"<<setw(16)<<"工号"<<setw(16)<<"部门"<<setw(16)<<"工资"<<setw(16)<<"职位"<<endl;
	for(i = 0; i < all; i++)
		cout<<add[i]->name<<setw(16)<<add[i]->no<<setw(16)<<add[i]->department<<setw(16)<<add[i]->wage<<setw(16)<<add[i]->position<<endl;
}

void welcome()
{
	cout<<"     $-------------------------------------------------------$"<<endl;
	cout<<"     |                                                       |"<<endl;
	cout<<"     |              **欢迎进入公司职员管理系统**             |"<<endl;
	cout<<"     |                         BY:一个瑟瑟发抖的弱鸡        |"<<endl;
	cout<<"     $-------------------------------------------------------$"<<endl;
	return;
}


//功能选择函数 
int select()																					
{
	int r, loop = 1;
	cout<<endl<<"请选择您的操作:"<<endl;
	cout<<"               1 增加员工信息"<<endl;
	cout<<"               2 查找员工信息"<<endl;
	cout<<"               3 显示所有员工信息"<<endl;
	cout<<"               4 删除员工信息"<<endl;
	cout<<"               0 退出"<<endl;
	cout<<"请输入序号(0~4):"<<endl;

	while(loop)
	{
		
		cin>>r;
		if(r >= 0 && r <= 4)
			break;
		else
			cout<<"输入有误,请重新输入:";
	}
	
	return r;
}

int main()
{
	Company c;
	int j,k,l,r, loop = 1;
	string name,no,position,department,wage;
	
	welcome();
	while(loop)
	{
		r = select();
		if(r == 0)
			break;
		else if(r == 1)
		{
			cout<<"请输入姓名(换行键结束):"<<endl;
			cin>>name;
			cout<<"请输入工号(换行键结束):"<<endl;
			cin>>no;
			cout<<"请输入部门(换行键结束):"<<endl;
			cin>>department;
			cout<<"请输入工资(换行键结束):"<<endl;
			cin>>wage;
			cout<<"请输入职位(换行键结束):"<<endl;
			cin>>position;
			c.AddStaff(name,no,department,wage,position);
		}
		else  if(r == 2)
		{
			cout<<"请输入您要查找的员工号:"<<endl;
			cin>>no;
			c.FindStaff(no);
		}
		else if(r == 3)
		{
			c.DispAll();
		}
		else if(r == 4)
		{
			cout<<"请输入您要删除的员工的员工号:"<<endl;
			cin>>no;
			c.DeleteStaff(no);
		}
	}
	return 0;
}












  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值