⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 通讯录.txt

📁 简单链表实现 基本功能: (1)添加记录 先判断链表是否已经存在。若不存在
💻 TXT
字号:
#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::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 szName[20]; 
char szNumber[20]; 
public: 
CTelRecord(){strcpy(szName,"\0");strcpy(szNumber,"\0");} 
CTelRecord(char *name,char *number) 
{ 
strcpy(szName,name); 
strcpy(szNumber,number); 
} 
void SetRecord(char *name,char *number) 
{ 
strcpy(szName,name); 
strcpy(szNumber,number); 
} 
int Compare(CData &); 
void Show(); 
}; 
int CTelRecord::Compare(CData &data) 
{ 
CTelRecord &temp=(CTelRecord &)data; 
return strcmp(szName,temp.szName); 
} 
void CTelRecord::Show() 
{ 
cout<<setw(15)<<szName<<setw(15)<<szNumber<<endl; 
} 
void AddRecord(CList &TelList) 
{ CNode *pNode; 
 CTelRecord *pTel; 
 char szName[20],szNumber[20]; 
 cout<<"shuruxingming(0jieshu):"; 
 cin.ignore(); 
 cin.getline(szName,20); 
 while(strcmp(szName,"0")) 
 { 
 cout<<"shurudianhuahaoma:"; 
 cin.getline(szNumber,20); 
 pTel=new CTelRecord; 
 pTel->SetRecord(szName,szNumber); 
 pNode=new CNode; 
 pNode->InputData(pTel); 
 TelList.AddNode(pNode); 
 cout<<"shuruxingming(0jieshu):"; 
 cin.getline(szName,20); 
 } 

 cout<<endl<<endl; 
} 
void DisplayRecord(CList &TelList) 
{ 
cout<<setw(15)<<"xingming"<<setw(15)<<"dianhuahaoma"<<endl; 
TelList.ShowList(); 
cout<<endl<<endl; 
} 

void LookUpRecord(CList &TelList) 
{ 
CNode *pLook; 
char szName[20]; 
cout<<"shuru chaxun xingming(0jiesu):"; 
 
 
  
 作者:124.119.50.*   2008-2-16 03:42   回复此发言    
 
--------------------------------------------------------------------------------
 
4 回复:跪求个通讯录c程序源代码  
 cin.getline(szName,20); 
while(strcmp(szName,"0")) 
{ 
CTelRecord tele(szName,"0"); 
pLook=TelList.LookUp(tele); 
if(pLook) 
{ 
cout<<"zaidianhuabuzhongzhaodao"<<szName<<",neirongshi:"<<endl; 
pLook->ShowNode(); 
} 
else 
cout<<"zaidianhuabuzhongzhaobudao"<<szName<<"."<<endl; 
cout<<"shuruniniyaozhaode xingming(0jiesu):"; 
cin.getline(szName,20); 
} 
cout<<endl<<endl; 
system("pause"); 
} 
void DeleteRecord(CList &TelList) 
{ 
CNode *pLook; 
char szName[20]; 
cout<<"shuruninxuyaoshanchudexingming(shuru0jieshu)"; 
cin.getline(szName,20); 
while(strcmp(szName,"0")) 
{ 
CTelRecord tele(szName,"0"); 
pLook=TelList.LookUp(tele); 
if(pLook) 
{ 
cout<<"zai dianhuabuzhongzhaodao"<<szName<<",neirongshi:"<<endl; 
pLook->ShowNode(); 
TelList.DeleteNode(pLook); 
cout<<szName<<"dezhiliaoyishanchu"<<endl; 
delete pLook; 
} 
else 
cout<<"zaidianhuabuzhongchazhaobudao"<<szName<<"."<<endl; 
cout<<"shuruninxuyaoshanchudexingming(0jieshu)"; 
cin.getline(szName,20); 
} 
cout<<endl<<endl; 
} 
void StoreFile(CList &TelList) 
{ ofstream outfile("TELEPHONE.DAT",ios::binary); 
 if(! outfile) 
 {  
 cout<<"shujuwenjiandakaicuowu,meyoujianshujuchunwenjian!\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<<"shurucuowu,qingshurunindexuanzhe:"; 
} 
void LoadFile(CList &TelList) 
{ ifstream infile("TELEPHONE.DAT",ios::binary); 
if(! infile) 
{cout<<"meiyoushujuwenjian!\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 mian(void) 
{ CList TelList; 
 system("cls"); 
cout<<"\t welcome from!\n"; 
LoadFile(TelList); 
string strChoice; 
do 
{ 
cout<<"\t1.tianjia\n"; 
cout<<"\t2.show\n"; 
cout<<"\t3.cha xun\n"; 
cout<<"\t4.delete\n"; 
cout<<"\t0.exit\n\n\n"; 
cout<<"qing shuru nin de choice"; 
cin>>strChoice; 
cin.ignore(); 
Operate(strChoice,TelList); 
}while(strChoice!="0"); 
cout<<"\n\n\t welcome shiyong dianhabu\n\n"; 
return 0; 
}  
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -