類指針 公司職工的信息管理程序 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;
}












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章