用C++编写一个简易的学生通讯录系统。能实现对通讯录内容的编辑、添加、删除、修改、统计等操作?

2025-03-23 11:58:24
推荐回答(1个)
回答1:

#include
#include
#include
#include
#include
struct Address //说明结构
{
char name[20];
char Tel[20];
char Email[20];
char Relation[20];
Address *next ;
} ;
Address allone[3] ;
void Createlist(Address *&head) //建立链表函数
{ Address *s,*p;
s=new Address;
int a=1;
while ( a==1)
{
cout<<"请输入姓名:";
cin>>s->name;
cout<<"请输入电话号:";
cin>>s->Tel;
cout<<"请输入Email:";
cin>>s->Email;
cout<<"请输入与您关系:";
cin>>s->Relation;
{
if ( head == NULL ) head = s ;
else p->next = s ;
}
p = s ;
s = new Address ;
cout<<"是否继续输入,输入按1不输入按0 :"; //判断是否继续输入
cin>>a;
}
p->next = NULL;
delete s;
return ;
}
void showlist( Address *head ) //显示链表
{ cout << "您的通信录为: "<< '\n';
while( head )
{
cout << head->name << '\t';
cout <Tel<<'\t';
cout << head->Email << '\t';
cout << head->Relation << '\n';
head = head->next ;
}
cout << endl ;
}
void Chayuelist(Address *head) //查阅链表
{
char chayue[20];
cout<<"请输入你所要查阅的姓名:";
cin>>chayue;
while( head )
{
if(strcmp(head->name,chayue)==0)
{
cout << head->name << '\t';
cout <Tel<<'\t';
cout << head->Email << '\t';
cout << head->Relation << '\n';
}
head = head->next ;
}
cout << endl ;
}
void baocun(Address *head)//保存
{
ofstream outstuf ;
outstuf.open("c:\\save.txt", ios::out ) ;
if ( !outstuf )
{
cerr << "File could not be open." << endl ;
abort();
}
while( head )
{
outstuf << head->name << ' '<< head->Tel<<' '<< head->Email << ' '<< head->Relation << '\n' ;
cout << "? " ;
head = head->next ;
}
}
void tianjia(Address *&head) //添加
{
Address *s;
s=new Address;//生成新结点
cout<<"\n\t\t请输入姓名:";
cin>>s->name;
cout<<"\n\t\t请输入电话号:";
cin>>s->Tel;
cout<<"\n\t\t请输入Email:";
cin>>s->Email;
cout<<"\n\t\t请输入与您关系:";
cin>>s->Relation;
cout<<"\n\t\t你已经添加成功";
s->next=head;
head=s;
return ;
}
void main() //主函数
{ system("cls");
int gongnenghao;
Address*head = NULL ;
cout<<"*************************本程序为通讯录管理系统***********************"<while(gongnenghao<=11)
{ system("cls");
cout<<" 请选择你所要实现的功能的编号"<cout<<"\n\t\t1.写记录"<cout<<"\n\t\t2.显示记录"<cout<<"\n\t\t3.查阅记录"<cout<<"\n\t\t5.删除记录"<cout<<"\n\t\t8.保存记录"<cout<<"\n\t\t9.添加记录"<cout<<"\n\t\t111.退出"<cin>>gongnenghao;
if(gongnenghao==1)
{
Createlist(head);
}
if (gongnenghao==2)
{
showlist(head);
}
if (gongnenghao==3)
{
Chayuelist(head);
}
if (gongnenghao==8)
{
baocun(head);
}
if(gongnenghao>9)
{
cout<<"退出";
}
if (gongnenghao==9)
{
tianjia(head);
}
}
}