当前位置:编程学习 > C/C++ >>

用C++做一个通讯录

通讯录包括姓名,电话号码,访问,插入,删除推出的功能
答案:
#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<<"电

上一个:C++语言的技巧?
下一个:c++的模板定义?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,