用C++实现手机通讯录(好的再加分!)

2025-04-13 22:53:44
推荐回答(2个)
回答1:

#include
#include
#include
#include
using namespace std;
class CData
{
public:
CData(){};
virtual int Compare(CData &)=0;
virtual void Show()=0;
virtual ~CData(){};
};
class CNode
{
private:
CData *pData;
CNode *pNext;
public:
CNode(){pData=0;pNext=0;};
CNode(CNode &node)
{
pData=node.pData;
pNext=node.pNext;
}
void InputData(CData *pdata){pData=pdata;}
void ShowNode(){pData->Show();}
CData *GetData(){return pData;}
friend class CList;
};
class CList
{
CNode *pHead;
public:
CList(){pHead=0;}
~CList(){DeleteList();}
void AddNode(CNode *pnode);
CNode *DeleteNode(CNode *);
CNode *LookUp(CData&);
void ShowList();
void DeleteList();
CNode*GetListHead(){return pHead;}
CNode*GetListNextNode(CNode*pnode);
};
CNode*CList::GetListNextNode(CNode *pnode)
{
CNode*p1=pnode;
return p1->pNext;
};
void CList::AddNode(CNode*pnode)
{
if(pHead==0)
{
pHead=pnode;
pnode->pNext=0;
return;
}
else
{
pnode->pNext=pHead;
pHead=pnode;
}
};
CNode*CList::DeleteNode(CNode*pnode)
{
CNode *p1,*p2;
p1=pHead;
while(p1!=pnode&&p1->pNext!=0)
{
p2=p1;
p1=p1->pNext;
}
if(p1==pHead)
{
pHead=pHead->pNext;
return pnode;
}
p2->pNext=p1->pNext;
return pnode;
}
CNode *CList::LookUp(CData&data)
{
CNode*p1=pHead;
while(p1)
{
if(p1->pData->Compare(data)==0)
return p1;
p1=p1->pNext;
}
return 0;
}
void CList::ShowList()
{
CNode*p1=pHead;
while(p1)
{
p1->pData->Show();
p1=p1->pNext;
}
}
void CList::DeleteList()
{
CNode *p1,*p2;
p1=pHead;
while(p1)
{
delete p1->pData;
p2=p1;
p1=p1->pNext;
delete p2;
}
}
class CTelRecord:public CData
{
private:
char szNumber[20];
char szName[20];
char szAddres[20];
public:
CTelRecord(){strcpy(szNumber,"\0");strcpy(szName,"\0");strcpy(szAddres,"\0");}
CTelRecord(char *number,char *name,char *addres=NULL)
{
strcpy(szNumber,number);
strcpy(szName,name);
strcpy(szAddres,addres);
}
void SetRecord(char*number,char *name,char*addres)
{
strcpy(szNumber,number);
strcpy(szName,name);
strcpy(szAddres,addres);
}
int Compare(CData &);
void Show();
};
int CTelRecord::Compare(CData &data)
{
CTelRecord &temp=(CTelRecord &)data;
return strcmp(szNumber,temp.szNumber);
strcmp(szAddres,temp.szAddres);
}
void CTelRecord::Show()
{
cout< }
void AddRecord(CList&TelList)
{
CNode *pNode;
CTelRecord *pTel;
char szName[20],szNumber[20],szAddres[20];
cout<<"输入电话号码(输入0结束):";
cin.ignore();
cin.getline(szNumber,20);
while(strcmp(szNumber,"0"),strcpy(szAddres,"0"))
{
cout<<"输入姓名:";
cout<<"输入地址:";
cin.getline(szName,20);
cin.getline(szAddres,20);
pTel=new CTelRecord;
pTel->SetRecord(szName,szNumber,szAddres);
pNode=new CNode;
pNode->InputData(pTel);
TelList.AddNode(pNode);
cout<<"输入姓名(输入0结束):";
cout<<"输入地址(输入0结束):";
cin.getline(szName,20);
cin.getline(szAddres,20);
}
cout< }
void DisplayRecord(CList&TelList)
{
cout< TelList.ShowList();
cout<}
void LookUpRecord(CList &TelList)
{
CNode *pLook;
char szNumber[20];
cout<<"输入您需要查找的号码(输入0结束)";
cin.getline(szNumber,20);
while(strcmp(szNumber,"0"))
{
CTelRecord tele (szNumber,"0");
pLook=TelList.LookUp(tele);
if(pLook)
{
cout<<"在电话薄中找到"<<" ,内容是:"< pLook->ShowNode();
}
else
cout<<"在电话薄中查找不到"< cout<<"输入您需要查找的号码(输入0结束)";
cin.getline(szNumber,20);
}
cout< system("pause");
}
void DeleteRecord(CList&TelList)
{
CNode *pLook;
char szNumber[20];
cout<<"输入您需要删除的号码(输入0结束)";
cin.getline(szNumber,20);
while(strcmp(szNumber,"0"))
{
CTelRecord tele(szNumber,"0");
pLook=TelList.LookUp(tele);
if(pLook)
{
cout<<"在电话薄中找到"< pLook->ShowNode();
TelList.DeleteNode(pLook);
cout< delete pLook;
}
else
cout<<"在电话薄中查找不到"< cout<<"输入您需要删除的号码(输入0结束)";
cin.getline(szNumber,20);
}
cout< }
void StoreFile(CList &TelList)
{
ofstream outfile("TELEPHONE.DAT",ios::binary);
if(! outfile)
{
cout<<"数据文件打开错误,没有将数据存入文件!\n";
return;
}
CNode *pnode;
CTelRecord *pTel;
string strName,strNumber;
pnode=TelList.GetListHead();
while(pnode)
{
pTel=(CTelRecord *)pnode->GetData();
outfile.write((char *)pTel,sizeof(CTelRecord));
pnode=TelList.GetListNextNode(pnode);
}
outfile.close();
}
void Operate(string&strChoice,CList&TelList)
{
if(strChoice=="1")
AddRecord(TelList);
else if(strChoice=="2")
DisplayRecord(TelList);
else if(strChoice=="3")
LookUpRecord(TelList);
else if(strChoice=="4")
DeleteRecord(TelList);
else if(strChoice=="0")
StoreFile(TelList);
else
cout<<"输入错误,请重新输入您的选择:";
}
void LoadFile(CList&TelList)
{
ifstream infile("TELEPHONE.DAT",ios::binary);
if(! infile)
{
cout<<"没有数据文件!\n\n";
return;
}
CNode *pNode;
CTelRecord *pTel;
while(! infile.eof())
{
pTel=new CTelRecord;
infile.read((char*)pTel,sizeof(CTelRecord));
pNode=new CNode;
pNode->InputData(pTel);
TelList.AddNode(pNode);
}
TelList.DeleteNode(pNode);
infile.close();
}
int main(void)
{
CList TelList;
system("cls");
cout<<"\t欢迎进入电话薄数据系统\n";
LoadFile(TelList);
string strChoice;
do
{
cout<<"\t1.添加电话薄记录\n";
cout<<"\t2.显示电话薄内容\n";
cout<<"\t3.根据号码查询电话薄数据\n";
cout<<"\t4.根据号码删除电话薄数据\n";
cout<<"\t0.退出系统\n\n\n";
cout<<"请输入您的选择:";
cin>>strChoice;
cin.ignore();
Operate(strChoice,TelList);
}
while(strChoice!="0");
cout<<"\n\n\t欢迎再次使用电话薄数据系统\n\n";
return 0;
}

回答2:

听不明白 你说的功能我的手机已经有了