简单通信录的C++实现
这是一个简单的通讯录系统,数据结构内包括学号,姓名,手机号,具备增加、删除、修改、查询的功能。
//2015/3/12 by LDSD
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
char num[15];
char name[7];
char phone[12];
node *next;
};
void serch(node *head)
{
head=head->next;
char con;
char data[15];
while(1)
{
cout<<"1:按学号查询 2:按姓名查询,请选择指令执行操作。
\n";
cin>>con;
if(con=='1')
{
cout<<"请输入学号。
"<<endl;
cin>>data;
while(head!=NULL)
{
if(strcmp(head->num,data)==0)
{
cout<<head->num<<'\t'<<head-
>name<<'\t'<<head->phone<<'\n';break;
}
else
head=head->next;
}
if(head==NULL)
cout<<"未查询到匹配的记录!"<<endl;
break;
}
else if(con=='2')
{
cout<<"请输入姓名。
"<<endl;
cin>>data;
while(head!=NULL)
{
if(strcmp(head->name,data)==0)
{
cout<<head->num<<'\t'<<head-
>name<<'\t'<<head->phone<<'\n';break;
}
else
head=head->next;
}
if(head==NULL)
cout<<"未查询到匹配的记录!"<<endl;
break;
}
else
cout<<"你输入的指令不正确!"<<endl;
}
}
void add(node *head)
{
node *new_stu=new node;
cout<<"依次输入学号,姓名,电话号码。
\n";
cin>>new_stu->num>>new_stu->name>>new_stu->phone;
new_stu->next=head->next;
head->next=new_stu;
}
void modify(node *head)
{
char num[15];
char name[7];
char phone[12];
node *head1=head->next;
int i=1;
node *new_stu=new node;
cout<<"通讯录内容如下,请依次输入编号,学号,姓名,电话号码,以便对信息更新。
\n";
while(head1!=NULL)
{
cout<<i++<<'\t'<<head1->num<<'\t'<<head1-
>name<<'\t'<<head1->phone<<'\n';
head1=head1->next;
}
cin>>i>>num>>name>>phone;
while(i--&&head!=NULL)
head=head->next;
if(head==NULL)
cout<<"你的编号不正确!"<<endl;
else
{
strcpy(head->num,num);
strcpy(head->name,name);
strcpy(head->phone,phone);
}
}
void del(node *head)
{
node *head1=head->next;
int i=1;
cout<<"通讯录内容如下,请输入要删除的编号。
\n";
while(head1!=NULL)
{
cout<<i++<<'\t'<<head1->num<<'\t'<<head1-
>name<<'\t'<<head1->phone<<'\n';
head1=head1->next;
}
cin>>i;
i--;
while(i--&&head->next!=NULL)
head=head->next;
if(head->next==NULL)
cout<<"你的编号不正确!"<<endl;
else
{
head->next=head->next->next;
}
}
void main()
{
char con;
node *head=new node;
head->next=NULL;
while(1)
{
cout<<"1:查询 2:添加 3:修改 4:删除,请选择指令执行操作。
\n";
cin>>con;
if(con=='1')
serch(head);
else if(con=='2')
add(head);
else if(con=='3')
modify(head);
else if(con=='4')
del(head);
else
cout<<"你输入的指令不正确!"<<endl; }
}。