⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 set.cpp

📁 Matrix code
💻 CPP
字号:
#include <iostream.h>
struct NODE{
	int data;
	NODE* next;
};

class Set{
	NODE* head;
	NODE* tail;
public:
	Set();
	Set(int data);
	Set(int min, int max);
	Set(const Set& other);
	~Set();
	int add( int element);
	int remove(int element);
	int reset();
	int is_member(int data) const;
	int is_empty()const;
	int length() const;
	Set& operator=(const Set& other);
        // 集合的并
	friend Set operator+(Set& first,Set& second);
	// 集合的差
	friend Set operator-(Set& first,Set& second);
	// 集合的交
	friend Set operator^(Set& first,Set& second);
	
	friend int operator==(Set& first,Set& second);
	friend int operator!=(Set& first,Set& second);
	
	friend ostream& operator<<(ostream& os, Set& set);
	friend istream& operator>>(istream& is, Set& set);
	
};

Set::Set()
{
	head=tail=0;   
}

Set::Set(int data)
{
	head=new NODE;
	head->data=data;
	head->next=0;
	tail=head;
}

Set::Set(int min,int max)
{
	head=tail=0;
	for(int i=min;i<=max;i++) add(i);
}

Set::Set(const Set& other)
{
	NODE*move;
	head=tail=0;
	move=other.head;
	while(move!=0)
	{
		add(move->data);
		move=move->next;
	}
}

Set::~Set()
{
	NODE* move;
	move=head;
	while(move!=0){
		head=move->next;
		delete move;
		move=head;
	}
}

int Set::add(int element)
{
	if(is_member(element)) 
		return 0;                 //集合的元素不能重复!
	NODE* entry;
	//创建新的结点存放待插入的整数element
	entry=new NODE;
	entry->data=element;
	entry->next=0;
	if(head==0)
		head=entry;              
	else tail->next=entry;       
	tail=entry;                  
	return 1;
}

int Set::remove(int element)
{
	NODE* move;
	NODE* prev;
	prev=0; 
	move=head;
	while(move!=0)
	{
		if(move->data==element)
			break;
		prev=move;
		move=move->next;
	}
	if(move==0)
		return 0;
	//请仔细分析下面的语句,确信它们已经处理了各种情况!
	if(move==tail)
		tail=prev;                
	if(move==head)
		head=head->next;       
	else 
		prev->next=move->next;
	delete move;
	return 1;
}

int Set::reset()
{
	NODE*move;
	move=head;
	while(move!=0)
	{
		head=move->next;
		delete move;
		move=head;
	}
	head=tail=0;
	return 1;
}

int Set::is_member(int data)const
{
	NODE* move;
	move=head;
    while (move!=NULL)
	{
	    if(move->data==data)
			return 1;
	    move=move->next;
	}
	return 0;
}

int Set::is_empty()const
{
	if(head==0)
		return 1;
	else 
		return 0;
}

int Set::length()const
{
	int len;
	NODE*move;
	move=head;  
	len=0;
	while(move!=0)
	{
		len=len=1;
		move=move->next;
	}
	return len;
}

Set& Set::operator=(const Set& other)
{
	NODE*move;
	if(&other==this)
		return *this;
	reset();
	move=other.head;
	while(move!=0)
	{
		add(move->data);
		move=move->next;
	}
	return*this;
}

Set operator+(Set& first,Set& second)
{
	NODE* move;
	Set result=first;
	move=second.head;
	while (move!=0)
	{
		result.add(move->data);
		move=move->next;
	}
	return result;
}

Set operator-(Set& first,Set& second)
{
	NODE* move;
	Set result=first;
	move=second.head;
	while(move!=0){
		result.remove(move->data);
		move=move->next;
	}
	return  result;
}

Set operator^(Set& first,Set& second)
{
	NODE* move;
	Set result;
	move=first.head;
	while(move!=0)
	{
		if( second.is_member(move->data) )
			result.add(move->data);
		move=move->next;
	}
	return result;
}

int operator ==(Set& first, Set& second)
{
	NODE* move;
	move=first.head;         
	while(move!=0)
	{
		if(!second.is_member(move->data))
			return 0;
		move=move->next;
	}
	move =second.head;       
	while(move!=0)
	{
		if(!first.is_member(move->data))
			return 0;
		move=move->next;
	}
	return 1;
}

int operator !=(Set& first, Set& second)
{
	if(first==second)
		return 0;
	else
		return 1;
}

ostream& operator<<(ostream& out, Set& set)
{
	NODE *move;
	out<<'{';
	move=set.head;
	while(move!=0)
	{
		out<<move->data;
		if(move!=set.tail)
			out<<",";
		move=move->next;
	}
	out<<'}';
	return out;
}

istream& operator>>(istream& in, Set& set)
{
	int size,count,data;
	set.reset();
	cout<<"Input the size of set";
	in>>size;
	count=0;
	while(count<size)
	{
		cout<<"No"<<(count+1)<<":";
		in>>data;
		set.add(data);
		count=count+1;
	}
	return in;
}
	
int main()
{
	Set set_one(14,24);
	Set set_two(20,30);
	cout<<set_one<<endl;
	cout<<set_two<<endl;
	cout<<(set_one+set_two)<<endl;
	cout<<(set_one-set_two)<<endl;
	cout<<(set_one^set_two)<<endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -