通讯录管理系统
#include<iostream.h>#include<string.h>
typedef struct lnode
{
int num;
char name[10];
char sex[2];
char pho[13];
char add[23];
lnode *next;
}lnode,*Llist;
Llist creat(Llist L)
{
lnode *s,*r;
int a;
r=new lnode;
L=new lnode;
L->next=NULL;
L=r;
while(a!=0)
{
s=new lnode;
cout<<"请输入编号/姓名/性别/电话号码/地址:"<<endl;
cin>>s->num>>s->name>>s->sex>>s->pho>>s->add;
r->next=s;
r=s;
cout<<"结束输入按0,其他键继续"<<endl;
cin>>a;
}
r->next=NULL;
return L;
}
Llist Insert(Llist L)
{
lnode *p=L;
lnode *s;
cout<<"请输入要插入的编号/姓名/性别/电话号码/地址:"<<endl;
s=new lnode;
cin>>s->num>>s->name>>s->sex>>s->pho>>s->add;
while(p->next)
p=p->next;
p->next=s;
s->next=NULL;
return L;
}
void output(Llist L)
{
lnode *s;
s=L;
cout<<"输出通讯录内容:"<<endl;
if(s->next==NULL)
cout<<"该通讯录没有任何内容!"<<endl;
else
{
cout<<"编号/姓名/性别/电话号码/地址"<<endl;
while(s->next)
{
s=s->next;
cout<<s->num<<" "<<s->name<<" "<<s->sex<<" "<<s->pho<<" "<<s->add<<endl;
}
}
}
void search_num(Llist L,int i)
{
lnode *s;
s=L->next;
while(s&&i!=s->num)
{
s=s->next;
}
if(s!=NULL)
{
cout<<s->num<<" "<<s->name<<" "<<s->sex<<" "<<s->pho<<" "<<s->add<<endl;
}
else
cout<<"查无记录"<<endl;
}
void search_name(Llist L,char n[10])
{
lnode *s;
s=L->next;
if(s!=NULL){while(s&&strcmp(s->name,n))
s=s->next;
if(s!=NULL)
cout<<s->num<<" "<<s->name<<" "<<s->sex<<" "<<s->pho<<" "<<s->add<<endl;
else
cout<<"查无记录!"<<endl;}
}
void search(Llist L)
{
int j,i;
char n[10];
cout<<"编号查找1 "<<"姓名查找2"<<endl;
cin>>j;
switch(j)
{
case 1:
cout<<"输入要查询的记录编码"<<endl;
cin>>i;
search_num(L,i);
break;
case 2:
cout<<"输入要查询的姓名"<<endl;
cin>>n;
search_name(L,n);
break;
default:
cout<<"输入错误"<<endl;
}
}
void del_num(Llist L,int i)
{
lnode *p,*q;
if(i<1)
{
cout<<"输入位置错误!"<<endl;
return;
}
q=L;
p=L->next;
while(p->next&&i!=p->num)
{
q=p;
p=p->next;
}
if(p!=NULL)
{
q->next=p->next;
delete p;
cout<<"删除成功!"<<endl;
}
else
cout<<"不存在该记录!"<<endl;
}
void del_name(Llist L,char n[10])
{
lnode *p,*q;
q=L;
p=L->next;
while(p&&strcmp(p->name,n))
{
q=p;
p=p->next;
}
if(p!=NULL)
{
q->next=p->next;
delete p;
cout<<"删除成功!"<<endl;
return;
}
else
cout<<"不存在该记录!"<<endl;
}
void del(Llist L)
{
int j,i;
char n[10];
cout<<"编号删除1 "<<"姓名删除2"<<endl;
cin>>j;
switch(j)
{
case 1:
cout<<"输入要删除的记录编码"<<endl;
cin>>i;
del_num(L,i);
break;
case 2:
cout<<"输入要删除的姓名"<<endl;
cin>>n;
del_name(L,n);
break;
default:
cout<<"输入错误"<<endl;
}
}
void main()
{
Llist L;
L=new lnode;
int c;
while(c!=0)
{
cout<<"请输入您要进行的操作序号:"<<endl;
cout<<"!!!!!!!!请确定你已经建立了通讯录!"<<endl;
cout<<"1、建立通讯录"<<endl;
cout<<"2、插入通讯录"<<endl;
cout<<"3、查询通讯录"<<endl;
cout<<"4、通讯录删除"<<endl;
cout<<"5、输出通讯录"<<endl;
cout<<"6、退出系统"<<endl;
cout<<endl;
int q;
cin>>q;
switch(q)
{
case 1:
L=creat(L);break;
case 2:
Insert(L);break;
case 3:
search(L);break;
case 4:
del(L);break;
case 5:
output(L);break;
case 6:
cout<<"你是否真的要退出程序?"<<endl;break;
default:
break;
}
cout<<"!!! 退出程序请选择0 其他键继续!!!"<<endl;
cin>>c;
}
}