窦智孔黄圣依:怎样用C++设计一个集合类,用来处理整型数!请程序高手帮帮忙贴程序!谢谢!

来源:百度文库 编辑:中科新闻网 时间:2024/04/29 10:38:30
请程序高手们帮帮忙吧!我正在苦恼做这个课程设计呢!如帮忙,小女不胜感激!

//BCB类库的
//文件ListSet.hpp
#ifndef LISTSET
#define LISTSET

#include <stdio.h>

typedef int bool;
#define true 1
#define false 0

template <class T>
class ListElement{
T datum;
ListElement * next;
ListElement( T const& _datum , ListElement * _next ): datum(_datum) , next(_next) { }
public:
T const& Datum( void ) const{ return datum; }
ListElement const * Next( ) const{ return next; }

friend LinkedList<T>;
};

template <class T>
class LinkedList{
ListElement<T> * head;
ListElement<T> * tail;
public:
LinkedList( ):head(0),tail(0){ }
~LinkedList() { Purge(); }

LinkedList( LinkedList const& );
LinkedList& operator = ( LinkedList const& );

ListElement<T> const * Head(void)const { return head; }
ListElement<T> const * Tail(void)const { return tail; }
bool IsEmpty() { return head == 0; }
T const& First(void) const;
T const& Last(void) const;

void Prepend( T const& );
void Append( T const& );
void Extract( T const& );

void Purge(void);

};

template <class T>
void LinkedList<T>::Purge(void)
{
while( head )
{
ListElement<T> * const tmp = head;
head = head->next;
delete tmp;
}
tail = 0;
}

template <class T>
LinkedList<T>::~LinkedList()
{
Purge();
}

template <class T>
T const& LinkedList<T>::First( void ) const
{
if( head == 0 )
// throw domain_error("list is empty");
printf("\nlist is empty");
return head->datum;
}

template <class T>
T const& LinkedList<T>::Last( void ) const
{
if( tail == 0 )
// throw domain_error("list is empty");
printf("\nlist is empty");
return tail->datum;
}

template <class T>
void LinkedList<T>::Prepend( T const& item )
{
ListElement<T> * const tmp = new ListElement<T> ( item , head );
if( head == 0 )
tail = tmp;
head = tmp;
}

template <class T>
void LinkedList<T>::Append( T const& item )
{
ListElement<T> * const tmp = new ListElement<T> ( item , 0 );
if( head == 0 )
head = tmp;
else
tail->next = tmp;
tail = tmp;
}

template <class T>
LinkedList<T>::LinkedList( LinkedList<T> const& LinkedList ):
head(0),tail(0)
{
ListElement<T> const * ptr;
for( ptr = LinkedList.head ; ptr != 0 ; ptr = ptr->next )
Append( ptr->datum );
}

template <class T>
LinkedList<T>& LinkedList<T>::operator = ( LinkedList<T> const& LinkedList )
{
if( &LinkedList != this )
{
Purge();
ListElement<T> const * ptr;
for( ptr = LinkedList.head ; ptr != 0 ; ptr = ptr->next )
Append( ptr->datum );
}
return *this;
}

template <class T>
void LinkedList<T>::Extract ( T const& item )
{
ListElement<T> * ptr = head;
ListElement<T> * prevPtr = 0;
while( ptr != 0 && ptr->datum != item )
{
prevPtr = ptr;
ptr = ptr->next;
}
if( ptr == 0 )
// throw invalid_argument("item not found");
printf("\nitem not found");
if( ptr == head )
head = ptr->next;
else
prevPtr->next = ptr->next;
if( ptr == tail )
tail = prevPtr;
delete ptr;
}

template <class T>
class Set : public LinkedList<T>{
LinkedList<T> list;
public:
Set():list() { }
Set( Set const& _set ):list( _set.list ) { }
~Set() { Purge(); }

int Has( T const& );
void operator << ( T const& );
void operator >> ( T const& );
Set& operator + ( Set& set );
Set& operator * ( Set& set );
};

template <class T>
int Set<T>::Has( T const& item )
{
ListElement<T> const * ptr = list.Head();
for( ; ptr != 0 ; ptr = ptr->Next() )
if( item == ptr->Datum() )
return true;
return false;
}

template <class T>
void Set<T>::operator << ( T const& item )
{
if( !Has( item ) )
list.Append( item );
}

template <class T>
void Set<T>::operator >> ( T const& item )
{
if( Has( item ) )
list.Extract( item );
}

template <class T>
Set<T>& Set<T>::operator + ( Set<T>& set )
{
Set<T> * _set = new Set<T>( set );
ListElement<T> const * ptr = list.Head();
for( ; ptr != 0 ; ptr = ptr->Next() )
*_set << ptr->Datum();
return *_set;
}

template <class T>
Set<T>& Set<T>::operator * ( Set<T>& set )
{
Set<T> * _set = new Set<T>;
ListElement<T> const * ptr = list.Head();
for( ; ptr != 0 ; ptr = ptr->Next() )
if( set.Has( ptr->Datum() ) )
*_set << ptr->Datum();
return *_set;
}

#endif

//文件TestSet.cpp
#include "listset.hpp"

void main()
{
Set<int> set1;
Set<int> set2;

set1 << 1;
set2 << 2;
if( set1.Has( 1 ) )
printf("\nSet1 has 1");
if( set1.Has( 1 ) )
printf("\nSet2 has 2");
Set<int> set3 =( set1 + set2 );
printf( "\nset3 = set1+set2");
if( set3.Has( 1 ) && set3.Has(2) )
printf("\nSet3 has 1 and 2");
Set<int> set4 = set1*set3;
printf("\nset4 = set1*set3");
if( set4.Has( 1 ) && !set4.Has( 2 ) )
printf("\nset has 1 but no 2");
Set<int> set5 = set2*set3;
printf("\nset5 = set2*set3");
if( set5.Has( 2 ) && !set5.Has( 1 ) )
printf("\nset has 2 but no 1");
}
//自己调试吧,我考紧试~~~
//有错误你再发问提示一下我吧~~~