用C++编的通讯录----虽然很粗糙,但也是辛苦劳动的成果o(∩_∩)o...

         我当时做这个实验的时候就想上网找一个参照的----呵呵,这习惯不好,什么都到网上找----现在做完了,把自己的在高手眼中不怎么样的代码拷上来,为新手铺个路,也请各位专家们多多指教啦。

#include<iostream.h>
#include<string>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>


struct Address
{
 char name[20];
 char tel[20];
 char email[50];
 char relation[10];
 Address*next;
};

//判断email
int Doe(char * s)
{
 int a=1;
 if(*s!='_'&&*s!='/'&&*s!='//'&&*s!='.')
 {
     while(*s)
  {
      if(
*s=='@')
   {
    a=0;
    break;
   }
      s++;
  }
      if(*s=='/0')
  {
   a=1;
   cout<<"没有@!"<<endl;
  }
 }
 else cout<<"首字母不能是:_、/、//、./n";
 return a;
}

//添加记录
void CreatList(Address*&head)    //引用参数是表头指针
{
 Address*s,*p;
 
 cout<<"(添加记录一直到输入人名为0时结束。)"<<endl;
 s=new Address;
 cout<<"name :";
 cin>>s->name;
 cout<<"tel :";
 cin>>s->tel;
 do
 {
     cout<<"email :"<<endl;
     cin>>s->email;
 }
 while(Doe(s->email));
 cout<<"relation :";
 cin>>s->relation;

 while(strcmp(s->name,"0/0"))   
 {
  if(head==NULL)    //判断列表是否已经存在。若不存在,则首先自动新建一个链表。
  {
   cout<<"列表不存在,新建一个链表。"<<endl;
   head=s;
  }else
  {
   p->next=s;
  }
  p=s;
  s=new Address;
  cout<<"name :";
     cin>>s->name;
     cout<<"tel :";
     cin>>s->tel;
     do
  {
   cout<<"email :"<<endl;
         cin>>s->email;
  }
  while(Doe(s->email));
     cout<<"relation :";
  cin>>s->relation;
 }
 p->next=NULL;
 delete s;    //释放名字为0的结点。
 return;
}

//显示记录
void ShowList(Address*head)
{
 cout<<"显示当前所有记录:/n";
 while(head)
 {
  cout<<head->name<<'/t'<<head->tel<<'/t'<<head->email<<'/t'<<head->relation<<'/n';
  head=head->next;
 }
 cout<<endl;
}

//查询记录
void FindPerson(Address*head)
{
 char in_name[20];
 cout<<"输入要查询的名字:";
 cin>>in_name;
 
 while(head)
 {
  if(strcmp(head->name,in_name)==0)    //比较名字是否相等。
  {
             cout<<head->name<<'/t'<<head->tel<<'/t'<<head->email<<'/t'<<head->relation<<'/n';
    break;
  }
  head=head->next;
 }
 if(head==NULL)
  cout<<"没有你要找的名字!";
 cout<<endl;

}

//通讯录排序
void Bubble(Address*head)   
{
 Address*m=head;
 Address*start=head,*present;
 present=start->next;
 Address*r=new Address;

 while(head->next!=NULL)
 {
  while(present!=NULL)
  {
   if(strcmp(start->name,present->name)>0)
   {
    strcpy(r->name,start->name);
    strcpy(r->tel,start->tel);
    strcpy(r->email,start->email);
    strcpy(r->relation,start->relation);
    
    strcpy(start->name,present->name);
    strcpy(start->tel,present->tel);
    strcpy(start->email,present->email);
    strcpy(start->relation,present->relation);
    
    strcpy(present->name,r->name);
    strcpy(present->tel,r->tel);
    strcpy(present->email,r->email);
    strcpy(present->relation,r->relation);

 


   }
   start=present;
   present=present->next;

  }
  start=m;
  present=m->next;
  head=head->next;
 }
 
    cout<<"显示当前所有记录:/n";
 while(m)
 {
  cout<<m->name<<'/t'<<m->tel<<'/t'<<m->email<<'/t'<<m->relation<<'/n';
  m=m->next;
 }
 cout<<endl;

}

//删除记录
void MoveAway(Address*head)
{
    Address*start,*present=head;
 char in_name[20];
 cout<<"输入要删除人的名字:";
 cin>>in_name;
 
 while(head)
 {
  if(strcmp(head->name,in_name)==0)    //比较名字是否相等。
  {
             cout<<"要删除的内容是: "<<endl;
    cout<<head->name<<'/t'<<head->tel<<'/t'<<head->email<<'/t'<<head->relation<<'/n';
    break;
  }
  
  if(head->next!=NULL)
      head=head->next;
  else
   cout<<"没有你要找的名字!"; break;   
 }
 cout<<endl;
 start=present;
 present=head;
 head=start;


 if(!head)
 {    cout<<"列表是空的。/n"; return;}
 if(present==head)    //被删除的节点是头节点。
 {
  start=head;
  head=head->next;
  delete start;
  start=NULL;
  cout<<"已删除。";
  return;
 }
    for(start=head;start!=present;start=start->next)    //被删除节点不是头节点。找到被删除节点的前一节点。
 {
  if(start->next==present)
  {
   start->next=present->next;
   delete present;
   present=NULL;
            cout<<"已删除。";
      return;
  }
 }
 return;
}

//更新记录
void ReSet(Address*head)
{
 Address*s=head;
 char in_name[20];
 cout<<"输入要更新的名字:";
 cin>>in_name;
 int i;
 
 while(head)    //首先判断该记录是否存在。
 {
  if(strcmp(head->name,in_name)==0)    //比较名字是否相等。
  {
             cout<<"要更新的内容存在。"<<endl;
             cout<<head->name<<'/t'<<head->tel<<'/t'<<head->email<<'/t'<<head->relation<<endl;
    break;
  }
  head=head->next;
 }
 if(head==NULL)
  cout<<"没有你要找的名字!";
 cout<<endl;
 head=s;
 
 while(s)   
 {
  if(strcmp(s->name,in_name)==0)    //比较名字是否相等。
  {
             cout<<"要更新什么内容?输入1选择更新人名;输入2选择更新电话;输入3选择更新Email;输入4选择更新与本人的关系。"<<endl;
    cin>>i;
    cout<<"输入要更改的内容。"<<endl;
    switch(i)
    {
    case 1:cin>>s->name;break;
             case 2:cin>>s->tel;break;
    case 3:cin>>s->email;break;
    case 4:cin>>s->relation;
    }
             cout<<"更新成功!"<<endl;
    cout<<s->name<<'/t'<<s->tel<<'/t'<<s->email<<'/t'<<s->relation<<endl;
    break;
  }
  s=s->next;
 }
}

//按关系显示记录
void ListInRela(Address*head)
{
 char in_rela[10];
    cout<<"请输入要显示的记录的关系:"<<endl;
 cin>>in_rela;

 while(head)
 {
  if(strcmp(head->relation,in_rela)==0)    //比较是否相等。
  {
             cout<<head->name<<'/t'<<head->tel<<'/t'<<head->email<<'/t'<<head->relation<<'/n';
  }
  head=head->next;
 }
 cout<<endl;

}

//保存记录
void KeepList(Address*head)
{
 char fileName[80];
 ofstream outstuf;    //建立输出文件流对象
 cout<<"请输入要保存记录的目的文件名:/n";
 cin>>fileName;
 outstuf.open(fileName,ios::out);    //链接文件,指定打开方式
 if(!outstuf)    //调用重载算符函数测试流
 {
  cerr<<"文件无法打开。"<<endl;
  abort();
 }
 
 cout<<"开始保存记录……"<<endl;

 Address*s=head;
 while(s)    //向流插入数据
 {
  outstuf<<s->name<<' '<<s->tel<<' '<<s->email<<' '<<s->relation<<'/n';
  s=s->next;
 }
 outstuf.close();    //关闭文件
 cout<<"记录已保存。"<<endl; 
}

//读入记录
void ReadFile()
{
 Address*sa=new Address;
 
 ifstream instuf("d://file//Phonebook.txt",ios::in);    //打开文件
 if(!instuf)   
 {
  cerr<<"文件无法打开。"<<endl;
  abort();
 }

 while(instuf>>sa->name>>sa->tel>>sa->email>>sa->relation)    //提取数据
 {
  cout<<"读入成功,数据为:"<<endl;
  cout<<sa->name<<"  "<<sa->tel<<"  "<<sa->email<<"  "<<sa->relation<<endl;
 }
 instuf.close();    //关闭文件 
}

void main()
{
 int choice=1;
 Address*head=NULL;
 cout<<"添加记录…………………………………………1"<<endl;
 cout<<"显示记录…………………………………………2"<<endl;
 cout<<"查询记录…………………………………………3"<<endl;
 cout<<"通讯录排序………………………………………4"<<endl;
 cout<<"删除记录…………………………………………5"<<endl;
 cout<<"更新记录…………………………………………6"<<endl;
    cout<<"按关系显示记录…………………………………7"<<endl;
    cout<<"保存记录…………………………………………8"<<endl;
    cout<<"读入记录…………………………………………9"<<endl;
    cout<<"退出通讯录………………………………………0"<<endl;
    cout<<"请输入相应的序号选择要执行的任务"<<endl;
   
 while(cin>>choice)
 {
        switch(choice)
  {
  case 1:CreatList(head);continue;
  case 2:ShowList(head);continue;
  case 3:FindPerson(head);continue;
  case 4:Bubble(head);continue;
  case 5:MoveAway(head);continue;
  case 6:ReSet(head);continue;
  case 7:ListInRela(head);continue;
  case 8:KeepList(head);continue;
  case 9:ReadFile();continue;
  }
        if(choice==0)
  {
      cout<<"退出操作"<<endl;
      break;
  }
 }
 
 
}

 

 

 

 

 

  
   

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值