#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
class Data
{public:
Data(){};
virtual int Compare(Data &)=0;
virtual void Show()=0;
virtual ~Data(){};
};
class Node
{Data *pData;
Node *next;
public:
Node(){pData=0;next=0;};
void Input(Data *pdata)
{pData=pdata;}
void ShowNode(){pData->Show();}
Data *Get(){return pData;}
friend class List;
};
class List
{Node *head;
public:
List(){head=0;}
~List(){Del_list();}
void Add_node(Node *pnode);
Node *Del_node(Node * );
Node *Lookfor(Data&);
void Showlist();
void Del_list();
Node *Gethead(){return head;}
Node *Getnext(Node *pnode);
};
Node * List::Getnext(Node *pnode)
{Node *p1 = pnode;
return p1->next;
};
void List::Add_node(Node *pnode)
{
if(head==0)
{
head=pnode;
pnode->next=0;
return;
}
else
{
pnode->next=head;
head=pnode;
}
};
Node * List::Del_node(Node *pnode)
{Node *p1,*p2;
p1=head;
while(p1!=pnode&&p1->next!=0)
{p2=p1;
p1=p1->next;
}
if(p1==head)
{head=head->next;
return pnode;
}
p2->next=p1->next;
return pnode;
}
Node *List::Lookfor(Data &data)
{Node *p1=head;
while(p1)
{
if(p1->pData->Compare(data)==0)
return p1;
p1=p1->next;
}
return 0;
}
void List::Showlist()
{
Node *p1=head;
while(p1)
{
p1->pData->Show();
p1=p1->next;
}
}
void List::Del_list()
{Node *p1,*p2;
p1=head;
while(p1)
{
delete p1->pData;
p2=p1;
p1=p1->next;
delete p2;
}
}
class Telrecord:public Data
{char name[20];
char num[20];
public:
Telrecord(){strcpy(name,"\0");strcpy(num,"\0");}
Telrecord(char *pname,char *pnum)
{strcpy(name,pname);strcpy(num,pnum);}
void Set(char *pname,char *pnum)
{strcpy(name,pname);strcpy(num,pnum);}
int Compare(Data &);
void Show();
};
int Telrecord::Compare(Data &data)
{Telrecord &temp=(Telrecord &)data;
return strcmp(name,temp.name);
}
void Telrecord::Show()
{cout<<setw(15)<<name<<setw(15)<<num<<endl;
}
void Add_record(List &TelList)
{Node *pNode;
Telrecord *pTel;
char name[20];
char num[20];
cout<<"请输入姓名:(输入0 over) ";
cin.ignore();
cin.getline(name,20);
while(strcmp(name,"0"))
{cout<<"请输入对方的电话号码: ";
cin.getline(num,20);
pTel=new Telrecord;
pTel->Set(name,num);
pNode=new Node;
pNode->Input(pTel);
TelList.Add_node(pNode);
cout<<"输入姓名(输入0 over): ";
cin.getline(name,20);
}
cout<<endl<<endl;
}
void Display(List &TelList)
{cout<<setw(15)<<"姓名"<<setw(15)<<"电话号码"<<endl;
TelList.Showlist();
cout<<endl<<endl;
}
void Lookforrecord(List &TelList)
{
Node *pLook;
char name[20];
cout<<"输入你要查找的姓名(输入0 over): ";
cin.getline(name,20);
while (strcmp(name,"0"))
{
Telrecord tele(name,"0");
pLook=TelList.Lookfor(tele);
if(pLook)
{cout<<name<<" 电话:"<<endl;
pLook->ShowNode();
}
else
cout<<"没有你要查早的相关信息"<<endl;
cout<<"重新输入要查找的姓名(按0结束)";
cin.getline(name,20);
}
cout<<endl<<endl;
system("pause");
}
void Del_record(List &TelList)
{Node *pLook;
char name[20];
cout<<"输入要删除的联系人信息(按0退出)";
cin.getline(name,20);
while (strcmp(name,"0"))
{Telrecord tele(name,"0");
pLook=TelList.Lookfor(tele);
if(pLook)
{cout<<name<<" 电话:"<<endl;
pLook->ShowNode();
TelList.Del_node(pLook);
cout<<name<<"资料已删除"<<endl;
delete pLook;
}
else
cout<<"没有你要查早的相关信息"<<endl;
cout<<"重新输入要删除的姓名(按0结束)";
cin.getline(name,20);
}
cout<<endl<<endl;
}
void StoreFile(List &TelList)
{ofstream outfile("TELEPHONE.DAT",ios::binary);
if(!outfile)
{cout<<"数据存入失败!\n";
return;
}
Node *pnode;
Telrecord *pTel;
string strname,strnum;
pnode=TelList.Gethead();
while(pnode)
{pTel=(Telrecord *)pnode->Get();
outfile.write((char *)pTel,sizeof(Telrecord));
pnode=TelList.Getnext(pnode);
}
outfile.close();
}
void Operate(string &strChoice,List &TelList)
{
if(strChoice=="1")
Add_record(TelList);
else if(strChoice=="2")
Display(TelList);
else if(strChoice=="3")
Lookforrecord(TelList);
else if(strChoice=="4")
Del_record(TelList);
else if(strChoice=="0")
StoreFile(TelList);
else
cout<<"输入错误,重新选择:";
}
void Load(List &TelList)
{ifstream infile("TELEPHONE.DAT",ios::binary);
if(!infile)
{cout<<"无数据!\n";
return;
}
Node *pNode;
Telrecord *pTel;
while(!infile.eof())
{pTel=new Telrecord;
infile.read((char *)pTel,sizeof(Telrecord));
pNode=new Node;
pNode->Input(pTel);
TelList.Add_node(pNode);
}
TelList.Del_node(pNode);
{infile.close();}
int main(void)
{List TelList;
system("cls");
cout<<"\t======简易通讯录======\n";
Load(TelList);
string strChoice;
do
{
cout<<"\t1.添加信息"\n;
cout<<"\t2.显示信息\n";
cout<<"\t3.查询信息\n";
cout<<"\t4.删除信息\n";
cout<<"\t5.查询信息\n";
cout<<"\t6.退出通讯录\n";
cin>>strChoice;
cin.ignore();
Operate(strChoice,TelList);
}
while(strChoice!="0");
cout<<"\t谢谢使用\n\n";
return 0;
}
求高手帮我调试成功