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

📄 project.cpp

📁 对员工信息进行管理
💻 CPP
字号:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
class Employee  //Define the class of Employee include its datas and functions
{
private:
	int emp_id;
	char emp_name[20];
	int age;
	float salary;
	char department[20];
	char position[20];
public:
	void AcceptData()
	{
		cout<<"enter a Employee\'s information"<<endl;
		
		cout<<"please enter the emp_id:\n";
		cin>>emp_id;
		cin.ignore();
		
		cout<<"please enter the emp_name\n";
		cin.getline(emp_name,20);
		
		cout<<"please enter age\n";
		cin>>age;
		
		cout<<"please enter the salary\n";
		cin>>salary;
		cin.ignore();
		
		cout<<"please enter the department\n";
		cin.getline(position,20);
		
		cout<<"please enter the position\n";
		cin.getline(department,20);
	}
    void Display()
	{
		cout<<"the id is:";
		cout<<emp_id<<"\t";
		cout<<"name:";
		cout<<emp_name<<"\t";
		cout<<"age:";
		cout<<age<<"\t";
		cout<<"salary:";
		cout<<salary<<"\t";
		cout<<"department:";
		cout<<department<<"\t";
		cout<<"position:";
		cout<<position<<endl;
	}
	char *getname()
	{
		return emp_name;
	}
	int getid()
	{
		return emp_id;
	}
};

struct Node  //create a node struct
{
	Employee employee;
	Node *next;
	Node(Employee e,Node *n=NULL):employee (e),next(n)
	{
	}
};

class List
{
private:
	Node *start,    //pointer to start of list
		*current,   //used by querynode() and delnode()
		*precede;   ////used by querynode() and delnode()
public:
	List();
	~List();
	
	void insertNode(Employee &e);//add a node
	bool deleteNode(char *name,int id);//delete a node
	bool searchNode(char *name,int id);//search for a given employee
	bool queryNode(char *name,int id);
	void listInformation();//display all the information
	void destory();
    bool update(char *name,int id);
	void commit();
};

List::List()
{
	start=current=precede=NULL;
}

void List::destory()
{
	//delete all the information
	while(start!=NULL)
	{
		current=start;
		start=start->next;
		delete current;
	}
	start=precede=current=NULL;
}
List ::~List()
{
	destory();
}
void List::insertNode(Employee &e)
{
	//constructor for adding the first node
	if(start==NULL||(e.getid()<start->employee.getid()))
	{
		start=new Node(e,start);
		return;
	}
	
	Node *prev,*curr;
	for(prev=curr=start;curr!=NULL&&e.getid()>curr->employee.getid();prev=curr,curr=curr->next)
	{
	}
	Node *n=new Node(e,curr);
	prev->next=n;
}

//search a employee
bool List::searchNode(char* name,int id)
{
	for(precede=current=start;current&&(strcmp(name,current->employee.getname())!=0)&&(id!=current->employee.getid());precede=current,current=current->next)
	{
	}
	if(current==NULL)
	{
		return(current!=NULL);
	}
	else
	{
		current->employee.Display();
		return(current!=NULL);
	}
}

//list all the information
void List::listInformation()
{
	for(Node *temp=start;temp!=NULL;temp=temp->next)
	{
		temp->employee.Display();
	}
}

//delete a employee
bool List::deleteNode(char* name,int id)
{
	if(queryNode(name,id)==false)
	{
		return false;
	}
	precede->next=current->next;
	if(current==start)
	{
		start=start->next;
	}
	delete current;
	return true;
}

//update a employee
bool List::update(char *name,int id)
{
	if(queryNode(name,id)==false)
	{
		return false;
	}
	current->employee.AcceptData();
	return true;
}

//save ton the file
void List::commit()
{
	ofstream fout("c:\\data.txt",ios::app);
	if(!fout)
	{
		cout<<"can not open file\n";
	}
	current=start;
	while(current!=NULL)
	{
       
		fout.write((char *)&current->employee,sizeof(Employee));
		current=current->next;
	}
}

//fand employee
bool List::queryNode(char *name,int id)
{
	for(precede=current=start;current&&(strcmp(name,current->employee.getname())!=0)&&(id!=current->employee.getid());precede=current,current=current->next)
	{
	}
	return(current!=NULL);
}


int main()
{
	List obj;
	int ch;
	while(1)
	{
		cout<<"1.create a file to store infomation\n";
		cout<<"2.use the file\n";
		cout<<"3.copy the file\n";
		cout<<"4.insert a eployee\'s infofmation\n";
		cout<<"5.search a employee\n";
		cout<<"6.update a employee\n";
		cout<<"7.delete a employee\'s information\n";
		cout<<"8.sort\n";
		cout<<"9.list all the information\n";
		cout<<"10.commit\n";
		cout<<"11.rollback\n";
		cout<<"12.exit\n";
		cin>>ch;
		switch(ch)
		{
		case 1:
			{
				ofstream out("c:\\data.txt");
				if(!out)
				{
					cout<<"can not open file\n";
				}
				out.close();
			}
			break;
		case 2:
			{
				ifstream in("c:\\data.txt");
				if(!in)
				{
					cout<<"can not open file\n";
				}
				Employee emp;
				if(!in.eof())
				{
					in.read((char *)&emp,sizeof(Employee));
					obj.insertNode(emp);
				}
				in.close();
			}
			break;
		case 3:
			{
				ofstream fout("c:\\datacopy.txt");
				if(!fout)
				{
					cout<<"can not open file\n";
				}
				ifstream fin("c:\\data.txt");
				if(!fin)
				{
					cout<<"can not open file\n";
				}
				Employee copy;
				if(!fin.eof())
				{
					fin.read((char *)&copy,sizeof(Employee));
					fout.write((char *)&copy,sizeof(Employee));
				}
				fin.close();
				fout.close();
			}
			break;
			
		case 4:
			{
				Employee mainemp;
				mainemp.AcceptData();
				obj.insertNode(mainemp);
			}
			break;
		case 5:
			{
				char mainname[20];
				int mainid;
				cout<<"enter a name or id ";
				cin>>mainname;
				cin>>mainid;
				if(obj.searchNode(mainname,mainid)==false)
				{
					cout<<"not found!\n";
				}
				else
				{
					cout<<"have found\n";
				}
				break;
			}
		case 6:
			{
				cout<<"enter the employee you want to update:name and id\n";
				char mainname2[20];
				int mainid2;
				cin>>mainname2;
				cin>>mainid2;
				if(obj.update(mainname2,mainid2)==true)
				{
					cout<<"success update\n";
				}
				else
				{
					cout<<"failed\n";
				}
			}
			break;
		case 7:
			{
				cout<<"enter the employee you want to delete:name and id\n";
				char mainname3[20];
				int mainid3;
				cin>>mainname3;
				cin>>mainid3;
				obj.deleteNode(mainname3,mainid3);
			}
			break;
		case 8:
			{
				cout<<"have already been sorted by default\n";
			}
			break;
		case 9:
			{
				obj.listInformation();
			}
			break;
		case 10:
			{
				obj.commit();
			}
			break;
		case 11:
			{
				exit(0);
			}
			break;
		case 12:
			{
				exit(0);
			}
			break;
		default:
			cout<<"enter error\n";
			break;
		}
	}
			return 0;
}
			
			
			
			
			
			
			
			
			
			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -