用C++编写通讯录
来源:优易学  2011-12-18 11:50:48   【优易学:中国教育考试门户网】   资料下载   IT书店

  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;
  }
  }
  }

上一页  [1] [2] [3] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训