# Include <iostream>
# Include <stdlib. h>
# Include <memory. h>
Using namespace STD;
Class Set
{
Friend void insertsort (int * array1, int Len );
Friend void mergeplus (int * array1, const int * ARRA, const int * arrb,
Int Lena, int lenb, int * ilen );
Friend void mergecut (int * array1, const int * ARRA, const int * arrb,
Int Lena, int lenb, int * ilen );
Friend void mergecross (int * array1, const int * ARRA, const int * arrb,
Int Lena, int lenb, int * ilen );
// Friend set operator + (const set & A, const set & B );
Public:
Set ();
Set (INT s [], int N );
Set (const set & ohter); // copy Structure
Int find (int x) const; // determines whether X is in the collection.
Set & operator = (const set & other); // value assignment operator overload
Set operator + (const set & other); // the union of the Set
Set operator-(const set & other); // set difference set
Set operator * (const set & other); // intersection of the Set
Void disp ();
~ Set ();
PRIVATE:
Int * ELEM; // the pointer that stores the set element.
Int count; // number of elements in the Set
};
Void insertsort (int * array1, int Len) // Insert the sorting directly.
{
Int I, J;
Int curkey;
For (I = 1; I <Len; ++ I)
{
Curkey = array1 [I];
J = I-1;
While (curkey <array1 [J] & J> = 0)
{
Array1 [J + 1] = array1 [J];
-- J;
}
Array1 [J + 1] = curkey;
}
}
Void mergeplus (int * array1, const int * ARRA, const int * arrb, int Lena, int lenb, int * ilen) // merge and sort
{
Static int IA = 0, IB = 0;
Static int flag = 1;
If (Lena <= 0 & lenb <= 0)
{
Flag = 0;
Return;
}
If (Lena <= 0 & lenb> 0)
{
Array1 [(* ilen) ++] = arrb [IB ++];
Mergeplus (array1, ARRA, arrb, Lena, lenB-1, ilen );
}
If (Lena> 0 & lenb <= 0)
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
Mergeplus (array1, ARRA, arrb, Lena-1, lenb, ilen );
}
If (flag & ARRA [Ia] <arrb [IB])
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
Mergeplus (array1, ARRA, arrb, Lena-1, lenb, ilen );
}
Else if (flag & ARRA [Ia] = arrb [IB])
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
IB ++;
Mergeplus (array1, ARRA, arrb, Lena-1, lenb-1, ilen );
}
Else if (FLAG)
{
Array1 [(* ilen) ++] = arrb [IB ++];
Mergeplus (array1, ARRA, arrb, Lena, lenb-1, ilen );
}
}
Void mergecut (int * array1, const int * ARRA, const int * arrb, int Lena, int lenb, int * ilen) // merge sort
{
Static int IA = 0, IB = 0;
Static int flag = 1;
If (Lena <= 0 & lenb <= 0)
{
Flag = 0;
Return;
}
If (Lena <= 0 & lenb> 0)
{
Array1 [(* ilen) ++] = arrb [IB ++];
Mergecut (array1, ARRA, arrb, Lena, lenB-1, ilen );
}
If (Lena> 0 & lenb <= 0)
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
Mergecut (array1, ARRA, arrb, Lena-1, lenb, ilen );
}
If (flag & ARRA [Ia] <arrb [IB])
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
Mergecut (array1, ARRA, arrb, Lena-1, lenb, ilen );
}
Else if (flag & ARRA [Ia] = arrb [IB])
{
IA ++;
IB ++;
Mergecut (array1, ARRA, arrb, Lena-1, lenb-1, ilen );
}
Else if (FLAG)
{
Array1 [(* ilen) ++] = arrb [IB ++];
Mergecut (array1, ARRA, arrb, Lena, lenb-1, ilen );
}
}
Void mergecross (int * array1, const int * ARRA, const int * arrb, int Lena, int lenb, int * ilen) // merge sort
{
Static int IA = 0, IB = 0;
Static int flag = 1;
If (Lena <= 0 & lenb <= 0)
{
Flag = 0;
Return;
}
If (flag & ARRA [Ia] <arrb [IB])
{
IA ++;
Mergecross (array1, ARRA, arrb, Lena-1, lenb, ilen );
}
Else if (flag & ARRA [Ia] = arrb [IB])
{
Array1 [(* ilen) ++] = ARRA [Ia ++];
IB ++;
Mergecross (array1, ARRA, arrb, Lena-1, lenb-1, ilen );
}
Else if (FLAG)
{
IB ++;
Mergecross (array1, ARRA, arrb, Lena, lenb-1, ilen );
}
}
Set: Set ()
{
ELEM = NULL;
Count = 0;
}
Set ::~ Set ()
{
Delete [] ELEM;
ELEM = NULL;
}
Set: Set (INT s [], int N)
{
ELEM = new int [N];
If (ELEM = NULL)
{
Cout <"memory assign failure !! "<Endl;
Exit (1 );
}
For (INT I = 0; I <n; ++ I)
{
ELEM [I] = s [I];
}
Count = N;
}
Set: Set (const set & other)
{
If (other. ELEM = NULL)
{
Cout <"other object is empty !~! "<Endl;
Exit (1 );
}
Else
{
ELEM = new int [other. Count];
If (ELEM = NULL)
{
Cout <"memory assign failure !! "<Endl;
Exit (1 );
}
For (INT I = 0; I <other. Count; ++ I)
{
ELEM [I] = Other. ELEM [I];
}
Count = Other. count;
}
}
Set & set: Operator = (const set & other)
{
If (this = & other)
{
Return * this;
}
Delete [] ELEM;
ELEM = new int [other. Count];
If (ELEM = NULL)
{
Cout <"memory assign failure !! "<Endl;
Exit (1 );
}
For (Int J = 0; j <other. Count; ++ J)
{
ELEM [J] = Other. ELEM [J];
}
Count = Other. count;
Return * this;
}
Int set: Find (int x) const
{
If (ELEM = NULL)
{
Cout <"the set now is empty !! /N "<Endl;
Exit (1 );
}
Else
{
For (INT I = 0; I <count; ++ I)
{
If (x = ELEM [I])
{
Return 1;
}
}
Return 0;
}
}
/*
Set operator + (const set & A, const set & B)
{
If (A. ELEM = NULL & B. ELEM! = NULL)
{
Return set (B. ELEM, B. Count );
}
Else if (B. ELEM = NULL)
{
Return;
}
Else
{
Int length = 0;
Insertsort (A. ELEM, A. Count );
Insertsort (B. ELEM, B. Count );
Int * array1 = new int [A. Count + B. Count];
If (array1 = NULL)
{
Cout <"memory assign failure ~~ "<Endl;
Exit (1 );
}
Mergeplus (array1, A. ELEM, B. ELEM, A. count,
B. Count, & length );
Set temp (array1, length); // construct a temporary object
// Temp. Count = length;
// Temp. ELEM = new int [length];
// Memcpy (temp. ELEM, array1, length * sizeof (* array1 ));
Delete [] array1;
Array1 = NULL;
Return temp;
}
}
*/
Set set: Operator + (const set & other)
{
If (this = NULL & other. ELEM! = NULL)
{
Return set (other. ELEM, other. Count );
}
Else if (other. ELEM = NULL)
{
Return * this;
}
Else if (this = & Other) // judge whether two sets are equal
{
Return * this;
}
Else
{
Int length = 0;
Insertsort (this-> ELEM, count );
Insertsort (other. ELEM, other. Count );
Int * array1 = new int [This-> count + other. Count];
If (array1 = NULL)
{
Cout <"memory assign failure ~~ "<Endl;
Exit (1 );
}
Mergeplus (array1, this-> ELEM, other. ELEM, this-> count,
Other. Count, & length );
Set temp (array1, length );
// Temp. Count = length; // construct a temporary object
// Temp. ELEM = new int [length];
// Memcpy (temp. ELEM, array1, length * sizeof (* array1 ));
Delete [] array1;
Array1 = NULL;
Return temp;
}
}
Set set: Operator-(const set & other)
{
If (this = NULL & other. ELEM! = NULL)
{
Return set (other. ELEM, other. Count );
}
Else if (other. ELEM = NULL)
{
Return * this;
}
Else if (this = & Other) // judge whether two sets are equal
{
Return * this;
}
Else
{
Int length = 0;
Insertsort (this-> ELEM, count );
Insertsort (other. ELEM, other. Count );
Int * array1 = new int [This-> count + other. Count];
If (array1 = NULL)
{
Cout <"memory assign failure ~~ "<Endl;
Exit (1 );
}
Mergecut (array1, this-> ELEM, other. ELEM, this-> count,
Other. Count, & length );
Set temp (array1, length );
Delete [] array1;
Array1 = NULL;
Return temp;
}
}
Set set: Operator * (const set & other)
{
If (this = NULL & other. ELEM! = NULL)
{
Return set (other. ELEM, other. Count );
}
Else if (other. ELEM = NULL)
{
Return * this;
}
Else if (this = & Other) // judge whether two sets are equal
{
Return * this;
}
Else
{
Int length = 0;
Insertsort (this-> ELEM, count );
Insertsort (other. ELEM, other. Count );
Int * array1 = new int [This-> count + other. Count];
If (array1 = NULL)
{
Cout <"memory assign failure ~~ "<Endl;
Exit (1 );
}
Mergecross (array1, this-> ELEM, other. ELEM, this-> count,
Other. Count, & length );
Set temp (array1, length );
Delete [] array1;
Array1 = NULL;
Return temp;
}
}
Void set: disp ()
{
For (INT I = 0; I <count; ++ I)
{
Cout <ELEM [I] <",";
If (I + 1) % 11 = 0)
{
Cout <Endl;
}
}
Cout <Endl;
}
Int main ()
{
Int setobj1 [] = {1, 3, 4, 5, 6, 7 };
Int setobj2 [] = {2, 4, 5, 8, 9, 0 };
Int len1 = sizeof (setobj1)/sizeof (INT );
Int len2 = sizeof (setobj2)/sizeof (INT );
Set Seta (setobj1, len1 );
Set SETB (setobj2, len2 );
Cout <"the set a is:" <Endl;
Seta. disp ();
Cout <"The Set B is:" <Endl;
SETB. disp ();
Cout <Endl;
Set setaplusb;
Setaplusb = (Seta + SETB );
Cout <"the set a plus Set B is:" <Endl;
Setaplusb. disp ();
Cout <Endl;
Set setcut;
Setcut = (Seta-SETB );
Cout <"the set a cut set B is:" <Endl;
Setcut. disp ();
Cout <Endl;
Set setcross;
Setcross = (Seta * SETB );
Cout <"the set a cross set B is:" <Endl;
Setcross. disp ();
Cout <Endl;
Int key;
Cout <"input the key to serch" <Endl;
Cin> key;
If (setaplusb. Find (key ))
{
Cout <"Success !! "<Endl;
}
Else
{
Cout <"No found !! "<Endl;
}
Return 0;
}
/*
The set a is:
1, 3, 4, 5, 6, 7,
The Set B is:
2, 4, 5, 8, 9, 0,
The set a plus Set B is:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
The set a cut set B is:
0, 1, 2, 3, 6, 7, 8, 9,
The set a cross set B is:
4, 5,
Input the key to serch
4
Success !!
Press any key to continue...
*/