补充代码,得出运行结果,麻烦了,初学者,不太懂,希望对此了解一下
#include <iostream>
#include<cstring>
#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)
{
char name[20], number[20];
cout << "请输入姓名:";
cin >> name;
cout << "请输入电话号码:";
cin >> number;
CTelRecord *pRecord = new CTelRecord(name, number);
CNode *pNode = new CNode();
pNode->InputData(pRecord);
TelList.AddNode(pNode);
cout << "添加成功!" << endl;
}
void DisplayRecord(CList &TelList)
{
cout<<setw(15)<<"姓名"<<setw(15)<<"电话号码"<<endl;
TelList.ShowList();
cout<<endl<<endl;
}
void LookUpRecord(CList &TelList)
{
char name[20];
cout << "请输入要查询的姓名:";
cin >> name;
CTelRecord record;
record.SetRecord(name,"");
CNode *pNode = TelList.LookUp(record);
if (pNode != NULL)
{
cout << "查询结果:" << endl;
pNode->ShowNode();
}
else
{
cout << "未找到该记录!" << endl;
}
system("pause");
}
void DeleteRecord(CList &TelList)
{
char name[20];
cout << "请输入要删除的姓名:";
cin >> name;
CTelRecord record;
record.SetRecord(name,"");
CNode *pNode = TelList.LookUp(record);
if (pNode != NULL)
{
TelList.DeleteNode(pNode);
cout << "删除成功!" << endl;
}
else
{
cout << "未找到该记录!" << endl;
}
}
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
cout<<"输入错误,请重新输入你的选择:";
}
int main(void)
{
CList TelList;
system("cls");
cout<<"\t欢迎进入电话簿数据系统\n";
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;
}
代码补全后运行结果:
完整代码:
#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=pHead;
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* node = new CNode;
CTelRecord* rec = new CTelRecord;
char name[20] = { 0 }, number[20] = { 0 };
cout << "请输入姓名:";
cin >> name;
cout << "请输入电话号码:";
cin >> number;
rec->SetRecord(name, number);
node->InputData(rec);
TelList.AddNode(node);
cout << "添加成功!" << endl;
system("pause");
}
void DisplayRecord(CList& TelList)
{
cout << setw(15) << "姓名" << setw(15) << "电话号码" << endl;
TelList.ShowList();
cout << endl << endl;
}
void LookUpRecord(CList& TelList)
{
/********************//***添加代码********//********************/
CTelRecord rec;
char name[20] = { 0 }, number[20] = { 0 };
cout << "请输入要查找的姓名:";
cin >> name;
//cout << "请输入要查找的电话号码:";
//cin >> number;
rec.SetRecord(name, number);
CNode* p = TelList.LookUp(rec);
if (p == 0)
cout << "未找到!" << endl;
else
{
CTelRecord* pd = (CTelRecord*)p->GetData();
pd->Show();
}
system("pause");
}
void DeleteRecord(CList& TelList)
{
/********************//***添加代码********//********************/
CTelRecord rec;
char name[20] = { 0 }, number[20] = { 0 };
cout << "请输入要删除的姓名:";
cin >> name;
//cout << "请输入要删除的电话号码:";
//cin >> number;
rec.SetRecord(name, number);
CNode* p = TelList.LookUp(rec); //查找节点
if (p == 0)
cout << "未找到!" << endl;
else
{
TelList.DeleteNode(p);
cout << "已删除!" << endl;
}
system("pause");
}
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
cout << "输入错误,请重新输入你的选择:";
}
int main(void)
{
CList TelList;
system("cls");
cout << "\t欢迎进入电话簿数据系统\n";
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;
}
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Data;
ElementType Num;
PtrToNode Next;
};
typedef PtrToNode List;
List Read();
List Min(List L);
void Print( List L );
int main()
{
List L;
L=Read();
L=Min(L);
Print(L);
return 0;
}
List Read(){
int m,n;
List L,head;
head = L = (List)malloc(sizeof(struct Node));
scanf("%d",&n);
for(int i=0;i<n;i++){
L->Next = (List)malloc(sizeof(struct Node));
scanf("%d",&(L->Next->Data));
L->Next->Num=i+1;
L = L->Next;
}
return head;
}
void Print( List L ){
List p=L->Next;
while(p){
printf("%d", p->Data);
if(p->Next!=NULL){
printf(" ");
}
p=p->Next;
}
printf("\n");
}
List Min(List L){
int min,num;
List head=L;
List p=L->Next;
min=p->Data;
while(p){
if(p->Data <= min){
min=p->Data;
num=p->Num;
}
p=p->Next;
}
L=L->Next;
printf("最小值为:%d\n",min);
if(min%2==0){
for(int i=1;i<=num;i++){
p=L;
L=L->Next;
}
p->Next=L->Next;
}else{
for(int i=1;i<=num;i++){
p=L;
L=L->Next;
}
p->Data=L->Data;
L->Data=min;
}
return head;
}