题目要求:
为某个单位建立一个员工通讯录管理系统,可以方便查询每一个员工的手机号、及电子邮箱。其功能包括通讯录链表的建立、员工通讯信息的查询、修改、插入与删除、以及整个通讯录表的输出。
操作分为查询(query)、修改(modify)、添加(add)、删除(del)、打印(print)
#include
using namespace std;
typedef struct
{
int number;
string name;
string phoneNumber;
string e_mail;
}StaffInfo;
typedef struct SNode
{
StaffInfo data;
struct SNode *next;
}SNode, *LinkList;
void InitList(LinkList &L)
{
L = new SNode; //生成新结点作为头结点
L->next = NULL; //头结点指针域置空
}
void add_CinStaffInfo(LinkList &L, StaffInfo staffData)
{
LinkList r, s;
r = L;//尾指针r指向头结点
while(r->next && (r->next->data.number < staffData.number))
{
r = r->next;
}
if(r->next == NULL)
{
s = new SNode;//生成新结点
s->data = staffData;
s->next = NULL;
r->next = s;
}
}
void add(LinkList &L)
{
StaffInfo data;
LinkList p = L->next;
cin>>data.number;
while(p)
{
if(data.number == p->data.number)
{
cout<<"error"<<endl;
}
p = p->next;
}
cin>>data.name;
LinkList q = L->next;
while(q)
{
if(data.name == q->data.name)
{
cout<<"error"<<endl;
}
q = q->next;
}
cin>>data.phoneNumber;
cin>>data.e_mail;
add_CinStaffInfo(L, data);
}
void print(LinkList &L)
{
LinkList p = L->next;
while(p)
{
cout<<p->data.number<<" "<<p->data.name<<" "<<p->data.phoneNumber<<" "<<p->data.e_mail<<endl;
p = p->next;
}
}
void query(LinkList L, string name)//查询
{
LinkList p = L->next;
while(p && p->data.name != name)
{
p = p->next;
}
if(!p)
{
cout<<"NOT FOUND!"<<endl;
}
else
{
cout<<p->data.number<<" "<<p->data.name<<" "<<p->data.phoneNumber<<" "<<p->data.e_mail<<endl;
}
}
void modify(LinkList &L, string name)//修改
{
LinkList p = L;
string str, sstr;
while(p->next)
{
if(name == p->next->data.name)
{
cin>>str;
if(str == "name")
{
cin>>sstr;
LinkList q = L;
while(q->next)
{
if(sstr == q->next->data.name)
{
cout<<"NOT FOUND!"<<endl;
break;
}
q = q->next;
}
if(q->next == NULL)
{
p->next->data.name = sstr;
break;
}
}
if(str == "tel")
{
cin>>sstr;
LinkList q = L;
while(q->next)
{
if(sstr == q->next->data.phoneNumber)
{
cout<<"NOT FOUND!"<<endl;
break;
}
q = q->next;
}
if(q->next == NULL)
{
p->next->data.phoneNumber = sstr;
break;
}
}
if(str == "email")
{
cin>>sstr;
LinkList q = L;
while(q->next)
{
if(sstr == q->next->data.e_mail)
{
cout<<"NOT FOUND!"<<endl;
break;
}
q = q->next;
}
if(q->next == NULL)
{
p->next->data.e_mail = sstr;
break;
}
}
}
else
{
p = p->next;
}
}
}
void del(LinkList &L, string name)//删除
{
LinkList p = L;
LinkList q;
while(p->next)
{
if(name == p->next->data.name)
{
q = p->next;
p->next = q->next;
delete q;
}
p = p->next;
}
if(!p)
{
cout<<"NOT FOUNT!"<<endl;
}
}
int main()
{
LinkList L;
InitList(L);
int n, m;
string operate, nname;
cin>>n;
while(n--)
{
add(L);
}
cin>>m;
while(m--)
{
cin>>operate;
if(operate == "del")
{
cin>>nname;
del(L, nname);
}
else if(operate == "query")//chaxun
{
cin>>nname;
query(L, nname);
}
else if(operate == "modify")//修改
{
cin>>nname;
modify(L, nname);
}
else if(operate == "add")
{
add(L);
}
else if(operate == "print")
{
print(L);
}
}
return 0;
}
2
1 aaa 13579246811 aaa@gmail.com
2 csl 13579246833 cls@gmail.com
9
del aaa
query aaa
query csl
modify csl email clsnb@gmail.com
query csl
add 3 cnz 01234567890 cnznb@gmail.com
modify cnz name cnznb
modify cnznb tel 13579246822
print
NOT FOUND!
2 csl 13579246833 cls@gmail.com
2 csl 13579246833 clsnb@gmail.com
2 csl 13579246833 clsnb@gmail.com
3 cnznb 13579246822 cnznb@gmail.com
样例中,
del aaa 表示删除aaa的信息
query aaa 查询aaa的信息,不存在,输出“NOT FOUND!”
query csl 表示查询csl的信息,输出“2 csl 13579246833 [email protected]”
modify csl email [email protected] 表示修改csl的邮箱为“[email protected]”
query csl 表示查询csl的信息,输出“2 csl 13579246833 [email protected]”
add 3 cnz 01234567890 [email protected] 添加一条员工信息
modify cnz name cnznb 修改cnz的name为cnznb
modify cnznb tel 13579246822 修改cnz的tel为13579246822
print 按编号顺序打印通讯录