65,210
社区成员
发帖
与我相关
我的任务
分享# 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;
}
}