这是一个简单的通讯录 四种最简单的功能增减删改 ,我作为一个初学者,我想说大家一起加油一起进步!
这是一个简单的通讯录 四种最简单的功能增减删改 ,我作为一个初学者,我想说大家一起加油一起进步!
#include"stdafx.h"
#include<iostream>
#include<string>
using namespace std;
class AddressBook
{
public:
string name;
int num;
AddressBook *next;
};
AddressBook*head=NULL;
AddressBook*creat()
{
AddressBook*p1,*p2;
p1=new AddressBook;
head=p1;
p2=p1;
cout<<"请出入添加人姓名,按0返回: ";
string str;
cin>>str;
p1->name=str;
if(p1->name!="0")
{
cout<<"请输入联系人电话号码: ";
cin>>p1->num;
}
else
{
delete p1;p2=NULL;head=NULL;
return head;
}
while(p1->name!="0")
{
p2=p1;
p1=new AddressBook;
cout<<"请出入添加人姓名,按0返回:";
cin>>str;
p1->name=str;
if(p1->name!="0")//可以用""表示空白
{
cout<<"请输入联系人电话号码:";
cin>>p1->num;
}
p2->next=p1;//p2的next指针指向p1,即p2的下一个节点是p1
}
delete p1;
p2->next=NULL;
return head;
}
void information(AddressBook*head)
{
cout<<"通讯信息如下:"<<endl;
while(head)
{
cout<<"联系人: "<<head->name<<"\t"<<"电话号码: "<<head->num<<endl;
head=head->next;
}
}
void insert(AddressBook*head,string name,int num)
{
AddressBook*human=new AddressBook;//创建一个新节点
AddressBook*i;
while(head)
{
i=head;
head=head->next;
}
human->name=name;
human->num=num;
i->next=human;
human->next=NULL;
}
void Delete(AddressBook*head,string na)
{
AddressBook*i;
if(head->name==na)
{
i=head;
head=head->next;
::head=head;
delete i;
cout<<"联系人删除成功!";
return;
}
while(head)
{
if(head->next->name==na)
{
i=head->next;
head->next=i->next;
delete i;
cout<<"联系人删除成功!";
return;
}
if(head->next==NULL)
{
cout<<"没有找到需要删除的对象"<<endl;
return;
}
}
}
int main()
{
int choose;
bool quit=true;
string Name;
string na;
int Num;
while(1)
{
cout<<"------------------------------《欢迎来到渐渐通讯录》----------------------------"<<endl;
cout<<"<1>编辑通讯录\n<2>增添联系人\n<3>删除联系人\n<4>显示通讯录\n<5>退出"<<endl;
cout<<"请输入你的选择:"<<endl;
cin>>choose;
switch(choose)
{
case 1:
system("cls");
head=creat();
system("cls");
break;
case 2:
system("cls");
cout<<"请输入需要添加联系人的姓名: ";
cin>>Name;
cout<<"请输入需要添加联系人的电话号码: ";
cin>>Num;
insert(head,Name,Num);
system("cls");
break;
case 3:
system("cls");
cout<<"请输入需要删除的联系人名字:";
cin>>na;
Delete(head,na);
cin.get();
cin.get();
system("cls");
break;
case 4:
system("cls");
if(head==NULL)
{
cout<<"对不起,现在您的通讯录为空,按回车键返回进行添加联系人......";
cin.get();
cin.get();
system("cls");
break;
}
information(head);
cin.get();
cin.get();
system("cls");
break;
case 5:
system("cls");
quit=false;
cout<<"您已退出程序,感谢您的使用.....";
cin.get();
cin.get();
system("cls");
break;
default:
cout<<"请重新选择!"<<endl;
break;
if(quit==false)
break;
}
}
system("pause");
return 0;
}