#include<iostream>
using namespace std;
class int_set
{
private:
int max_size;
int current_size;
int *p;
public:
int_set(int &arr_size) :max_size(arr_size), current_size(0)
{
this->p = new int[arr_size];
}
int_set(const int_set ©)
{
this->max_size = copy.max_size;
this->current_size = copy.current_size;
this->p = new int[copy.max_size];
for (int i = 0; i < this->current_size; i++)
{
this->p[i] = copy.p[i];
}
}
~int_set()
{
delete[]p;
}
int search(int &elem)
{
for (int i = 0; i < this->max_size; i++)
{
if (this->p[i] == elem)
{
return 1;
}
}
return 0;
}
void input()
{
for (int i = 0; i < this->max_size; i++)
{
cin >> p[i];
}
this->current_size = this->max_size;
}
void ouput()
{
for (int i = 0; i < this->current_size; i++)
{
cout << p[i] << " ";
}
}
int_set* elem_union(int_set *B)
{
int i, j;
int new_max_size = this->max_size + B->max_size;
int_set *C = new int_set(new_max_size);
C->p[0] = this->p[0];
for (i = 1; i < this->max_size; i++)
{
for (j = i - 1; this->p[i] < C->p[j] && j >= 0; j--)
{
C->p[j + 1] = C->p[j];
}
C->p[j + 1] = this->p[i];
}
C->current_size = this->max_size;
for (i = 0; i < B->max_size; i++)
{
if (!C->search(B->p[i]))
{
for (j = C->current_size - 1; B->p[i] < C->p[j] && j >= 0; j--)
{
C->p[j + 1] = C->p[j];
}
C->p[j + 1] = B->p[i];
C->current_size++;
}
}
return C;
}
};
int main()
{
int m = 0, n = 0;
cout << "请输入A集合大小:" << endl;
cin >> m;
cout << "请输入B集合大小:" << endl;
cin >> n;
int w = m + n;
int_set *A = new int_set(m);
int_set *B = new int_set(n);
int_set *temp = new int_set(w);
cout << "请输入集合A元素:" << endl;
A->input();
cout << "请输入集合B元素:" << endl;
B->input();
temp = A->elem_union(B);
cout << "集合C元素:" << endl;
temp->ouput();
system("pause");
return 0;
}