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

📄 search.cpp

📁 Implement a phone book system for employees of a company. Your program will output the following me
💻 CPP
字号:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

class Employee{
public:
	string name;
	string phone;
private:
};

void Menu()
{
	cout<<"please choose 1-8 that you want to do"<<endl;
	cout<<"(1) Enter an employee and a phone pair to the system"<<"\n"
        <<"(2) Lookup an employee's phone number"<<"\n"
        <<"(3) Find out who is/are the person(s) of a given number"<<"\n"
        <<"(4) How many people are currently in the system"<<"\n"
        <<"(5) Delete an employee from the system"<<"\n"
        <<"(6) Output all employees' name‐phone pair"<<"\n"
        <<"(7) How many phone numbers total in the current system"<<"\n"
        <<"(8) Quit"<<"\n"
		<<endl;
}

void InputData(vector<Employee> &em)
{
	Employee employ;
	cout<<"please enter name and then enter a phone number"<<endl;
	cin.clear();
	cin.ignore();
	getline(cin,employ.name);
	cin>>employ.phone;
	em.push_back(employ);
	cout<<"\n"<<endl;
    Menu();
}

void OutputData(vector<Employee> &em)
{   
	for(vector<Employee>::iterator it=em.begin();it!=em.end();++it){
		cout<<it->name<<" "<<it->phone<<'\n'<<endl;
	}
	Menu();
}


	
void FindNum(vector<Employee> &em)
{
	Employee employ;	
	char ch=' ';
	do{
		cout<<"please enter an Employee's Name"<<endl;
		cin.clear();
		cin.ignore();
		getline(cin,employ.name);
		for(vector<Employee>::iterator it=em.begin();it!=em.end();++it){
			if((it->name)==employ.name){
			    cout<<"The Phone Number for "<<employ.name<<" is "<<it->phone<<"\n"<<endl;
				ch='n';
				break;
			}
			if(it==em.end()-1){
				cout<<"The Phone Number for "<<employ.name<<" is not present"<<"\n"<<endl;
				cout<<"would you like to continue to search or go back to main-menu?(y-yes, n-no)"<<endl;
				cin>>ch;
			}
		}	    
	}while(ch!='n'&&ch!='N');
	Menu();
}

void FindName(vector<Employee> &em)
{
	Employee employ;	
	char ch=' ';
	do{ int i=0;
		cout<<"please enter an Employee's Phone"<<endl;
		cin>>employ.phone;
		for(vector<Employee>::iterator it=em.begin();it!=em.end();++it){
			if((it->phone)==employ.phone){
			    cout<<"The Phone Number for "<<employ.phone<<" is "<<it->name<<"\n"<<endl;
				i++;
                }
		}
		ch='n';
		if(i==0){
			cout<<"The Phone Number for "<<employ.phone<<" is not present"<<"\n"<<endl;
		    cout<<"would you like to continue to search or go back to main-menu?(y-yes, n-no)"<<endl;
		    cin>>ch;
		}
	}while(ch!='n'&&ch!='N');
	Menu();;
}

void NumberEmployee(vector<Employee> &em)
{
	vector<string>::size_type num=em.size();
	cout<<"the total number of employee in company is "<<num<<"\n"<<endl;
	Menu();
}

void DeleteEmployee(vector<Employee> &em)
{
	Employee employ;
	cout<<"Which employee would you like to delete from the system"<<endl;
	cin.clear();
	cin.ignore();
	getline(cin,employ.name);
	for(vector<Employee>::iterator it=em.begin();it!=em.end();++it){
		if((it->name)==employ.name){
				em.erase(it);
				cout<<employ.name<<" have already been deleted "<<endl;
				break;
		}	
		if(it==em.end()-1)
			cout<<"The Empolyee for name "<<employ.name<<" is not present"<<"\n"<<endl;
	}
	Menu();
}

void NumberPhone(vector<Employee> &em)
{
	vector<string>::size_type i=0;
	Employee employ;
	vector<Employee>::iterator it1,it2;
	for(it1=em.begin();it1!=em.end()-1;++it1)
		for(it2=it1+1;it2!=em.end();++it2){
			if(it2->phone==it1->phone){
				i++;
				break;
			}
		}
	vector<string>::size_type num=em.size();
	cout<<"the total number of unique phone in company is "<<(num-i)<<"\n"<<endl;
    Menu();
}

int main()
{   
	char num;
    vector<Employee> Emp;
	Menu();
    while(cin>>num){

		if(num=='8'){
			cout<<"Thank you for using Songfei Li's system"<<endl;
			break;
		}
		switch(num){
			case '1':
				InputData(Emp);
			    break;
		    case '2':
				FindNum(Emp);
				break;
		    case '3':
				FindName(Emp);
				break;
		    case '4':
				NumberEmployee(Emp);
				break;
		    case '5':
				DeleteEmployee(Emp);
				break;
		    case '6':
				OutputData(Emp);
			    break;
		    case '7':
				NumberPhone(Emp);
				break;
			default:
				cout<<"error,input number as above"<<"\n"<<endl;
				Menu();
				break;
		}
	}
	return 0;
}

⌨️ 快捷键说明

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