C/C++职工管理系统

我们在入门C++后都会自己设计一个系统,来检验自己的c++基础是否牢固。而博主最近在某站上跟着黑马一起看视频学习c++,其中有个职员管理系统,博主也跟着敲了一下,接下来让我们一起来看看程序吧。

在设计一个系统之前我们应该思考系统的结构,系统的功能以及客户的需求等等。我设计的这个系统有以下功能:添加职员信息,查询职员信息,删除职员信息,修改职员信息,显示职员信息,排序,退出以及清空数据。

有任何问题请在下面评论,谢谢。

接下来直接上代码:

下面展示 文件存放目录
文件目录

// 下面展示 `具体文件代码`。

boss.h文件

// An highlighted block
#pragma once
#include <iostream>
#include <string>
#include "worker.h"
using namespace std;

class boss:public worker
{
	public:
		boss(int num,string name,int id);
		virtual void show();
		virtual string get_id();
		
};

manage.h文件

// An highlighted block
#pragma once
#include <iostream>
#include <string>
#include "worker.h"
using namespace std;

class manage:public worker
{
	public:
		manage(int num,string name,int id);
		virtual void show();
		virtual string get_id();
		
};

employee.h文件

// An highlighted block
#pragma once
#include <iostream>
#include <string>
#include "worker.h"
using namespace std;

class employee:public worker
{
	public:
		employee(int num,string name,int id);
		virtual void show();
		virtual string get_id();
		
};

workmanage.h文件

// An highlighted block
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "worker.h"
#include "employee.h"
#include "manage.h"
#include "boss.h"
#define FILENAME "empFile.txt"
using namespace std;

class workmanage
{
	public:
		void show_menu();
		void exit_xt();
		void add_xt();
		void show_xt();
		void save_xt();
		void read_xt();
		void get_num();
		void sort_xt();
		void clear_xt();
		void find_xt();
		void delete_xt(); 
		void update_xt(); 
		workmanage();
		~workmanage();
		
		int sum;
		worker **m_array;
		bool m_file;
 };

worker.h文件

// An highlighted block
#pragma once
#include <iostream>
#include <string>
using namespace std;

class worker
{
	public:
		virtual void show()=0;
		virtual string get_id()=0;
		virtual ~worker();
		
		int m_num;
		string m_name;
		int m_id;
}; 

boss.cpp文件

// An highlighted block
#include "boss.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

	boss::boss(int num,string name,int id)
	{
		this->m_num=num;
		this->m_name=name;
		this->m_id=id;
	}
	void boss::show()
	{
		cout << "职工编号:"<< this->m_num 
			 <<"\t职工姓名:"<<this->m_name
			 <<"\t职工部门:"<<this->get_id()
			 <<endl; 
	}
	string boss::get_id()
	{
		return string("老板"); 
	}

manage.cpp文件

// An highlighted block
#include "manage.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

	manage::manage(int num,string name,int id)
	{
		this->m_num=num;
		this->m_name=name;
		this->m_id=id;
	}
	void manage::show()
	{
		cout << "职工编号:"<< this->m_num 
			 <<"\t职工姓名:"<<this->m_name
			 <<"\t职工部门:"<<this->get_id()
			 <<endl; 
	}
	string manage::get_id()
	{
		return string("经理"); 
	}

employee.cpp文件

// An highlighted block
#include "employee.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

	employee::employee(int num,string name,int id)
	{
		this->m_num=num;
		this->m_name=name;
		this->m_id=id;
	}
	void employee::show()
	{
		cout << "职工编号:"<< this->m_num 
			 <<"\t职工姓名:"<<this->m_name
			 <<"\t职工部门:"<<this->get_id()
			 <<endl; 
	}
	string employee::get_id()
	{
		return string("普通职工"); 
	}

main.cpp文件

// An highlighted block
#include "boss.h"
#include "employee.h"
#include "manage.h"
#include "worker.h"
#include "workmanage.h"

using namespace std;
int main()
{
	int ch;
	workmanage wm;
	
	
	while(1)
	{
	wm.show_menu();
	cout << "请输入要进行的操作:";
	cin >> ch;
	switch(ch)
	{
		case 1:wm.add_xt();break;
		case 2:wm.show_xt();break;
		case 3:wm.delete_xt();break;
		case 4:wm.update_xt();break;
		case 5:wm.sort_xt();break;
		case 6:wm.clear_xt();break;
		case 7:wm.find_xt();break;
		case 0:wm.exit_xt();break;
		default:cout <<"输入错误!"<<endl;
	 }
	 system("pause");
	 system("cls");
	}
	return 0;
}

workmanage.cpp文件

// An highlighted block
#include "workmanage.h"

workmanage::workmanage(){
	fstream file;
	char ch;
	
	file.open(FILENAME,ios::in);
	
	if(!file.is_open())
	{
		this->sum=0;
		this->m_array=NULL;
		this->m_file=true;
	}
	else
	{	
		file >> ch; 
		if(file.eof())
		{
			this->sum=0;
			this->m_array=NULL;
			this->m_file=true;
		}
		else
		{
			int i=0;
			this->get_num();
			this->m_array=new worker * [this->sum];
 			int num,id;
 			string name;
			file.seekg(0);
		 	while(file>>num&&file>>name&&file>>id)
		 	{
		 		worker *wk=NULL;
		 				
				 switch(id)
				 {
				 	case 1:wk=new boss(num,name,id);break;
				 	case 2:wk=new manage(num,name,id);break;
				 	case 3:wk=new employee(num,name,id);break;
				 	default:cout <<"读取数据错误!"<<endl; 
				 }
				
				 this->m_array[i]=wk;
				 i++;
			 }
		}
	}
	file.close(); 
	

	
}

workmanage::~workmanage()
{
	if(this->m_array!=NULL)
	{
		delete[] m_array;
		this->m_array=NULL;
	 } 
}
void workmanage::show_menu()
 {
 	cout <<"--------------------------------------------------"<<endl;	
 	cout <<"-------------------欢迎光临-----------------------"<<endl;	
 	cout <<"---------------1.增加职工人数---------------------"<<endl;	
 	cout <<"---------------2.显示职工信息---------------------"<<endl;	
 	cout <<"---------------3.删除职工信息---------------------"<<endl;	
 	cout <<"---------------4.修改职工信息---------------------"<<endl;	
 	cout <<"---------------5.职工信息排序---------------------"<<endl;	
 	cout <<"---------------6.清空职工信息---------------------"<<endl;	
 	cout <<"---------------7.查找职工信息---------------------"<<endl;	
 	cout <<"---------------0.退出系统-------------------------"<<endl;	
 	cout <<"--------------------------------------------------"<<endl;		
 }
 
 void workmanage::exit_xt()
 {
 	exit(0);
 	system("pause");
 }
 
 void workmanage::add_xt()
 {
 	int ch=0,i,num,id;
	string name;  
 	cout<<"请输入要增加的职工人数:";
 	cin >> ch;
 	
 	worker **array=new worker*[this->sum+ch];
 	if(this->m_array!=NULL)
	 {
	 	for(i=0;i<this->sum;i++)
	 	{
	 		array[i]=this->m_array[i];
		 }
	}
 	for(i=0;i<ch;i++)
 	{
 		cout <<"请输入第"<<i+1<<"个的职工编号:";
		cin >>  num;
		
		cout <<"请输入第"<<i+1<<"个的职工姓名:";
		cin >>  name;
		
		cout <<"请输入第"<<i+1<<"个的职工部门:"<<endl;
		cout <<"1. 老板"<<endl;
		cout <<"2. 经理"<<endl;
		cout <<"3. 普通员工"<<endl;
		cin >>  id;
		
		worker *wk=NULL;
		switch(id)
		{
			case 1:wk=new boss(num,name,id);break;
			case 2:wk=new manage(num,name,id);break;
			case 3:wk=new employee(num,name,id);break;
			default: cout<<"输入错误!"<<endl;return;
			
		}
			
 		array[this->sum+i]=wk;
	 }
	 delete[] this->m_array;
	 this->m_array=array;
	 this->sum=this->sum+ch;
	 this->save_xt();
	 cout <<"添加完毕!"<<endl;
	 
 	
 }
 
 
 void workmanage::show_xt()
 {
 	int i;
 	//this->read_xt();
 	for(i=0;i<this->sum;i++)
 	{
 	 this->m_array[i]->show();
 	}
 }
 
 
 void workmanage::save_xt()
 {
 	ofstream file;
 	int i;
 	file.open(FILENAME,ios::out);
 	if(!file.is_open())
 	{
 		cout<<"文件打开失败!"<<endl;
 		return;
	 }
	 
	 for(i=0;i<this->sum;i++)
		{
			if( this->m_array[i]!=NULL)
			{
			file << this->m_array[i]->m_num << ' '
				 << this->m_array[i]->m_name << ' '
			 	 << this->m_array[i]->m_id << endl;
			}
		}
		file.close();
 }
 
 void workmanage::read_xt()
 {
 	ifstream file;
 	int i=0;
 	int num,id;
 	string name;
 	file.open(FILENAME,ios::in);
 	if(!file.is_open())
 	{
 		cout<<"文件打开失败!"<<endl;
 		return;
	 }
	 worker **array=new worker*[3];
 	while(file>>num&&file>>name&&file>>id)
 	{
 		worker *wk=NULL;
		 switch(id)
		 {
		 	case 1:wk=new boss(num,name,id);break;
		 	case 2:wk=new manage(num,name,id);break;
		 	case 3:wk=new employee(num,name,id);break;
		 	default:cout <<"读取数据错误!"<<endl; 
		 }
		
		 array[i]=wk;
		 i++;
	 }
	 this->m_array=array;
	 this->sum=i;
	 file.close();
 }
 
 void workmanage::get_num()
 {
 	ifstream file;
 	int i=0;
 	int num,id;
 	string name;
 	file.open(FILENAME,ios::in);
 	if(!file.is_open())
 	{
 		cout<<"文件打开失败!"<<endl;
 		return;
	 }
 	while(file>>num&&file>>name&&file>>id)
 	{
		 i++;
	 }

	 this->sum=i;
	 file.close();
 }
 
 void workmanage::sort_xt()
 {
 	int i,j;
 	worker *wk=NULL;
 	for(i=0;i<this->sum;i++)
	 {
	 	for(j=0;j<this->sum-i-1;j++)
	 	if(this->m_array[j]->m_num>this->m_array[j+1]->m_num)
	 	{
			wk=this->m_array[j];
			this->m_array[j]=this->m_array[j+1];
			this->m_array[j+1]=wk;
		 }
	 }
	 this->show_xt();
 }
 
 void workmanage::clear_xt()
 {
 	cout<<"是否清空?:是(Y)/否(N)"<<endl;
	char ch;
	int i;
	cin >>ch;
	if(ch=='y'||ch=='Y')
	{
		ofstream file(FILENAME,ios::trunc);
		file.close();
		for(i=0;i<this->sum;i++)
		{
			if(this->m_array[i]!=NULL)
				delete this->m_array[i];
		}
		delete[] this->m_array;
		this->sum=0;
		this->m_file=true; 
		this->m_array=NULL;
		
	}
	else
	{
		cout <<"清空失败!"<<endl;
	}
 }
 
 
 void workmanage::find_xt()
 {
 	int num,i,n=0;
 	cout <<"请输入要查找职工的编号:";
	cin >> num; 
	for(i=0;i<this->sum;i++)
	{
		if(this->m_array[i]->m_num==num)
		{
			cout <<"查找到该职工!"<<endl;
			this->m_array[i]->show();
			n=1;
		}
	}
 	if(n!=1)
 	{
 		cout<<"未查找到该职工!"<<endl;
	 }
 	
 }
 
 void  workmanage::delete_xt()
 {
 	cout <<"请输入要删除职工的编号:";
 	int num,j,i,n;
	cin >> num;
	for(i=0;i<this->sum;i++)
	{
		if(this->m_array[i]->m_num==num)
		{
			cout <<"查找到该职工!"<<endl;
			this->m_array[i]->show();
			n=1;
			break;
		}
	}

 	if(n!=1)
 	{
 		cout<<"未查找到该职工!"<<endl;
	 }
	 else
	 {

			if(i==this->sum-1)
			{
				delete this->m_array[this->sum-1];
				this->m_array[this->sum-1]=NULL;
			}
			else
			{
				for(j=i;j<this->sum-1;j++)
					{
						this->m_array[j]=this->m_array[j+1];
					}
				//delete this->m_array[this->sum-1];
				this->m_array[this->sum-1]=NULL;
			}
	
	
			this->sum--;
	 }
 	
 	this->save_xt();
 	cout <<"删除成功!"<<endl;
 	
 }
 
 void workmanage::update_xt()
 {
 	cout <<"请输入要修改职工的编号:";
 	int num,j,i=0,n=0,a[3]={0},id;
 	string name;
 	char ch=' ';
 	worker *wk=NULL;
	cin >> num;
	for(j=0;j<this->sum;j++)
	{
		if(this->m_array[j]->m_num==num)
		{
			cout <<"查找到该职工!"<<endl;
			this->m_array[j]->show();
			n=1;
			break;
		}
	}

 	if(n!=1)
 	{
 		cout<<"未查找到该职工!"<<endl;
	 }
	 else
 	{
 		cout <<"1,职工编号"<<endl <<"2.职工姓名" <<endl <<"3.职工部门"<<endl; 
 		cout <<"请输入要修改的项目:(可多选,用空格隔开)";
 		while(ch==' '&&i<3)
 		{
 			cin >>a[i];
 			ch = getchar();
 			i++;
		 }
		 i=0;
		while(i<3)
		{
			switch(a[i])
			{	
				case 1:
					cout <<"请输入修改后的职工编号:";
					cin >> num;
					this->m_array[j]->m_num=num;
					break;
				case 2:
					cout <<"请输入修改后的职工名称:";
					cin >> name;
					this->m_array[j]->m_name=name;
					break;
				case 3:
					cout <<"请输入修改后的职工部门:"<<endl;
					cout <<"1.老板"<<endl <<"2.经理"<<endl<<"3.普通员工"<<endl;
					cin >> id;
					if(id == 1)
					{
						wk=new boss(this->m_array[j]->m_num,this->m_array[j]->m_name,id); 
					 }else if(id == 2)
					 {
					 	wk=new manage(this->m_array[j]->m_num,this->m_array[j]->m_name,id); 
					 }
					 else if(id == 3)
					 {
					 	wk=new employee(this->m_array[j]->m_num,this->m_array[j]->m_name,id); 
					 }
					 else
					 {
					 	cout << "输入错误"<<endl;
						return; 
					 }
					this->m_array[j]=wk;
 					break;
				case 0:break;
				default:cout <<"输入错误!" <<endl;return; 
				
			}
			i++;
		}
	 } 
	 this->save_xt();
	 cout <<"修改成功" <<endl; 
 }
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值