#include <iostream>
using namespace std;
template < typename SaveType= int >
class Set {
public :
explicit Set ( int m_max = 20 ) {
this - > max = m_max;
this - > index = 0 ;
this - > a = new SaveType[ max] ;
}
Set< SaveType> & operator = ( Set m_set) {
Set temp_set = new Set< SaveType> ( this - > index + m_set. index + 2 ) ;
for ( int i = 0 ; i < this - > index + m_set. index; ++ i) {
temp_set. append ( this - > a[ i] ) ;
}
return * this ;
}
Set< SaveType> & operator - ( Set m_set) {
for ( int i = 0 ; i < m_set. index+ 1 ; ++ i) {
this - > setDeleteNum ( m_set. a[ i] ) ;
}
return * this ;
}
Set< SaveType> Union ( Set m_set) {
if ( this - > isEmpyt ( ) ) {
return m_set;
} else {
Set temp_set = * new Set< SaveType> ( ( int ) ( this - > index + m_set. index + 2 ) ) ;
for ( int i = 0 ; i < this - > index; ++ i) {
temp_set. append ( this - > a[ i] ) ;
}
for ( int i = 0 ; i < m_set. index; ++ i) {
temp_set. append ( m_set. a[ i] ) ;
}
return temp_set;
}
}
Set< SaveType> intersection ( Set m_set) {
if ( this - > isEmpyt ( ) ) {
return m_set;
} else {
Set temp_set = * new Set< SaveType> ( ( int ) ( this - > index + m_set. index + 2 ) ) ;
for ( int i = 0 ; i < m_set. index; ++ i) {
if ( find ( m_set. a[ i] ) ) {
temp_set. append ( m_set. a[ i] ) ;
}
}
return temp_set;
}
}
Set< SaveType> Difference ( Set m_set) {
if ( this - > isEmpyt ( ) ) {
return m_set;
} else {
Set< SaveType> temp_intersection_set = this - > intersection ( m_set) ;
Set< SaveType> temp_Union_set = this - > Union ( m_set) ;
Set< SaveType> temp_Difference_set = * this - m_set;
return temp_Difference_set;
}
}
void setinit ( ) {
for ( int i = 0 ; i < this - > max; i++ ) {
this - > append ( 100 - i) ;
}
}
int setFindNum ( SaveType num) {
int m_index = find ( num) ;
if ( m_index != 0 ) {
return m_index - 1 ;
} else {
return - 1 ;
}
}
void append ( SaveType num) {
if ( this - > isFull ( ) ) {
cout << "超出范围" << endl;
return ;
}
if ( find ( num) ) {
return ;
}
this - > a[ index] = num;
index++ ;
}
SaveType pop ( ) {
if ( this - > isEmpyt ( ) ) {
cout << "元素已空" << endl;
return 0 ;
}
index-- ;
SaveType temp = this - > a[ index] ;
SaveType temp_null = 0 ;
this - > a[ index] = temp_null;
return temp;
}
void setDeleteNum ( SaveType num) {
int index_temp = find ( num) ;
if ( index_temp != 0 ) {
for ( int i = index_temp - 1 ; i < index - 1 ; i++ ) {
this - > a[ i] = this - > a[ i + 1 ] ;
}
SaveType temp_null = 0 ;
this - > a[ -- index] = temp_null;
} else {
return ;
}
}
void printf ( ) {
for ( int i = 0 ; i < index; i++ ) {
cout << this - > a[ i] << " , " ;
}
cout << endl;
}
int length ( ) {
return index;
}
void setInsert ( int n, SaveType e) {
if ( this - > isFull ( ) ) {
cout << "插入,集合元素已经满了" << endl;
return ;
}
if ( find ( e) ) {
return ;
}
for ( int i = index - 1 ; i > n - 1 ; i-- ) {
this - > a[ i + 1 ] = a[ i] ;
}
a[ n] = e;
index++ ;
}
private :
int max;
int index;
SaveType * a;
int isEmpyt ( ) {
return index == 0 ? 1 : 0 ;
}
int isFull ( ) {
return index == max ? 1 : 0 ;
}
int find ( SaveType num) {
for ( int i = 0 ; i < index; i++ ) {
if ( this - > a[ i] == num) {
return i + 1 ;
}
}
return 0 ;
}
} ;
int main ( ) {
Set< char > x ( 30 ) ;
x. append ( 'z' ) ;
x. append ( 'c' ) ;
x. append ( 'q' ) ;
x. append ( 'a' ) ;
x. append ( 't' ) ;
x. append ( 'q' ) ;
x. append ( 'a' ) ;
Set< char > y ( 20 ) ;
y. append ( 'a' ) ;
y. append ( 'f' ) ;
y. append ( 'p' ) ;
y. append ( 'A' ) ;
y. append ( 'W' ) ;
y. append ( 'D' ) ;
y. append ( 'C' ) ;
y. append ( 'q' ) ;
y. append ( 'a' ) ;
cout << "集合x:" ;
x. printf ( ) ;
cout << "集合y:" ;
y. printf ( ) ;
cout << endl;
cout << "交集z0:" ;
Set< char > z0 = x. intersection ( y) ;
z0. printf ( ) ;
cout<< endl;
Set< char > z1 = x. Union ( y) ;
cout << "并集z1:" ;
z1. printf ( ) ;
cout<< endl;
Set< char > z2 = x. Difference ( y) ;
cout << "差集z2:" ;
z2. printf ( ) ;
cout<< endl;
return 0 ;
}