#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=nodepData;
pNext=nodepNext;
}
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();
CNodeGetListHead(){return pHead;}
CNodeGetListNextNode(CNodepnode);
};
CNodeCList::GetListNextNode(CNode pnode)
{
CNodep1=pnode;
return p1->pNext;
};
void CList::AddNode(CNodepnode)
{
if(pHead==0)
{
pHead=pnode;
pnode->pNext=0;
return;
}
else
{
pnode->pNext=pHead;
pHead=pnode;
}
};
CNodeCList::DeleteNode(CNodepnode)
{
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)
{
CNodep1=pHead;
while(p1)
{
if(p1->pData->Compare(data)==0)
return p1;
p1=p1->pNext;
}
return 0;
}
void CList::ShowList()
{
CNodep1=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(charnumber,char name,charaddres)
{
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,tempszNumber);
strcmp(szAddres,tempszAddres);
}
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结束):";
cinignore();
cingetline(szNumber,20);
while(strcmp(szNumber,"0"),strcpy(szAddres,"0"))
{
cout<<"输入姓名:";
cout<<"输入地址:";
cingetline(szName,20);
cingetline(szAddres,20);
pTel=new CTelRecord;
pTel->SetRecord(szName,szNumber,szAddres);
pNode=new CNode;
pNode->InputData(pTel);
TelListAddNode(pNode);
cout<<"输入姓名(输入0结束):";
cout<<"输入地址(输入0结束):";
cingetline(szName,20);
cingetline(szAddres,20);
}
cout<<endl<<endl;
}
void DisplayRecord(CList&TelList)
{
cout<<setw(15)<<"电话号码"<<setw(15)<<"姓名"<<endl;
TelListShowList();
cout<<endl<<endl;
}
void LookUpRecord(CList &TelList)
{
CNode pLook;
char szNumber[20];
cout<<"输入您需要查找的号码(输入0结束)";
cingetline(szNumber,20);
while(strcmp(szNumber,"0"))
{
CTelRecord tele (szNumber,"0");
pLook=TelListLookUp(tele);
if(pLook)
{
cout<<"在电话薄中找到"<<" ,内容是:"<<endl;
pLook->ShowNode();
}
else
cout<<"在电话薄中查找不到"<<szNumber<<""<<endl;
cout<<"输入您需要查找的号码(输入0结束)";
cingetline(szNumber,20);
}
cout<<endl<<endl;
system("pause");
}
void DeleteRecord(CList&TelList)
{
CNode pLook;
char szNumber[20];
cout<<"输入您需要删除的号码(输入0结束)";
cingetline(szNumber,20);
while(strcmp(szNumber,"0"))
{
CTelRecord tele(szNumber,"0");
pLook=TelListLookUp(tele);
if(pLook)
{
cout<<"在电话薄中找到"<<szNumber<<",内容是:"<<endl;
pLook->ShowNode();
TelListDeleteNode(pLook);
cout<<szNumber<<"的资料已删除"<<endl;
delete pLook;
}
else
cout<<"在电话薄中查找不到"<<szNumber<<""<<endl;
cout<<"输入您需要删除的号码(输入0结束)";
cingetline(szNumber,20);
}
cout<<endl<<endl;
}
void StoreFile(CList &TelList)
{
ofstream outfile("TELEPHONEDAT",ios::binary);
if(! outfile)
{
cout<<"数据文件打开错误,没有将数据存入文件!\n";
return;
}
CNode pnode;
CTelRecord pTel;
string strName,strNumber;
pnode=TelListGetListHead();
while(pnode)
{
pTel=(CTelRecord )pnode->GetData();
outfilewrite((char )pTel,sizeof(CTelRecord));
pnode=TelListGetListNextNode(pnode);
}
outfileclose();
}
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("TELEPHONEDAT",ios::binary);
if(! infile)
{
cout<<"没有数据文件!\n\n";
return;
}
CNode pNode;
CTelRecord pTel;
while(! infileeof())
{
pTel=new CTelRecord;
infileread((char)pTel,sizeof(CTelRecord));
pNode=new CNode;
pNode->InputData(pTel);
TelListAddNode(pNode);
}
TelListDeleteNode(pNode);
infileclose();
}
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;
cinignore();
Operate(strChoice,TelList);
}
while(strChoice!="0");
cout<<"\n\n\t欢迎再次使用电话薄数据系统\n\n";
return 0;
}
你对结构体中的子结构体的赋值也是不对的……
等我看看怎么改比较好……
改成这样:
#include<stdioh>#include<stringh>
struct data
{
int month;
int day;
int year;
};
struct stud
{
char name[20];
char tele[12];
char zip[7];
struct data birthday;
char addre[30];
};
/{/
int main()
{
//birthday是个整体,要加{}给birthday赋值
struct stud stu[30]={
"Liming","123456789","216546",{3,14,2001},"beijing",
"Xavi","789456123","216549",{4,5,2009},"tianjing",
"Messi","894564613","121487",{12,1,1993},"shanghai"};
int k,i,j,n=3;
struct stud temp;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(strcmp(stu[j]name,stu[k]name)<0)
k=j;
temp=stu[i];
stu[i]=stu[k];
stu[k]=temp;
}
printf("姓名 电话 邮编 生日 地址\n");
for(i=0;i<3;i++)
printf("%10s%12s%8s %2d-%2d-%4d %15s\n",stu[i]name,stu[i]tele,stu[i]zip,stu[i]birthdaymonth,stu[i]birthdayday,stu[i]birthdayyear,stu[i]addre);//少了电话……
return 0;
}
仔细调试啊,少年……
自己调试才能提高自己的编程能力。
为你解决疑问其实就是调试的过程,也是我学习的过程。
傍晚好,少年!
欢迎分享,转载请注明来源:品搜搜测评网