// 很久没写C++ 程序了,写的不对的地方请指出
#include <iostream>
#include <cstdio>
using namespace std;
class Set
{
private:
int * element;
int n;
public:
Set(){ element = NULL; n = 0;}
Set(int * e, int size)
{
n = size;
element = new int[n];
memcpy(element, e, n*sizeof(int));
}
void creat(int * e, int size)
{
if(element)
delete [] element;
n = size;
element = new int[n];
memcpy(element, e, n*sizeof(int));
}
void insertElement(int e)
{
bool no_same = true;
int * temp = NULL;
if(n == 0)
{
n = 1;
element = new int;
*element = e;
}
else
{
for(int i = 0; i<n; i++)
{
if(element[i] != e)
continue;
else
{
no_same = false;
break;
}
}
if(no_same)
{
n++;
temp = new int[n];
//memset(temp, 0, n*sizeof(int));
memcpy(temp, element, (n-1)*sizeof(int));
temp[n-1] = e;
delete [] element;
element = new int[n];
memcpy(element, temp, n*sizeof(int));
delete [] temp;
}
}
}
void eraseElement(int e)
{
bool no_same = true;
int * temp = NULL;
int pos = 0;
for(int i = 0; i<n; i++)
{
if(element[i] != e)
continue;
else
{
no_same = false;
pos = i;
break;
}
}
if(!no_same)
{
temp = new int[--n];
memcpy(temp, element, pos*sizeof(int));
memcpy(temp+pos, element+pos+1, (n-pos)*sizeof(int));
delete [] element;
element = new int[n];
memcpy(element, temp, n*sizeof(int));
delete [] temp;
}
}
void showElement()
{
for(int i = 0; i<n; i++)
{
cout<<element[i]<<" ";
//if((i+1)%6 == 0)
// cout<<endl;
}
}
int * getInfo(int & nOfElement)
{
nOfElement = n;
return element;
}
Set(Set & s)
{
if(element)
{
delete [] element;
n = 0;
}
int * e_s = NULL, n_s = 0;
e_s = s.getInfo(n_s);
element = new int[n_s];
n = n_s;
memcpy(element, e_s, n*sizeof(int));
}
void jiaoji(Set & sx, Set & jiaoji)
{
int * e_sx = NULL, n_sx = 0;
e_sx = sx.getInfo(n_sx);
int n_min = n<n_sx?n:n_sx;
int * temp = new int[n_min];
int n_temp = 0;
for(int i = 0; i<n_sx; i++)
{
for(int j = 0; j<n; j++)
{
if(element[j] == e_sx[i])
{
temp[n_temp] = element[j];
if(n_temp<n_min)
n_temp++;
}
}
}
jiaoji.creat(temp, n_temp);
delete [] temp;
}
void bingji(Set & sx, Set & bingji)
{
int * e_sx = NULL, n_sx = 0;
e_sx = sx.getInfo(n_sx);
int n_max = n + n_sx; //n>n_sx?n:n_sx;
int * temp = new int[n_max];
memcpy(temp, element, n*sizeof(int));
bool nicht_vorkommen = true;
int n_temp = n;
for(int i = 0; i<n_sx; i++)
{
for(int j = 0; j<n; j++)
{
if(e_sx[i] != element[j])
continue;
else
{
nicht_vorkommen = false;
break;
}
}
if(nicht_vorkommen)
{
temp[n_temp] = e_sx[i];
n_temp++;
}
nicht_vorkommen = true;
}
bingji.creat(temp, n_temp);
delete [] temp;
}
void caji(Set & sx, Set & caji)
{
int * e_sx = NULL, n_sx = 0;
caji.creat(element, n);
e_sx = sx.getInfo(n_sx);
for(int i = 0; i<n_sx; i++)
{
for(int j = 0; j<n; j++)
{
if(e_sx[i] == element[j])
caji.eraseElement(e_sx[i]);
}
}
}
~Set()
{
if(element)
{
delete [] element;
n = 0;
}
}
};
int main()
{
int source1[6] = {1,2,3,4,5,6};
int source2[6] = {4,5,6,7,8,9};
Set s1(source1, 6);
Set s2;
Set jiaoji, bingji, caji;
s2.creat(source2, 6);
s1.insertElement(8);
s2.insertElement(66);
s1.eraseElement(2);
s1.eraseElement(3);
s1.jiaoji(s2, jiaoji);
s1.bingji(s2, bingji);
s2.caji(s1, caji);
cout<<"The element of s1: ";
s1.showElement();
cout<<endl;
//------------------------------
cout<<"The element of s2: ";
s2.showElement();
cout<<endl;
//------------------------------
cout<<"The element of jiaoji: ";
jiaoji.showElement();
cout<<endl;
//------------------------------
cout<<"The element of bingji: ";
bingji.showElement();
cout<<endl;
//------------------------------
cout<<"The element of caji: ";
caji.showElement();
cout<<endl;
system("pause");
return 0;
}