欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

C++实现链表版本通讯录

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

本文实例为大家分享了C++实现链表版本通讯录的具体代码,供大家参考,具体内容如下

#include <iostream>
#include <string>
using namespace std;
class Address;
 
class Contact{
private:
 string name;
 string sex;
 string tel;
 string QQ;
 string address;
 string addition;
 Contact *next;
public:
 Contact();
 friend class Address; 
 
};
Contact::Contact()
{
 next = NULL;
}
class Address{
public:
 Address();
 ~Address();
 int show();
 void insert();
 void delete_per();
 void display();
 void search();
 void update(); 
private:
 Contact *head;
};
Address::Address()
{
 head = new Contact;
 if(head == NULL)
 {
 cout<<"fail create"<<endl;
 }
}
Address::~Address()
{
 delete head;
}
int Address::show()  //主菜单函数
{
 int choice = 0 ;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 通讯录c++简易版本 *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 1、添加 2、删除  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 3、查看 4、搜索  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t* 5、更新 6、退出  *"<<endl;
 cout<<"\t\t\t\t**************************"<<endl;
 cout<<"\t\t\t\t请输入选择:";
 cin>>choice;
 while(!(choice >= 1&&choice <= 6))
 {
 while(getchar()!='\n');
 cout<<"输入有误,请重新输入!";
 cin>>choice;
 }
 return choice;
 
}
void Address::insert() //添加联系人
{
 Contact *p = head;
 char relay = 0;
 while(p->next != NULL)
 {
 p = p->next;
 }
 Contact *person = new Contact;
 cout<<"请输入姓名:";
 cin>>person->name;
 cout<<"请输入性别:";
 cin>>person->sex;
 cout<<"请输入电话:";
 cin>>person->tel;
 cout<<"请输入QQ:";
 cin>>person->QQ;
 cout<<"请输入住址:";
 cin>>person->address;
 cout<<"请输入备注:";
 cin>>person->addition;
 p->next = person;
 person->next = NULL;
 cout<<"\n添加成功,是否继续添加?(y/n)";
 cin>>relay;
 while(!(relay == 'y'||relay == 'Y'||relay == 'N'||relay == 'n'))
 {
 cout<<"输入错误,请重新输入(y/n):";
 cin>>relay;
 }
 if(relay == 'y'||relay == 'y')
 {
 system("clear");
 insert();
 }
}
void Address::delete_per() //删除联系人
{
 string m_name;
 Contact *p = head;
 Contact *pre = head;
 int flag = 0;
 cout<<"请输入你要删除的联系人姓名!";
 cin>>m_name;
 while(p->next != NULL)
 {
 pre = p;
 p = p->next;
 if(p->name == m_name)
 {
 pre->next = p->next;
 delete p;
 p = NULL;
 flag = 1;
 break;
 }
 }
 if(flag == 1)
 {
 cout<<"删除成功!"<<endl;
 }
 else
 {
 cout<<"您删除的联系人不存在,删除失败!"<<endl;
 }
}
 
void Address::display() //查看联系人 
{
 Contact *p = head;
 while(p->next != NULL)
 {
 p = p->next;
 cout<<endl<<"======================================="<<endl;
 cout<<"姓名:"<<p->name<<endl;
 cout<<"性别:"<<p->sex<<endl;
 cout<<"电话:"<<p->tel<<endl;
 cout<<"QQ:"<<p->QQ<<endl;
 cout<<"地址:"<<p->address<<endl;
 cout<<"备注:"<<p->addition<<endl; 
 }
 
}
void Address::search() //搜索联系人
{
 string m_name;
 Contact *p = head;
 int flag = 0;
 cout<<"请输入你要搜索的联系人姓名:";
 cin>>m_name;
 while(p->next != NULL)
 {
 p = p->next;
 if(p->name == m_name)
 {
 cout<<endl<<"======================================="<<endl;
 cout<<"姓名:"<<p->name<<endl;
 cout<<"性别:"<<p->sex<<endl;
 cout<<"电话:"<<p->tel<<endl;
 cout<<"QQ:"<<p->QQ<<endl;
 cout<<"地址:"<<p->address<<endl;
 cout<<"备注:"<<p->addition<<endl; 
 flag = 1;
 }
 }
 if(flag == 1)
 {
 cout<<"\n查询成功!"<<endl;
 }
 else
 {
 cout<<"您查询的联系人不存在,删除失败!"<<endl;
 }
}
void Address::update() //修改联系人
{
 Contact *p = head;
 string m_name;
 int flag = 0;
 
 cout<<"请输入你要更新的姓名:";
 cin>>m_name;
 while(p->next != NULL)
 {
 p = p->next;
 if(p->name == m_name)
 {
 cout<<"请更新性别:";
 cin>>p->sex;
 cout<<"请更新电话:";
 cin>>p->tel;
 cout<<"请更新QQ:";
 cin>>p->QQ;
 cout<<"请更新住址:";
 cin>>p->address;
 cout<<"请更新备注:";
 cin>>p->addition;
 flag = 1;
 break;
 }
 }
 if(flag == 1)
 {
 cout<<"\n更新成功"<<endl;
 }
 else
 {
 cout<<"查无此人,更新失败!"<<endl;
 }
}
 
int main()
{
 Address *person = new Address;
 int choice = 0;
 while(1)
 {
 system("clear");
 choice = person->show();
 switch(choice)
 {
 case 1:
 {
 system("clear");
 person->insert();
 break;
 }
 case 2:
 {
 system("clear");
 person->delete_per();
 break;
 }
 case 3:
 {
 system("clear");
 person->display();
 break;
 }
 case 4:
 {
 system("clear");
 person->search();
 break;
 }
 case 5:
 {
 system("clear");
 person->update();
 break;
 }
 case 6:
 {
 exit(0);
 }
 }
 cout<<"\n\n按任意键返回.....";
 getchar();
 getchar();
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:Qt自定义控件实现圆盘进度条

栏    目:C语言

下一篇:C++ opencv实现的把蓝底照片转化为白底照片功能完

本文标题:C++实现链表版本通讯录

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/61.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有