计算机之家


 
标题: [软件类] 求C++高手帮助
zqh12356
新手上路
Rank: 1



UID 39509
精华 0
积分 10
帖子 35
威望 10
现金 39 币币
存款 0 币币
阅读权限 1
注册 2007-9-2
状态 离线
 
发表于 2008-1-3 19:52  资料  个人空间  短消息  加为好友            
求C++高手帮助

问题描述  
编写一个简单的通讯录管理程序。通讯录记录有姓名,地址(省、市(县)、街道),电话号码,邮政编码等四项。  
基本要求  
程序应提供的基本基本管理功能有:  
1) 添加:即增加一个人的记录到通信录中  
2) 显示:即在屏幕上显示所有通信录中的人员信息,应能分屏显示。  
3) 存储:即将通讯录信息保存在一个文件中。  
4) 装入:即将文件中的信息读入程序。  
5) 查询:可根据姓名查找某人的相关信息,若找到显示其姓名、地址、电话号码和邮政编码。  
6) 修改:可修改一个人的除姓名外其它信息。  
测试数据  
程序应输入不少于10个人员的通讯录信息,应考虑到人员可以同名的情况。  
实现提示  
程序可用一个单向链表来管理人员信息,每个人员的姓名,地址,电话号码和邮政编码用一个类Cperson来实现,作为链表的值指针指向这些Cperson类对象,通过链表的遍历可以操作这些数据。  
选做内容  
为了加快数据定位查找的速度,采用常用优先的方法对链表的各个节点进行排序,即一旦操作了一个人员的数据,他的数据就将被调用到链表的链首。这样经过有限次操作,经常查阅的人员的信息就将排在链表的前端。虽然不能说链首的节点一定是最常用的,但常用的节点一定会排在较靠前的部分,链表查找时所要走的平均距离一定较短。
附以下程序:
#include<iostream>  
#include<string>  
#include<iomanip>  
#include<fstream>  
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:ookUp(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<<setw(15)<<szAddres<<setw(15)<<szNumber<<setw(15)<<szName<<endl;  
}  
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<<endl<<endl;  
}  
void DisplayRecord(CList&TelList)  
{  
cout<<setw(15)<<"电话号码"<<setw(15)<<"姓名"<<endl;  
TelList.ShowList();  
cout<<endl<<endl;  
}  
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<<"在电话薄中找到"<<" ,内容是:"<<endl;  
pLook->ShowNode();  
}  
else  
cout<<"在电话薄中查找不到"<<szNumber<<"."<<endl;  
cout<<"输入您需要查找的号码(输入0结束)";  
cin.getline(szNumber,20);  
}  
cout<<endl<<endl;  
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<<"在电话薄中找到"<<szNumber<<",内容是:"<<endl;  
pLook->ShowNode();  
TelList.DeleteNode(pLook);  
cout<<szNumber<<"的资料已删除"<<endl;  
delete pLook;  
}  
else  
cout<<"在电话薄中查找不到"<<szNumber<<"."<<endl;  
cout<<"输入您需要删除的号码(输入0结束)";  
cin.getline(szNumber,20);  
}  
cout<<endl<<endl;  
}  
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;  
}

哪个高手帮助修改下以上程序实现 :存储:即将通讯录信息保存在一个文件中。  
和装入:即将文件中的信息读入程序。功能啊    无限感激

顶部
zqh12356
新手上路
Rank: 1



UID 39509
精华 0
积分 10
帖子 35
威望 10
现金 39 币币
存款 0 币币
阅读权限 1
注册 2007-9-2
状态 离线
 
发表于 2008-1-3 19:53  资料  个人空间  短消息  加为好友 
是: L

顶部
zqh12356
新手上路
Rank: 1



UID 39509
精华 0
积分 10
帖子 35
威望 10
现金 39 币币
存款 0 币币
阅读权限 1
注册 2007-9-2
状态 离线
 
发表于 2008-1-3 19:53  资料  个人空间  短消息  加为好友 
是: L

顶部
 

 
 
当前时区 GMT+8, 现在时间是 2009-1-8 14:50