C++课程设计----电话管理系统


//这是多文件结构


#include <iostream>
#include <string>
#include "类的声明.h" 
using namespace std;





/*******         以下为函数声明区域      ********/

void __menu__();//选项菜单函数 
void __main__();//类似于主函数 

/********        以上为函数声明区域      ********/





//主函数 
int main()
{

	__main__();

	


	return 0;
}




/*------下面代码是函数的定义------*/ 

void __main__()
{
	Company xiejing;//从公司类中实例化一个公司对象 
	
	while("网图小姐")
	{
		int option;
		__menu__();//选项菜单函数 
		cout<<"请输入你的选项:";
		cin>>option;
		if(1 == option)		//1.添加一个部门
		{
			xiejing.__Append__();
		}
		else if(2 == option)//2.修改一个部门 
		{
			xiejing.__Modify__();
		}
		else if(3 == option)//3.删除一个部门
		{
			xiejing.__Delete__();	
		}
		else if(4 == option)//4.显示全部部门
		{
			xiejing.__DisplayAll__();		
		}
		else if(5 == option)//5.添加联系人
		{
			xiejing.__Append__Data__();	
		}
		else if(6 == option)//6.修改联系人
		{
			xiejing.__Modify__Data__();
		}
		else if(7 == option)//7.删除联系人
		{
			xiejing.__Delete__Data__();
		}
		else if(8 == option)//8.搜索联系人
		{
			xiejing.__Search__Data__();
		}
		else if(9 == option)//9.显示全部部门下的联系人
		{
			xiejing.__DisplayAll__Data__();
		}
		else if(10 == option)//10.退出系统
		{
			system("cls");
			cout<<"退出系统"<<endl;
			break;
		}
		else//错误的选项,提示用户输入错误,需要重新输入 
		{
			cout<<"选项错误"<<endl;
			system("cls");
		}
		 
	}
	
	
	
}







void __menu__()//选项菜单函数 
{
	cout<<endl;
	cout<<"                                   \n";
	cout<<"              \\                 /\n";
	cout<<"               \\               /\n";
	cout<<"    ##DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD##\n";
	cout<<"    ## DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD ##\n";
	cout<<"    ## hh       电话号码管理系统       hh ##\n";
	cout<<"    ## hh                              hh ##\n";
	cout<<"    ## hh        1. 添加一个部门       hh ##\n";
	cout<<"    ## hh        2.修改一个部门       hh ##\n";
	cout<<"    ## hh        3.删除一个部门       hh ##\n";
	cout<<"    ## hh        4.显示全部部门       hh ##\n";
	cout<<"    ## hh                              hh ##\n";
	cout<<"    ## hh        5.添加联系人          hh ##\n";
	cout<<"    ## hh        6.修改联系人          hh ##\n";
	cout<<"    ## hh        7.删除联系人          hh ##\n";
	cout<<"    ## hh        8.搜索联系人          hh ##\n";
	cout<<"    ## hh        9.显示全部联系人      hh ##\n";
	cout<<"    ## hh                              hh ##\n";
	cout<<"    ## hh        10.退出管理系统      hh ##\n";
	cout<<"    ## hh                              hh ##\n";
	cout<<"    ## MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM ##\n";
	cout<<"    ##MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM##\n";
	cout<<"      \\/                                \\/\n";
	cout<<endl;
}


/*--------以上的代码是函数的定义--------*/ 
#include <iostream>
#include <string>
#include "类的声明.h"


 

/*----------这是联系人类的重载抽取运算符的友元函数----------*/ 
ostream &operator <<(ostream &out,Data *d)
{
	out	<<"姓名:"		<<d->name
		<<"\t手机号码:"	<<d->phone_number
		<<"\t工作号码:"	<<d->company_number
		<<"\t家庭号码:"	<<d->family_number;
	return out;
}



/*----------这是部门类的重载抽取运算符的友元函数----------*/
ostream & operator <<(ostream &out,Department *depart)
{
	out<<depart->Department_name;
	return out;
}






/*----------以下都是电话本成员函数----------*/ 


//电话本类的构造函数
//主要功能:将电话本的联系人设为空,并且将联系人个数设为 0 
Address_List::Address_List()
{
	this->count = 0;
	this->Head = NULL;

}



//电话本类的析构函数
//主要功能:释放生成联系人信息的结点 
Address_List::~Address_List()
{
	Data *t;
	if(this->Head != NULL)
	{
		while(this->Head)
		{
			t = this->Head;
			this->Head = this->Head->next;
			delete t;
		}
	}
	
}



//电话本类的打印全部联系人函数
//主要功能:按照格式打印联系人信息的函数 
void Address_List::__DisplayAll__(string depart_name)	 
{
	string title = "\t----------"+depart_name
	+"部门"+"联系人号码"+"----------";
	
	
	cout<<title<<endl;
	if(this->Head == NULL)
	{
		cout<<"\t\t      无"<<endl; 
	}
	else
	{
		int i = 1;
		Data *t = this->Head;
		while(t)
		{
			cout<<i++<<":"<<t<<endl;
			t = t->next;
		}
	}
}



//电话本类的增加联系人函数 
//基本功能:增加一个新联系人
void Address_List::__Append__()		//增加联系人,已经写好,测试通过 
{
	string name;
	string phone_number;
	string company_number;
	string family_number;
	
	
	//输入联系人的各种信息 
	cout<<"输入联系人姓名:";
	cin>>name;
	cout<<"输入手机号码:";
	cin>>phone_number;
	cout<<"输入工作号码:";
	cin>>company_number;
	cout<<"输入家庭号码:";
	cin>>family_number;

	
	Data *p = new Data;
	
	p->name = name;
	p->phone_number = phone_number;
	p->company_number = company_number;
	p->family_number = family_number;
	p->next = this->Head;
	this->Head = p;

	this->count++;
}




//电话本类的搜索联系人函数 
//基本功能:搜索联系人
void Address_List::__Search__(string depart_name,string information)
{
	int i = 1;
	Data *p = this->Head;
	string title;
	int index1,index2,index3,index4,flag = 0;
	
	title = "----------"+depart_name+"部门搜索结果----------";

	
	cout<<title<<endl;
	
	while(p)
	{
		index1 = p->name.find(information);
		index2 = p->phone_number.find(information);
		index3 = p->company_number.find(information);
		index4 = p->family_number.find(information);
		
		
		if(index1 != string::npos || index2 != string::npos ||
			index3 != string::npos || index4 != string::npos)//代表找到了 
		{
			cout<<i++<<":"<<p<<endl;
			flag = 1;
		}
		p = p->next;
	}
	if(flag != 1)
	{
		cout<<"\t      无"<<endl;
	}

}




/*----------以下是公司类的成员函数----------*/ 



//这是公司类的构造函数 (已经写好,测试通过)
Company::Company() 
{
	this->count = 0;
	this->Head = NULL;
}



//这是公司类的析构函数 (已经写好,测试通过) 
Company::~Company()
{
	Department *t;
	if(this->Head != NULL)
	{
		while(this->Head)
		{
			t = this->Head;
			this->Head = this->Head->next;
			delete t;
		}
	}
}




//这是公司类的增加一个部门的成员函数
void Company::__Append__()
{
	string depart_name;
	Department *p,*q;
	
	cout<<"请输入部门的名字:";
	cin>>depart_name;
	
	
	p = new Department;
	p->Department_name = depart_name;
	p->next = this->Head;
	this->Head = p;
	this->count++;
	cout<<"添加成功"<<endl;
	 
}

 
 

//这是公司类的删除一个部门的成员函数
void Company::__Delete__()
{
	int option;
	
	
	if(0 != this->count)
	{
		__DisplayAll__();
		cout<<"请输入要删除部门的序号:";
		cin>>option;
		if(1 <= option && option <= this->count)
		{
			int i = 1;
			Department *p,*q;
			p = this->Head;
			while(i != option)
			{
				p = p->next;	//现在p指向要删除的结点 
				i++;
			}
			
			
			if(p != this->Head)
			{
				q = this->Head;
				while(q->next != p)
				{
					q = q->next;
				}
				q->next = p->next;
			}
			else
			{
				this->Head = p->next;
			}
			
			delete p;
			this->count--;
			cout<<"删除成功"<<endl; 
		}
		else
		{
			cout<<"选项错误,删除失败"<<endl;
		}
	}
	else
	{
		cout<<"没有任何部门信息,删除失败"<<endl;
	}

}





//这是公司类的修改一个部门的成员函数
void Company::__Modify__()
{			
	int option,i = 1;
	string new_depart_name;
	Department *p;
	
	
	if(0 != this->count)//这个if分支测试通过了 
	{
		__DisplayAll__();
		
		cout<<"请输入要修改部门的序号:";
		cin>>option;
		
		if(1 <= option && option <= this->count)
		{
			cout<<"请输入新的部门名:";
			cin>>new_depart_name;

			p = this->Head;
			while(i != option)
			{
				p = p->next;	//现在p指向要修改的结点 
				i++;
			}
			p->Department_name = new_depart_name;
			cout<<"修改成功"<<endl;
		}
		else//这个else分支通过测试 
		{
			cout<<"选项错误,修改失败"<<endl;
		}
	}
	else
	{
		cout<<"没有任何部门信息,修改失败"<<endl;
	}
}




//这是公司类的显示全部部门的成员函数
void Company::__DisplayAll__()
{

	cout<<"----------所有部门----------"<<endl;
	if(this->Head == NULL)
	{
		cout<<"\t     无"<<endl;
	}
	else
	{
		int i = 1;
		Department *t = this->Head;
		while(t)
		{
			cout<<i++<<":"<<t<<endl;
			t = t->next;
		}
	}
}



//这是公司类的增加联系人的成员函数
void Company::__Append__Data__()
{
	if(0 != this->count)
	{
		int option;
		
		__DisplayAll__();
		cout<<"请选择一个部门:";
		cin>>option;
		
		
		
		if(1 <= option && option <= this->count)
		{
			int i = 1;
			Department *p = this->Head;
			
			while(i != option)
			{
				p = p->next;	//现在p指向要修改的结点 
				i++;
			}
			
			//调用电话本中的增加联系人函数
			p->Department_List.__Append__();
			cout<<"添加成功"<<endl; 
		}
		else//这个else分支通过测试 
		{
			cout<<"部门选择错误,增加失败"<<endl;
		}
	}
	else
	{
		cout<<"没有任何部门,不能增加任何联系人"<<endl;
	}
}


//这是公司类的删除联系人的成员函数
void Company::__Delete__Data__()
{
	int flag = 0,j = 1,index1,index2,index3,index4,option;
	Data *temp,*front_temp;
	Department *p;
	string information;
	
	
	if(this->count != 0)
	{
		__DisplayAll__();
		cout<<"请选择一个部门:";
		cin>>option;
		
		if(1 <= option && option <= this->count)
		{

			int i = 1;
			p = this->Head;
			
			while(i != option)
			{
				p = p->next;	//现在p指向要修改的部门结点 
				i++;
			}
			cout<<"请输入要查找的联系人信息:";
			cin>>information;
			

			temp = p->Department_List.Head;
			
			if(temp != NULL)
			{
				while(temp)
				{
					
					index1 = temp->name.find(information);
					index2 = temp->phone_number.find(information);
					index3 = temp->company_number.find(information);
					index4 = temp->family_number.find(information);
	
					if(index1 != string::npos || index2 != string::npos ||
						index3 != string::npos || index4 != string::npos)//代表找到了 
					{
						cout<<j++<<":"<<temp<<endl;
						flag = 1;
					}
					temp = temp->next;
				}
				
				//此处的 goto 简直是画龙点睛啊,nice啊,马玲政
				//你还有的学 
				goto xiejing;
				
			}
			else
			{
				cout<<"该部门的电话本为空,删除失败"<<endl; 
			} 
		}
		else
		{
			cout<<"部门选择错误,删除联系人失败"<<endl;
		}
	}
	else
	{
		cout<<"没有任何部门,不能删除任何联系人"<<endl;
	}


	return ;


	xiejing:
		if(0 == flag)
		{
			cout<<"没有查找到该联系人的有关信息"<<endl;
		}
		else
		{
			cout<<"请输入你要删除的联系人的序号:";
			cin>>option;
			if(1 <= option && option <= j-1)
			{
			
				flag = 0;
				temp = p->Department_List.Head;
				while(temp)
				{
					
					index1 = temp->name.find(information);
					index2 = temp->phone_number.find(information);
					index3 = temp->company_number.find(information);
					index4 = temp->family_number.find(information);
	
					if(index1 != string::npos || index2 != string::npos ||
						index3 != string::npos || index4 != string::npos)//代表找到了 
					{
						flag++;
						if(flag == option)
						{
							break;
						}
					}
					temp = temp->next;
				}
				
				
				
			
				if(temp != p->Department_List.Head)
				{
					front_temp = p->Department_List.Head;
					while(front_temp->next != temp)
					{
						front_temp = front_temp->next;
					}
					front_temp->next = temp->next;
				}
				
				else
				{
					p->Department_List.Head = temp->next;
				}
				delete temp;
				p->Department_List.count--; 
	
				cout<<"删除成功"<<endl; 
			}
			else
			{
				cout<<"选项错误,删除失败"<<endl;
			}
		}
}


//这是公司类的修改联系人的成员函数
void Company::__Modify__Data__()
{	
	int flag = 0,j = 1,index1,index2,index3,index4,option;
	Data *temp;
	Department *p;
	string information;
	
	
	if(this->count != 0)
	{
		__DisplayAll__();
		cout<<"请选择一个部门:";
		cin>>option;
		
		if(1 <= option && option <= this->count)
		{
		
			int i = 1;
			p = this->Head;
			
			while(i != option)
			{
				p = p->next;	//现在p指向要修改的部门结点 
				i++;
			}
			cout<<"请输入要查找的联系人信息:";
			cin>>information;
			

			temp = p->Department_List.Head;
			
			if(temp != NULL)
			{
				while(temp)
				{
					
					index1 = temp->name.find(information);
					index2 = temp->phone_number.find(information);
					index3 = temp->company_number.find(information);
					index4 = temp->family_number.find(information);
	
					if(index1 != string::npos || index2 != string::npos ||
						index3 != string::npos || index4 != string::npos)//代表找到了 
					{
						cout<<j++<<":"<<temp<<endl;
						flag = 1;
					}
					temp = temp->next;
				}
				
				//你还有的学,马玲政 
				goto xiejing;
		
			}
			else
			{
				cout<<"该部门的电话本为空,修改失败"<<endl; 
			} 
		}
		else
		{
			cout<<"部门选择错误,修改联系人失败"<<endl;
		}
	}
	else
	{
		cout<<"没有任何部门,不能修改任何联系人"<<endl;
	}

	return;

	xiejing:
		if(0 == flag)
		{
			cout<<"没有查找到该联系人的有关信息"<<endl;
		}
		else
		{
			cout<<"请输入你要修改的联系人的序号:";
			cin>>option;
			if(1 <= option && option <= j-1)
			{
				
				flag = 0;
				temp = p->Department_List.Head;
				while(temp)
				{
					
					index1 = temp->name.find(information);
					index2 = temp->phone_number.find(information);
					index3 = temp->company_number.find(information);
					index4 = temp->family_number.find(information);
	
					if(index1 != string::npos || index2 != string::npos ||
						index3 != string::npos || index4 != string::npos)//代表找到了 
					{
						flag++;
						if(flag == option)
						{
							break;
						}
					}
					temp = temp->next;
				}
			
				string name;	
				string phone_number;
				string company_number;
				string family_number;	
				
				cout<<"输入联系人新的姓名:";
				cin>>name;
				cout<<"输入新的手机号码:";
				cin>>phone_number;
				cout<<"输入新的工作号码:";
				cin>>company_number;
				cout<<"输入新的家庭号码:";
				cin>>family_number;
			
				
				temp->name = name;
				temp->phone_number = phone_number;
				temp->company_number = company_number;
				temp->family_number = family_number;
				
				cout<<"修改成功"<<endl; 
			}
			else
			{
				cout<<"选项错误,修改失败"<<endl;
			}
		}
}


//这是公司类的搜索联系人的成员函数
void Company::__Search__Data__()
{
	if(this->count != 0)
	{
		string information;
		cout<<"输入你要检索的联系人的信息:";
		cin>>information;
			
		int i = 1;
		Department *t = this->Head;
		while(t)
		{
			t->Department_List.__Search__(t->Department_name,information);
			t = t->next;
		}
		
	}
	else
	{
		cout<<"无任何部门信息,搜索联系人信息失败"<<endl;
	}

}


//这是公司类的显示联系人的成员函数
void Company::__DisplayAll__Data__()
{
	if(this->count != 0)
	{
		int i = 1;
		Department *t = this->Head;
		while(t)
		{
			t->Department_List.__DisplayAll__(t->Department_name);
			t = t->next;
		}
	}
	else
	{
		cout<<"无任何部门信息,显示联系人信息失败"<<endl;
	}
}

#include<string> 
using namespace std;



/*以下是类的从属关系


注:	因准备不足,且有无任何经验,所以全部数据项都
	设置为public,当然是可以设置多个友元类来解决此问题
	(已经设置过了,但怕出问题,故又设置回去了)
	
	 
	公司类
		--部门类
			--电话本类
				--联系人信息类
*/ 






class Data		//每一个联系人的资料,即联系人类 
{
	public:
		string name;			//储存联系人的姓名 
		string phone_number;	//储存联系人的手机号码 
		string company_number;	//储存联系人的公司号码 
		string family_number;	//储存联系人的家庭号码 
		Data *next;				//储存下个联系人的地址 
	public:
		friend ostream &operator <<(ostream &out,Data *d);//重载输出 
};




class Address_List						//电话本类,管理每一个结点 
{
	public:
		int count;						//联系人计数 
		Data *Head;						//所有联系人以链队列的方式存储 
	public:
		Address_List(); 				//构造函数,已经写好,测试通过 
		~Address_List();				//析构函数,已经写好,测试通过 
		void __DisplayAll__(string); 	//显示联系人,已经写好,测试通过 
		void __Append__(); 				//增加联系人,已经写好,测试通过 
		void __Search__(string,string);	//搜索联系人,已经写好,测试通过 

};



//部门类 		注释:这个部门类已经完毕了 
class Department
{
	public:
		string Department_name;			//部门名 
		Address_List Department_List;//这个部门的电话本(一个部门设置一个电话本)
		Department *next;
	public:
		friend ostream & operator <<(ostream &,Department *);
};



//公司类 
class Company
{
	public:
		int count;					//部门计数 
		Department *Head;			//所有部门以链队列的方式存储 
		
	public:
		Company(); 		//构造函数,已经写好,测试通过 
		~Company();		//析构函数,已经写好,测试通过
		
		void __Append__(); 		//增加一个部门,已经写好,测试通过 
		void __Delete__(); 		//删除一个部门,已经写好,测试通过 
		void __Modify__(); 		//修改一个部门,已经写好,测试通过 
		void __DisplayAll__(); 	//显示所有部门,已经写好,测试通过
		
		
		void __Append__Data__();	//增加一个联系人,已经写好,测试通过 
		void __Delete__Data__();	//删除一个联系人,已经写好,测试通过 
		void __Modify__Data__();	//修改一个联系人,已经写好,测试通过 
		void __Search__Data__();	//搜索一个联系人,已经写好,测试通过 
		void __DisplayAll__Data__();//显示全部联系人,已经写好,测试通过 
		
};

发布了17 篇原创文章 · 获赞 1 · 访问量 292

猜你喜欢

转载自blog.csdn.net/xjlovewjh/article/details/103775252