用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

c++ 利用类的包含来实现代码重用 链表

2012-09-02 作者: 神马举报

[c++]代码库

#include <iostream>
using namespace std;
class Date
{
public:
	Date() :date ( 1 ) {}
	Date ( int number ) :date ( number ) {}
	virtual ~Date() {};
	int GetDate() const {return date;}
	virtual void print() const =0;
private:
	int date;
};
class Book:public Date
{
public:
	Book() :Price ( 94 ) {}
	Book ( float Price,int number );
	virtual void print() const
	{
		cout<<"图书编号为:"<<Book::GetDate() <<endl;
		cout<<"图书价格为:"<<Price<<endl;
	}
private:
	float Price;
};
Book::Book ( float price,int number ) :Price ( price ),Date ( number ) {}
class medica:public Date
{
public :
	medica() :Price ( 1 ) {};
	medica ( float Price,int number );
	virtual void print() const
	{
		cout<<"药品编号为:"<<medica::GetDate() <<endl;
		cout<<"药品价格为:"<<Price<<endl;
	}
private:
	float Price;
};
medica::medica ( float price,int number ) :Price ( price ),Date ( number ) {}
class Node
{
public:
	Node ( Date* );
	~Node();
	void setnext ( Node*node ) {itsnext=node;}
	Node*getnext() const;
	Date*getDate() const;
private:
	Date*itsDate;
	Node*itsnext;
};
Node::Node ( Date*pDate ) :itsDate ( pDate ),itsnext ( 0 ) {}
Node::~Node()
{
	delete itsDate;
	itsDate=0;
	delete itsnext;
	itsnext=0;
}
Node*Node::getnext() const
{
	return itsnext;
}
Date*Node::getDate() const
{
	if ( itsDate )
		return itsDate;
	else
		return NULL;
}
class List
{
public:
	List();
	~List();
	Date*List::find ( int number ) const;
	Date*find ( int &increase,int number ) const;
	int getcount() const {return count;}
	Date*getfirst() const;
	void insert ( Date* );
	void repeat() const;
	Date*operator[] ( int ) const;
private:
	Node*head;
	int count;
};
List::List() :head ( 0 ),count ( 0 ) {}
List::~List()
{
	delete head;
}
Date*List::getfirst() const
{
	if ( head )
		return head->getDate();
	else
		return NULL;
}
Date*List::operator[] ( int offset ) const
{
	Node*pn=head;
	if ( !head )
		return NULL;
	if ( offset>count )
		return NULL;
	for ( int i=0; i<offset; i++ )
		pn=pn->getnext();
	return pn->getDate();
}
Date*List::find ( int number ) const
{
	Node*pn=0;
	for ( pn=head; pn!=NULL; pn=pn->getnext() )
	{
		if ( pn->getDate()->GetDate() ==number )
			break;
	}
	if ( pn==NULL )
		return NULL;
	else
		return pn->getDate();
}
Date*List::find ( int &increase,int number ) const
{
	Node*pn=0;
	for ( pn=head,increase=0; pn!=NULL; pn=pn->getnext(),increase++ )
	{
		if ( pn->getDate()->GetDate() ==number )
			break;
	}
	if ( pn==NULL )
		return NULL;
	else
		return pn->getDate();
}
void List::repeat() const
{
	if ( !head )
		return;
	Node*pn=head;
	do
		pn->getDate()->print();
	while ( pn=pn->getnext() );
}
void List::insert ( Date*pDate )
{
	Node*pn=new Node ( pDate );
	Node*pNow=head;
	Node*pNext=0;
	int New=pDate->GetDate();
	int next=0;
	count++;
	if ( !head )
	{
		head=pn;
		return;
	}
	if ( head->getDate()->GetDate() >New )
	{
		pn->setnext ( head );
		head=pn;
		return;
	}
	for ( ;; )
	{
		if ( !pNow->getnext() )
		{
			pNow->setnext ( pn );
			return;
		}
		pNext=pNow->getnext();
		next=pNext->getDate()->GetDate();
		if ( next>New )
		{
			pNow->setnext ( pn );
			pn->setnext ( pNext );
			return;
		}
		pNow=pNext;
	}
}
class Repair
{
public:
	void insert ( Date* );
	void PrintAll() {ll.repeat();}

private:
	Node*head;
	List ll;
};
void Repair::insert ( Date*newdate )
{
	int num=newdate->GetDate();
	int place=0;
	if ( !ll.find ( place,num ) )
		ll.insert ( newdate );
	else
	{
		cout<<"您输入的编号"<<num<<"与链表中";
		switch ( place )
		{
		case 0:
			cout<<"第"<<place+1;
			break;
		default:
			cout<<"第"<<place+1;
		}
		cout<<"个编号重复\n";

	}
}
int main()
{
	Repair pl;
	Date*pDate=0;
	int number;
	float value;
	int choice;
	while ( 1 )
	{
		cout<<"(0)退出(1)图书(2)药品:";
		cin>>choice;
		if ( !choice )
			break;
		cout<<"请输入编号:";
		cin>>number;
		if ( choice==1 )
		{
			cout<<"请输入图书价格:";
			cin>>value;
			pDate=new Book ( value,number );
		}
		else
		{
			cout<<"请输入药品价格:";
			cin>>value;
			pDate=new medica ( value,number );
		}
		pl.insert ( pDate );
	}
	pl.PrintAll();
	return 0;
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...