#include<iostream>
#include<cstdlib>
using namespace std;
const int maxlen=200;
/*##########################set类的声明#################################*/
class set
{
public:
set();
set(int *t,int s);
set(set& ab);
~set(){delete []array;}
void intsert(int n,int i);
void remove(int i);
void display();
set& intersection(set& s1);
set& together(set& s1);
set& different(set& s1);
set& operator +(set& s1);
set& operator -(set& s1);
set& operator *(set& s1);
istream& operator >>(istream&);
ostream& operator <<(ostream&);
int& operator[](int i );
private:
int *array;
int len;
};
/*******************默认构造函数定义**********************************/
set::set()
{
array=new int [maxlen];
if(!array)
{cout<<"there are out memory!";exit(1);}
len=0;
}
/*##################有两个构造函数的定义#############################*/
set::set(int *t,int s)
{
array=new int[maxlen];
if(!array)
{
cout<<"there are out of memory!";
exit(1);
}
for(int i=0;i<maxlen;i++)
*(array+i)=0;
len=s;
for(int j=0;j<s;j++)
*(array+j)=*(t+j);
}
/*###################挎贝构造函数的定义##############################*/
set::set(set& ab)
{
array=new int [ab.len];
if(!array)
{cout<<"there are out of memory!!";exit(1);}
len=ab.len;
for(int i=0;i<ab.len;i++)
*(array+i)=*(ab.array+i);
}
/*######################在i处插入值为n的函数的定义###################*/
void set::intsert(int n,int i)
{
if(i<0||i>len)
{cout<<"The position is wrong!!"<<endl;exit(1);}
else
{
int size=len-i;
for(int j=size;j>0;j--)
*(array+i+j)=*(array+i+j-1);
*(array+i)=n;
len=len+1;
}
}
/*#####################删除第i位的函数的定义########################*/
void set::remove(int i)
{
if(i<0||i>len-1){cout<<"The position is wrong!!";exit(1);}
else
{
int size=len-i;
for(int j=0;j<size;j++)
*(array+i+j)=*(array+i+j+1);
len=len-1;
}
}
/*###############输出#######################*/
void set::display()
{
for(int j=0;j<len;j++)
cout<<*(array+j)<<' ';
cout<<endl;
}
/*#################求交集###############*/
set& set::intersection(set& s1)
{
static set ab;
int i=0,j=0;int k=0;
for(j=0;j<s1.len;j++)
{
for(i=0;i<len;i++)
{
if(*(s1.array+j)==*(array+i))
{
*(ab.array+k)=*(s1.array+j);
k++;
}
}
}
cout<<"the two set's intersection is :"<<endl;
if(ab.len==0)
cout<<"there is none count in the set !"<<endl;
ab.display();
return ab;
}
set& set::together(set& s1)
{
static set ab;
int p=0;
for(int i=0;i<len;i++)
*(ab.array+i)=*(array+i);
for(int j=0;j<s1.len;j++)
for(int k=0;k<ab.len;k++)
if(*(ab.array+j)!=*(s1.array+k))
{
*(ab.array+ab.len+p)=*(s1.array+k);
p++;break;
}
cout<<"这两个数组的并集为:"<<endl;
if(ab.len==0)
cout<<"error!!没有元素在数组中请检查你的输入:"<<endl;
ab.display();
return ab;
}
set& set::different(set& s1)
{
static set ab;
for(int k=0;k<len;k++)
*(ab.array+k)=*(array+k);
for(int i=0;i<s1.len;i++)
for(int j=0;j<ab.len;j++)
if(*(ab.array+j)==*(s1.array+i))
*(ab.array+j)=*(ab.array+(j+1));
cout<<"两个数组的差集为:"<<endl;
if(len==0)
cout<<"error!没有元素在数组中请检查你的输入:"<<endl;
ab.display();
return ab;
}
set& set::operator +(set& s1)
{
static set ab;
ab.together(s1);
return ab;
}
set& set::operator -(set& s1)
{
static set ab;
ab.different(s1);
return ab;
}
set& set::operator *(set& s1)
{
static set ab;
ab.intersection(s1);
return ab;
}
istream& set::operator>>(istream& in)
{
static set s1;
for(int i=0;i<s1.len;i++)
return in>>s1[i];
}
ostream& set::operator<<(ostream& out)
{
set s1;
for(int i=0;i<s1.len;i++)
return out<<s1[i];
}
int& set::operator [](int i)
{
return array[i];
}
int main()
{
set C1;
int a1[7]={1,2,3,4,5,6,7};
int b1[7]={9,8,7,4,5,10,20};
set A1(a1,7);
set B1(b1,7);
cout<<"A1集合的元素为:"<<endl;
A1.display();
cout<<"B1集合的元素为:"<<endl;
B1.display();
A1.intsert(123,3);
cout<<"向A1中的第四个位置添加123后的A1为:"<<endl;
A1.display();
A1.remove(1);
cout<<"把A1中的第二个元素删除后A1为:"<<endl;
A1.display();
A1.intersection(B1);
C1=A1*B1;
C1.display();
A1.together(B1);
C1=A1+B1;
C1.display();
A1.different(B1);
C1=A1-B1;
C1.display();
return 0;
}
调试无错误,但运行时候会出现内存错误,改了好几次,没该出来,谁帮忙看看
谢谢!