定义元素类型为int、元素个数不受限制的集合类Set, 包括:

什么都不多说了,直接上代码= =、

感谢汪二的帮忙,不然一直都有问题>.<考试的时候能把汪二随身携带就好啦

但是呢,还是要自力更生啦

#include 
using namespace std;
struct IntSet{
	int *inset;
};
class Set{
private:
	int size;
	int *elements;
public:
	Set(){size = 0;elements = NULL;}
	void show();
	bool operator <=(Set &s);
	bool operator ==(Set &s);
	bool operator !=(Set &s);
	Set & operator |(Set &s);
	Set & operator &(Set &s);
	Set & operator -(Set &s);
	void operator +=(int x);
	void operator -=(int x);
	~Set(){if (elements!=NULL){
	    delete []elements;
	}
	}

};

void Set::show(){
	for (int i = 0;isize = size;
	set->elements = new int[set->size];
	for (int r= 0;relements[r] = elements[r];
	}
	for (int i = 0;isize++;
			set->elements = new int[set->size];
			for (int i1 = 0;i1size-1;i1++)
			{
				set->elements[i1]= temp[i1];
			}
		    set->elements[set->size-1]= s.elements[i] ;
		}
	}
	return *set;
}
//交集
Set & Set:: operator & (Set &s){
	Set *set = new Set;
	set->elements = NULL;
	set->size = 0;
	for (int i = 0;ielements;
				set->size++;
				set->elements = new int[set->size];
				for (int i1 = 0;i1size-1;i1++)
				{
					set->elements[i1]=temp[i1];
				}
				set->elements[set->size-1]=elements[j];
			}
		}
	}
	return *set;
}

//差集
Set & Set:: operator - (Set &s){
	Set *set = new Set;
	set->size = 0;
	set->elements = NULL;
	for (int i = 0;ielements;
			set->size++;
			set->elements = new int[set->size];
			for (int i1 = 0;i1size-1;i1++)
			{
				set->elements[i1] = temp[i1];
			}
			set->elements[set->size-1] = elements[i];
		}
	}
	return *set;
}

void Set::operator+=(int x){
	int flag = 0;
	for (int i =0;i


你可能感兴趣的:(C++)