发布网友 发布时间:2022-04-23 07:23
共5个回答
热心网友 时间:2022-06-17 15:14
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
template <class T> class Link;
template <class T> class ListIterator;
template <class T> class List;
template <class T> class List{
public:
List();
void add(T name,T address,T tel);
List(const List<T>& source);
void deleteAllvalue();
List<T>* plicate() const;
virtual ~List(){
deleteAllvalue();
}
private:
Link<T>* p1stLink;
friend class Link<T>;
friend class ListIterator<T>;
};
template<class T> void List<T>::add(T name,T address,T tel){
p1stLink= new Link<T>(name,address,tel,p1stLink);
}
template<class T> List<T>* List<T>::plicate() const{
List<T>* newlist;
newlist = new List<T>;
if(p1stLink)
newlist->p1stLink=p1stLink->plicate();
else
newlist->p1stLink=0;
return newlist;
}
template<class T> List<T>::List():p1stLink(0){}
template<class T> List<T>::List(const List<T>& source){
if(source.p1stLink)
p1stLink=source.p1stLink->plicate();
else
p1stLink=0;
}
template<class T> void List<T>::deleteAllvalue(){
Link<T>* next;
for(Link<T>* i=p1stLink;i;i=next){
next = i->pNextLink;
i->pNextLink=0;
delete i;
}
}
template <class T> class Link{
public:
Link<T>* insert(T name,T address,T tel);
private:
Link(T name,T address,T tel,Link<T>* pNext);
Link(const Link<T>& source);
Link<T>* plicate() const;
T name;
T address;
T tel;
Link<T>* pNextLink;
friend class List<T>;
friend class ListIterator<T>;
};
template<class T> Link<T>* Link<T>::insert(T name,T address,T tel){
pNextLink=new Link(name,address,tel,pNextLink);
return pNextLink;
}
template<class T> Link<T>* Link<T>::plicate() const{
Link<T>* newlink;
if(pNextLink!=0)
newlink= new Link<T>(name,address,tel,pNextLink->plicate());
else
newlink=new Link<T>(name,address,tel,0);
return newlink;
}
template<class T> Link<T>::Link(T name,T address,T tel,Link<T>* pNext):name(name),address(address),tel(tel),pNextLink(pNext){
}
template<class T> Link<T>::Link(const Link<T>& source):name(source.name),address(source.address),tel(source.tel),pNextLink(source.pNextLink){
}
template<class T> class ListIterator{
public:
virtual int init();
ListIterator<T>(List<T>& refList);
virtual int operator ++(int);
virtual int operator !();
T& name();
T& address();
T& tel();
int includeName(T name);
void addBefore(T name,T address,T tel);
void addAfter(T name,T address,T tel);
void removeCurrent();
private:
Link<T>* currentLink;
List<T>& source;
Link<T>* previousLink;
};
template<class T> int ListIterator<T>::init(){
currentLink = source.p1stLink;
previousLink =0;
return operator !();
}
template<class T> ListIterator<T>::ListIterator(List<T>& refList):source(refList){
init();
}
template<class T> int ListIterator<T>::operator ++(int){
if(currentLink)
{
previousLink = currentLink;
currentLink = currentLink->pNextLink;
}
else
{
if(previousLink){
currentLink = previousLink->pNextLink;
}
else
{
currentLink = source.p1stLink;
}
}
return operator !();
}
template<class T> int ListIterator<T>::operator !(){
if(currentLink)
return 1;
else
{
if(previousLink)
return previousLink->pNextLink!=0;
else
return source.p1stLink!=0;
}
}
template<class T> T& ListIterator<T>::name(){
if(!currentLink)
throw 1;
return currentLink->name;
}
template<class T> T& ListIterator<T>::address(){
if(!currentLink)
throw 1;
return currentLink->address;
}
template<class T> T& ListIterator<T>::tel(){
if(!currentLink)
throw 1;
return currentLink->tel;
}
template<class T> void ListIterator<T>::addBefore(T name,T address,T tel){
if(previousLink){
previousLink=previousLink->insert(name,address,tel);
}
else{
source.add(name,address,tel);
previousLink = source.p1stLink;
}
}
template<class T> void ListIterator<T>::addAfter(T name,T address,T tel){
if(currentLink)
currentLink->insert(name,address,tel);
else
if(previousLink){
previousLink->insert(name,address,tel);
}
else
source.add(name,address,tel);
}
template<class T> void ListIterator<T>::removeCurrent(){
if(!currentLink)
throw 1;
if(previousLink)
previousLink->pNextLink=currentLink->pNextLink;
else
source.p1stLink = currentLink->pNextLink;
delete currentLink;
currentLink = 0;
}
template<class T> int ListIterator<T>::includeName(T rName){
for(init();currentLink;operator ++(1)){
if(currentLink->name==rName)
return 1;
}
return 0;
};
int main(){
try{
List<string> list;//表链,初始为空
ListIterator<string> li(list);//遍历器类,用list初始化
li.addAfter("搜狐广州分公司","广州天河北路183号大都会广场2307室","(8620)85115666
");//通过遍历器向表链(List)添加链类(Link)
li.addAfter("搜狐分公司","中环夏悫道12号美国银行中心16楼1605室","852 - 29 2278");
li.addAfter("奥特曼","M78星云","未知");
li.addAfter("我","宁波","137777777777");
li.addAfter("焦点房地产网","北京市海淀区中关村东路1号院搜狐网络大厦15层","010-62726770");
li.addAfter("新华网","京宣武门西大街127号大成大厦","(010)63070925");
string aName("奥特曼");//要查找的人的名字
//包含测试,如果包含则 将currentLink指向包含该值的链类(Link) 并返回1
//否则,currentLink指向0,返回0
if(li.includeName(aName))
cout<<"姓名:"<<aName<<endl
<<"地址:"<<li.address()<<endl//返回地址,可以复制
<<"电话:"<<li.tel();
else
cout<<"SORRY,查无此人";
}
catch(int err)
{
if(err=1)
cout<<"错误,当前节点为空";
}
getchar();
return 0;
}
List是表链 用于结合链类
Link是链类
ListIterator是遍历器 ,
遍历器有其他的功能没用出来
注释是有点少,
......
热心网友 时间:2022-06-17 15:15
花了不少时间写了下面程序,
希望对你有所帮助(有问题QQ 274222686)
#include<string>
#include<iostream>
using namespace std;
#include "malloc.h"
struct LNode{
char name[20];
char phone[15];
char ad[40];
struct LNode *next;
};
struct LNode *A;
void getnum() //输入
{
struct LNode *p;
p=(LNode *)malloc(sizeof(LNode));
cout<<"输入姓名(以 0 结束):";
cin>>p->name;
cout<<"输入电话(以 0 结束):";
cin>>p->phone;
cout<<"输入地址(以 0 结束):";
cin>>p->ad;
cout<<endl;
while(p->name[0]!='0')
{
p->next=A->next;
A->next=p;
p=(LNode *)malloc(sizeof(LNode));
cout<<"输入姓名(以 0 结束):";
cin>>p->name;
cout<<"输入电话(以 0 结束):";
cin>>p->phone;
cout<<"输入地址(以 0 结束):";
cin>>p->ad;
cout<<endl;
}
}
void found() //查找
{
struct LNode *p;
char na[20];int k=0;
cout<<endl<<"输入查找人的姓名:";
cin>>na;
for(p=A->next;p!=NULL;p=p->next)
if(strcmp(p->name,na)==0)
{cout<<"姓名:"<<p->name<<" "<<"电话:"<<p->phone<<" "<<"地址:"<<p->ad<<endl;k=1;}
if(k==0) cout<<"没有找到此人"<<endl;
}
void del() //删除
{
struct LNode *p,*q;
char na[20];
int k=0;
cout<<endl<<"输入删除人的姓名:";
cin>>na;
for(p=A->next,q=A;p!=NULL;q=p,p=p->next)
if(strcmp(p->name,na)==0)
{
q->next=p->next;free(p);p=q;cout<<"已删除"<<endl;k=1;
}
if(k==0) cout<<"没有"<<endl;
}
void display()
{
struct LNode *p;
for(p=A->next;p!=NULL;p=p->next)
cout<<"姓名:"<<p->name<<" "<<"电话:"<<p->phone<<" "<<"地址:"<<p->ad<<endl;
}
void main()
{
A=(LNode *)malloc(sizeof(LNode));
A->next=NULL;
cout<<"录入数据......"<<endl;
getnum();
int t=1;
while(t==1)
{
int ch;
cout<<endl<<" 1. 查找:";
cout<<endl<<" 2. 删除:";
cout<<endl<<" 3. 插入:";
cout<<endl<<" 4. 显示:";
cout<<endl<<" 0. 退出"<<endl;
cin>>ch;
switch(ch){
case 0:t=0;break; //退出
case 1:found();break;//查找
case 2:del();break;//删除
case 3:getnum();break;//插入
case 4:display();break;//显示
}
}
}
热心网友 时间:2022-06-17 15:15
#include<list>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
class Man
{
public:
string name;
string phone;
string address;
bool operator ==(const Man& man) const
{
return man.name==name;
}
};
typedef list<Man> ManList;
void input(ManList& mans)
{
Man man;
cout<<"姓名为“#”表示结束录入。"<<endl;
for(;true;)
{
cout<<endl<<"输入姓名:";
cin>>man.name;
cout<<endl<<"输入电话:";
cin>>man.phone;
cout<<endl<<"输入地址:";
cin>>man.address;
if(man.name=="#")
return;
mans.push_back(man);
}
}
void repMan(const Man& man)
{
cout<<"姓名:"<<man.name<<endl;
cout<<"电话:"<<man.phone<<endl;
cout<<"地址:"<<man.address<<endl;
}
void query(const ManList& mans)
{
Man man;
cout<<"姓名为“#”表示结束查询。"<<endl;
for(;true;)
{
cout<<endl<<"输入姓名:";
cin>>man.name;
if(man.name=="#")
return;
ManList::const_iterator it=find(mans.begin(),mans.end(),man);
cout<<"查询结果:"<<endl;
if(it!=mans.end())
repMan(*it);
else
cout<<"查无此人"<<endl;
}
}
int main(int argc, char* argv[])
{
ManList mans;
cout<<"录入数据..."<<endl;
input(mans);
cout<<"开始查询..."<<endl;
query(mans);
return 0;
}
热心网友 时间:2022-06-17 15:16
估计现编得花好几天。去csnd.net看看。建议最好直接去搜,比如去维普、万方等等,百度也可以试试。
做这些东西的很多,肯定有现成的
热心网友 时间:2022-06-17 15:16
不想写了......
这个貌似不难吧~
怎么都说不好弄呢?
我遭黑到了!!