一个异质链表的实现 请大家帮我看一下

lzokwlz 2008-11-19 10:25:06
# include <iostream>
#include<string>

using namespace std;

//类和继承类

class people //基类
{
private:
int number,age;
string name;
public:
int getnum(){return number;}
people(int num, string s,int i):number(num),name(s),age(i){}
virtual void addnode()
{int num,i;
string s;
cout<<"please input the number of the person:\n";
cin>>num;
cout<<"please input the name of the person:\n";
cin>>s;
cout<<"please input the age of the person:\n";
cin>>i;
}
virtual void print()
{

cout << "\nnumber:" <<number
<< "\nName:" << name
<< "\nage:"<< age ;

}

};


class doctor: public people //docter中增加部门
{int d;
public:
doctor(int num,string s,int i, string d):people(num,s,i),department(d){}

void addnode()
{
people::addnode();
cout<<"please input the department of the doctor:\n";
cin>>d;
}

void print()
{
people::print();
cout<<"\ndepartment: "<<department<<endl;
}



private:
string department;
};



class student: public people //student中增加分数
{int sc;
public:
student(int num,string s,int i,int sc):people(num,s,i),scores(sc){}
void addnode()
{
people::addnode();
cout<<"please input the scores of the student:\n";
cin>>sc;
scores=sc;
}
void print()
{
people::print();
cout<<"\nscores: "<<scores<<endl;
}
private:
int scores;
};


class worker: public people //worker中增加工资
{int sa;
public:
worker(int num,string s, int i,int sa):people(num,s,i){}
void addnode()
{
people::addnode();
cout<<"please input the salary of the worker:\n";
cin>>sa;
salary=sa;
}
void print()
{
people::print();
cout<<"\nsalary: "<<salary<<endl;
}
private:
int salary;
};


class List
{
public:
List(people* q):po(q),pnext(0){}
people* po; //指向对象
List* pnext; //指向结点
};



class Listoperate
{
private:
List * pfirst; //链首结点指针
public:
Listoperate():pfirst(0){}
//void Creat(people*); //建立初始节点
void Delete(int); //删除结点
void Insert(people*); //插入结点
void Find(int); //寻找结点
void Pri(); //输出链表
};

void Listoperate::Creat(people* pn) //建立初始节点
{

List* n= new List(pn);

if (!pfirst) pfirst = n;

else

{

n->pnext = pfirst;

pfirst = n;

}
}



void Listoperate::Insert(people* pn) //插入结点

{ //int x,z,k;
char m;
string y,i;
doctor* q;
student* p;
worker* w;


cout<<"\n\n1.doctor 2.worker 3.student "<<endl;
cout<<"\n\nplease input the kind of the people that you want to insert:"<<endl;
cin>>m;
if(m=='1')
{
pn=p;
pn->addnode();
}

else
if(m=='2')
{
pn=w;
pn->addnode();
}
else
{
pn=q;
pn->addnode();
}


doctor doc();
worker wor();
student stu();
//Listop.Insert((people*)&doc);
//Listop.Insert((people*)&wor);
//Listop.Insert((people*)&stu);

List* n= new List(pn);

if (!pfirst) pfirst = n;

else

{

n->pnext = pfirst;

pfirst = n;

}
}



void Listoperate::Delete(int nu) //删除结点t
{
List* p=pfirst;
if(pfirst->po->getnum()==nu)
{
pfirst=p->pnext;
delete p;

}
else
{
for(List* p=pfirst; p->pnext;p=p->pnext)
if(p->pnext->po->getnum()==nu)
{
List*q=p->pnext;
p->pnext=p->pnext->pnext;
break;
delete q;
}
}


}



void Listoperate:: Find(int nu) //寻找编号为nu的结点并输出
{

for(List* p=pfirst;p;p=p->pnext)

if(p->po->getnum()==nu)
{
p->po->print();
}
}



void Listoperate:: Pri() //输出链表
{
List*p=pfirst;
while(p)
{
p->po->print();
p=p->pnext;
}
}



int main()
{
int n,d;
char g;
people* pn;
doctor doc(1, "张三",28,"内科");
student stu(2,"李四", 21,90);
worker wor(3,"王五",40,5000);
Listoperate Listop;
Listop.Creat((people*)&wor);
Listop.Creat((people*)&stu);
Listop.Creat((people*)&doc);

cout <<"The people list:" <<endl;
Listop.Pri();
//cout <<"Do you want to add some information?('Y'/'N')" <<endl;
//cin>>g;
//if(g=='Y')

Listop.Insert(pn);
Listop.Pri();

cout <<"The new people list:" <<endl;
Listop.Pri();
cout<<"\n\nplease input the number of the people that you Seek:"<<endl;
cin>>n;
cout<<"\n\nfind the people:"<<endl;
Listop.Find(n);
cout<<"\n\nplease input the number of the people that you want to remove:"<<
endl;
cin>>d;
cout <<"\n\nAfter Test Remove function:" <<endl;
Listop.Delete(d);
Listop.Pri();
system("pause");
}



运行时总提示出错,应该是Insert()这个函数错了。但我不知道怎么该。哪位能帮我看看,谢谢啦!










...全文
82 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-11-19
  • 打赏
  • 举报
回复
# include <iostream> 
#include <string>

using namespace std;

//类和继承类

class people //基类
{
private:
int number,age;
string name;
public:
int getnum(){return number;}
people(int num, string s,int i):number(num),name(s),age(i){}
virtual void addnode()
{int num,i;
string s;
cout <<"please input the number of the person:\n";
cin>>num;
cout <<"please input the name of the person:\n";
cin>>s;
cout <<"please input the age of the person:\n";
cin>>i;
}
virtual void print()
{

cout << "\nnumber:" <<number
<< "\nName:" << name
<< "\nage:" << age ;

}

};


class doctor: public people //docter中增加部门
{int d;
public:
doctor(int num,string s,int i, string d):people(num,s,i),department(d){}

void addnode()
{
people::addnode();
cout <<"please input the department of the doctor:\n";
cin>>d;
}

void print()
{
people::print();
cout <<"\ndepartment: " <<department <<endl;
}



private:
string department;
};


class student: public people //student中增加分数
{int sc;
public:
student(int num,string s,int i,int sc):people(num,s,i),scores(sc){}
void addnode()
{
people::addnode();
cout <<"please input the scores of the student:\n";
cin>>sc;
scores=sc;
}
void print()
{
people::print();
cout <<"\nscores: " <<scores <<endl;
}
private:
int scores;
};


class worker: public people //worker中增加工资
{int sa;
public:
worker(int num,string s, int i,int sa):people(num,s,i){}
void addnode()
{
people::addnode();
cout <<"please input the salary of the worker:\n";
cin>>sa;
salary=sa;
}
void print()
{
people::print();
cout <<"\nsalary: " <<salary <<endl;
}
private:
int salary;
};


class List
{
public:
List(people* q):po(q),pnext(0){}
people* po; //指向对象
List* pnext; //指向结点
};



class Listoperate
{
private:
List * pfirst; //链首结点指针
public:
Listoperate():pfirst(0){}
void Creat(people*); //建立初始节点
void Delete(int); //删除结点
void Insert(people*); //插入结点
void Find(int); //寻找结点
void Pri(); //输出链表
};

void Listoperate::Creat(people* pn) //建立初始节点
{

List* n= new List(pn);

if (!pfirst) pfirst = n;

else

{

n->pnext = pfirst;

pfirst = n;

}
}



void Listoperate::Insert(people* pn) //插入结点

{ //int x,z,k;
char m;
string y,i;
doctor* q;
student* p;
worker* w;


cout <<"\n\n1.doctor 2.worker 3.student " <<endl;
cout <<"\n\nplease input the kind of the people that you want to insert:" <<endl;
cin>>m;
if(m=='1')
{
pn=p;
pn->addnode();
}

else
if(m=='2')
{
pn=w;
pn->addnode();
}
else
{
pn=q;
pn->addnode();
}


doctor doc();
worker wor();
student stu();
//Listop.Insert((people*)&doc);
//Listop.Insert((people*)&wor);
//Listop.Insert((people*)&stu);

List* n= new List(pn);

if (!pfirst) pfirst = n;

else

{

n->pnext = pfirst;

pfirst = n;

}
}



void Listoperate::Delete(int nu) //删除结点t
{
List* p=pfirst;
if(pfirst->po->getnum()==nu)
{
pfirst=p->pnext;
delete p;

}
else
{
for(List* p=pfirst; p->pnext;p=p->pnext)
if(p->pnext->po->getnum()==nu)
{
List*q=p->pnext;
p->pnext=p->pnext->pnext;
break;
delete q;
}
}


}



void Listoperate:: Find(int nu) //寻找编号为nu的结点并输出
{

for(List* p=pfirst;p;p=p->pnext)

if(p->po->getnum()==nu)
{
p->po->print();
}
}



void Listoperate:: Pri() //输出链表
{
List*p=pfirst;
while(p)
{
p->po->print();
p=p->pnext;
}
}



int main()
{
int n,d;
char g;
people* pn;
doctor doc(1, "张三",28,"内科");
student stu(2,"李四", 21,90);
worker wor(3,"王五",40,5000);
Listoperate Listop;
Listop.Creat((people*)&wor);
Listop.Creat((people*)&stu);
Listop.Creat((people*)&doc);

cout <<"The people list:" <<endl;
Listop.Pri();
//cout <<"Do you want to add some information?('Y'/'N')" <<endl;
//cin>>g;
//if(g=='Y')

Listop.Insert(pn);
Listop.Pri();

cout <<"The new people list:" <<endl;
Listop.Pri();
cout <<"\n\nplease input the number of the people that you Seek:" <<endl;
cin>>n;
cout <<"\n\nfind the people:" <<endl;
Listop.Find(n);
cout <<"\n\nplease input the number of the people that you want to remove:" << endl;
cin>>d;
cout <<"\n\nAfter Test Remove function:" <<endl;
Listop.Delete(d);
Listop.Pri();
system("pause");
}
===============================================


void Listoperate::Insert(people* pn) //插入结点

{ //int x,z,k;
char m;
string y,i;
doctor* q;
student* p;
worker* w;


cout <<"\n\n1.doctor 2.worker 3.student " <<endl;
cout <<"\n\nplease input the kind of the people that you want to insert:" <<endl;
cin>>m;
if(m=='1')
{
pn=p; //这里,假如我进来了,那么这里的p是NULL啊,下面你又想调用NULL的成员函数,哪儿行!!!
pn->addnode();
}

else
if(m=='2')
{
pn=w;
pn->addnode();
}
else
{
pn=q;
pn->addnode();
}


doctor doc();
worker wor();
student stu();
//Listop.Insert((people*)&doc);
//Listop.Insert((people*)&wor);
//Listop.Insert((people*)&stu);

List* n= new List(pn);

if (!pfirst) pfirst = n;

else

{

n->pnext = pfirst;

pfirst = n;

}
}








Longinc 2008-11-19
  • 打赏
  • 举报
回复
Up先
内容概要:本文针对无刷直流电机驱动的电子机械制动(EMB)执行器,建立了考虑Stribeck摩擦特性的非线性耦合动力学模型,并在Simulink环境中完成了系统级仿真分析。研究综合集成了电机动力学、齿轮传动机构与制动执行机构的动力学特性,构建了高保真的机电一体化系统模型。重点引入Stribeck摩擦模型以精确描述低速工况下执行器内部存在的静摩擦、粘滞摩擦与库仑摩擦之间的过渡行为,有效提升了系统在启停、反向运动等瞬态过程中的动态响应仿真精度。通过多工况仿真验证了模型的有效性,能够准确反映摩擦引起的爬行、滞后与定位误差等非线性现象,为EMB系统的高性能控制算法设计(如摩擦补偿、滑模控制)与结构优化提供了高可信度的仿真平台。; 适合人群:从事汽车电子制动系统、电机驱动控制、机电系统建模与仿真研究的研究生、科研人员及工程技术人员,需具备扎实的机械动力学、自动控制理论基础和MATLAB/Simulink仿真能力。; 使用场景及目标:①用于高精度电子机械制动系统的设计验证与性能预测;②为消除摩擦非线性影响的先进控制策略(如自适应控制、智能控制)提供精确的被控对象模型;③深入探究Stribeck摩擦等非线性因素对系统动态性能(如响应延迟、稳态误差)的作用机理; 阅读建议:读者应结合提供的Simulink模型文件,深入剖析Stribeck摩擦模块的数学实现与参数辨识方法,建议通过改变输入指令(如阶跃、正弦)和负载条件进行对比仿真,以直观理解非线性摩擦对系统动态特性的影响。

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧