| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 557 人关注过本帖
标题:大虾们,分析通讯录代码,
只看楼主 加入收藏
zqllsszhuqi
Rank: 2
等 级:论坛游民
帖 子:26
专家分:45
注 册:2010-3-29
结帖率:75%
收藏
 问题点数:0 回复次数:0 
大虾们,分析通讯录代码,
#include<iostream>
#include<string>
#include<iomanip>
#include<list>
#include<fstream>
#include<algorithm>
#define SPACE "\t\t\t"
using namespace std;
struct Record{
 string name;
 string tel;
public:
 friend ostream& operator<<(ostream& os,const Record *rec){
  return os<<rec->name<<SPACE<<rec->tel<<endl;
 };
 friend istream& operator>>(istream& is,Record *rec){
  is>>rec->name;
  
  is>>rec->tel;
  return is;
 }
 static bool comp(string patten,int type,Record *rec){
  switch(type){
   case 0: {return rec->name==patten;}break;
   case 1: {return rec->tel==patten;}break;
  }
  return false;
 }
 static bool isvan(Record *rec){
  return (rec->name==""&&rec->tel=="");
 }
};
class RecordList:public list<Record*>
{
public:
 friend ostream& operator<<(ostream& os,const RecordList &c_rl){
  RecordList::const_iterator it=c_rl.begin();
  while(it!=c_rl.end()){
   os<<*it;
   it++;
  }
  return os;
 }
 friend istream& operator>>(istream& is,RecordList &rl){
  RecordList::const_iterator it;
  while(!is.eof()){
   Record *prec=new Record;
   is>>prec;
   if(Record::isvan(prec)) continue;
   rl.list<Record*>::push_back(prec);
  }
  return is;
 }
 void clear();
 iterator erase(iterator pos);
 ~RecordList(){
  this->clear();
 }
};
void RecordList::clear(){
    RecordList::const_iterator it=this->begin();
 while(it!=this->end()){
  delete(*it);
  it++;
 }
 this->list<Record*>::clear();
}
RecordList::iterator RecordList::erase(iterator pos){
 delete(*pos);
 return list<Record*>::erase(pos);
}
class AddressList{
protected:
 RecordList recList;
public:
 typedef RecordList::iterator Iterator;
 //从通信录中寻找一个记录
 virtual Iterator findRecord(string patten,int type,Iterator from);
 virtual void addRecord(Record* rec);
 virtual void removeRecord(Iterator it);
 virtual void editRecord(Record* rec,Iterator it);
 virtual void saveRecords(ostream& os);
 virtual void loadRecords(istream& is);
 virtual void addRecords(istream& is);
 virtual ~AddressList();
 const int size() const{return (int)recList.size();}
 const void clear(){recList.clear();}
 Iterator begin(){return recList.begin();}
 Iterator end() {return recList.end();}
};
AddressList::Iterator AddressList::findRecord(string patten,int type,Iterator from){
 Iterator it;
 it=from;
 while(it!=recList.end()){
  if(Record::comp(patten,type,*it)){
   return it;
  }
  it++;
 }
 return it;
}
void AddressList::addRecord(Record* rec){
 recList.push_back(rec);
}
void AddressList::removeRecord(Iterator it){
 recList.erase(it);
}
void AddressList::editRecord(Record* rec,Iterator it){
 delete(*it);
 *it=rec;
}
 
void AddressList::saveRecords(ostream& os){
 os<<recList;
}
void AddressList::loadRecords(istream& is){
 is>>recList;
}
void AddressList::addRecords(istream& is){
}
AddressList::~AddressList(){
 recList.clear();
}
class AddressBook : public AddressList{
protected:
 bool isModified;//通信录是否已被修改还未保存
public:
 AddressBook();
 virtual ~AddressBook();
 //开始通信录的用户界面操作
 virtual void start();
 //菜单处理函数管理程序的用户界面操作
 virtual void handleMenu();
 //显示记录
 virtual void displayRecords();
 //查询记录
 virtual void queryRecord();
 //添加记录
 virtual void addRecord();
 //保存记录
 virtual void saveRecords();
 //读取记录
 virtual void loadRecords();
 //结束程序
 virtual void quit();
 //清空当前记录信息
 virtual void clear();
protected:
 //菜单选择函数
 char menuSelect();
 //显示表头
 void dispTable();
};
AddressBook::AddressBook(){
}
AddressBook::~AddressBook(){;}
char AddressBook::menuSelect(){
 char a=0;
 cin>>a;
 if(a=='y'||a=='Y'){a='1';}
 if(a=='n'||a=='N'){a='2';}
 return a;
}
void AddressBook::start(){
 cout << "\n\t***** 欢迎使用sjb版通讯录 *****";
 handleMenu();
}
void AddressBook::handleMenu(){
        cout << "\n\n\t\t\t-- 菜单管理 --\n\n";
  cout << "\tA -- 添加人员纪录 (Add Record)\n"
    << "\tF -- 查询纪录 (Find Record)\n"
    << "\tD -- 显示纪录 (Display Records)\n"
    << "\tL -- 从文本或标准输入流中加载纪录 (Load Records)\n"
    << "\tC -- 清空当前记录信息\n"
    << "\tS -- 保存记录 (Save Records)\n"
    << "\tQ -- 结束程序 (QUIT)\n";
 switch(menuSelect()){
  
  case 'A':
        case 'a':addRecord();break;
        case 'F':
        case 'f':queryRecord();break;
  case 'D':
        case 'd':displayRecords();break;
  case 'L':
        case 'l':loadRecords();break;
     case 'C':
        case 'c':clear();break;
  case 'S':
        case 's':saveRecords();break;
  case 'Q':
        case 'q':quit();break;
 }
 handleMenu();
 
}  
void AddressBook::displayRecords(){
 cout<<"姓名"<<SPACE<<"电话"<<endl;
 AddressList::saveRecords(cout);
 cout<<"一共找到"<<size()<<"个对象。"<<endl;
 cout<<"1"<<SPACE<<"返回"<<endl;
 cout<<"2"<<SPACE<<"修改"<<endl;
 cout<<"3"<<SPACE<<"删除"<<endl;
 switch(menuSelect()){
  case '1':break;
  case '2':{
   if(!size()){
    cout<<"记录为0,无法修改。"<<endl;
    break;
   }
   cout<<"要修改第几个?"<<"1~"<<size()<<endl;
   int i;
   cin>>i;
   if(i<1||i>size()){
    break;
   }
   Record *prec=new Record;
   cout<<"姓名:"<<'\n'<<"电话:"<<'\n';
   cin>>prec;
   Iterator it=recList.begin();
   while(i!=1){
    it++;
    i--;
   }
   editRecord(prec,it);
  }break;
  case'3':{
   if(!size()){
    cout<<"记录为0,无法修改。"<<endl;
    break;
   }
   cout<<"要删除第几个?"<<"1~"<<size()<<endl;
   int i;
   cin>>i;
   if(i<1||i>size()){
    break;
   }
   Iterator it=recList.begin();
   while(i!=1){
    it++;
    i--;
   }
   removeRecord(it);
  }break;
 }
}
void AddressBook::addRecord(){
  string name,tel;
 cout << "\n输入名字: ";
 cin >> name;
 cout << "输入电话: ";
 cin >> tel;
 Record* temp=new Record;
 temp->name=name;
 temp->tel=tel;
 AddressList::addRecord(temp);
 cout<<"是否继续(Y/N)"<<endl;
 switch(menuSelect()){
  case '1':addRecord();break;
  case '2':break;
 }
}
void AddressBook::queryRecord(){
 cout<<"1"<<SPACE<<"按姓名查找"<<endl;
 cout<<"2"<<SPACE<<"按电话查找"<<endl;
 int type=-1;
 string patten;
 switch(menuSelect()){
  case '1':{
   type=0;
   cout<<"请输入姓名"<<endl;
   cin>>patten;
  }break;
  case '2':{
   type=1;
   cout<<"请输入电话号码"<<endl;
   cin>>patten;
  }break;
 }
 Iterator it=begin();
 list<Iterator> li;
 cout<<"姓名"<<SPACE<<"电话"<<endl;
 while((it=findRecord(patten,type,it))!=end()){
  cout<<*it;
  li.push_back(it);
  it++;
 }
 cout<<"一共找到"<<li.size()<<"个对象。"<<endl;
 cout<<"1"<<SPACE<<"返回"<<endl;
 cout<<"2"<<SPACE<<"修改"<<endl;
 cout<<"3"<<SPACE<<"删除"<<endl;
 switch(menuSelect()){
  case'1':break;
  case'2':{
   if(!li.size()){
    cout<<"记录为0,无法修改。"<<endl;
    break;
   }
   cout<<"要修改第几个?"<<"1~"<<li.size()<<endl;
   int i;
   cin>>i;
   Record *prec=new Record;
   cout<<"姓名:"<<'\n'<<"电话:"<<'\n';
   cin>>prec;
   list<Iterator>::const_iterator it=li.begin();
   while(i!=1){
    it++;
    i--;
   }
   editRecord(prec,*it);
  }break;
  case'3':{
   if(!li.size()){
    cout<<"记录为0,无法修改。"<<endl;
    break;
   }
   cout<<"要删除第几个?"<<"1~"<<li.size()<<endl;
   int i;
   cin>>i;
   list<Iterator>::const_iterator it=li.begin();
   while(i!=1){
    it++;
    i--;
   }
   removeRecord(*it);
  }break;
 }  
}
void AddressBook::saveRecords(){
 cout<<"1"<<SPACE<<"使用默认文件名。"<<endl;
 cout<<"2"<<SPACE<<"使用自定义文件名。"<<endl;
 cout<<"3"<<SPACE<<"返回"<<endl;
 switch(menuSelect()){
 case '1':{
  ofstream file("default",ios::app);
  AddressList::saveRecords(file);
  file.close();
  }break;
 case '2':{
  cout<<"请输入您定义的文件名:"<<endl;
  string fname;
  cin>>fname;
  ofstream file(fname.c_str(),ios::app);
  AddressList::saveRecords(file);
  file.close();
  }break;
 case '3':break;
 }
  
}
void AddressBook::loadRecords(){
 cout<<"1"<<SPACE<<"使用默认文件。"<<endl;
 cout<<"2"<<SPACE<<"使用自定义文件。"<<endl;
 cout<<"3"<<SPACE<<"返回"<<endl;
 switch(menuSelect()){
 case '1':{
  cout<<"1"<<SPACE<<"覆盖当前内容"<<endl;
  cout<<"2"<<SPACE<<"追加到当前文件"<<endl;
  switch(menuSelect()){
  case '1':
   AddressList::clear();break;
  case '2':break;
  }
   
  ifstream file("default");
  AddressList::loadRecords(file);
  file.close();
  }break;
 case '2':{
  cout<<"请输入文件名:"<<endl;
  string fname;
  cin>>fname;
  ifstream file(fname.c_str());
  AddressList::loadRecords(file);
  file.close();
  }break;
 case '3':break;
 }
}
void AddressBook::clear(){
 cout<<"是否要清除内存中所有内容?(Y/N)"<<endl;
 switch(menuSelect()){
   case '1':AddressList::clear();break;
   case '2':break;
 }
}
void AddressBook::quit(){
 cout<<"是否要保存文件?(Y/N)"<<endl;
 switch(menuSelect()){
  case'1':saveRecords();break;
  case'2':break;
 }
 exit(0);
}
 
  
   
int main(int argc, char *argv[])
{
 AddressBook a;
 a.start();
    system("PAUSE");
    return EXIT_SUCCESS;
   
}
搜索更多相关主题的帖子: 代码 通讯录 
2010-06-06 15:01
快速回复:大虾们,分析通讯录代码,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026760 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved