1)可以用以下表达式实现整数集合的运算:
s1+s2 两个整数集合的并运算
s1-s2 两个整数集合的差运算
s1*s2 两个整数集合的交运算
使用C++语言编写
用类gather(集合)来实现这些功能。gather类的框架
class gather //集合
{
public:
gather(int n);
void show_gather();
gather operator+(gather& g);
gather operator-(gather& g);
gather operator*(gather& g);
int* arr;
int size;
};
gather::gather(int n)
{
arr = new int[n];
size = n;
}
1.先将本身的集合存入临时的一个集合
2.通过两层for循环来寻找本身的集合没有的元素
gather gather::operator+(gather& g)
{
int s1 = this->size, s2 = g.size,i,j,flag=0,num=0;
int* temp = new int[s1+s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for ( i = 0; i < s1; i++)
temp[i] = this->arr[i];
for ( j = 0; j < s2; j++)
{
for (i = 0; i < s1; i++)
if (temp[i] == g.arr[j])
break;
if (i >= s1)
temp[s1 + (flag++)] = g.arr[j];
else
num++;
}
gt.arr = temp;
gt.size = s1 + s2 - num;
return gt;
}
这里的差的解释:
集合a{ 1 ,2 , 3 }
集合b{ 2, 8 ,9 }
其中a-b的结果就是去除a中与b相同的元素
核心思想:通过两层for循环的遍历,找出两个集合中的重复元素,只要是重复元素就不再存入数组
这里我用一个bool类型的数组来表示是否重复
gather gather::operator-(gather& g) //差
{
int s1 = this->size, s2 = g.size, i, j, flag = 0, num = 0;
int* temp = new int[s1 + s2];
bool* isSame = new bool[s1 + s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for (i = 0; i < s1; i++)
{
for (j = 0; j < s2; j++)
if (this->arr[i] == g.arr[j])
{
isSame[i] = true;
break;
}
if (j >= s2)
{
isSame[i] = false;
num++;
}
}
for (i = 0; i < s1; i++)
if (isSame[i] == false)
temp[flag++] = this->arr[i];
if (num == 0)
{
delete[]temp;
temp = NULL;
}
gt.arr = temp;
gt.size = flag;
return gt;
}
交集其实和差的过程类似,只不过差是需要返回数组中不相同的元素,而交集是需要两个集合都有的元素
核心思想:两层for循环遍历出两个集合中相同的元素。
gather gather::operator*(gather& g)
{
int s1 = this->size, s2 = g.size, i, j,flag=0, num = 0;
int* temp = new int[s1 + s2];
bool* isSame = new bool[s1 + s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for (i = 0; i < s1; i++)
{
for(j=0;j<s2;j++)
if (g.arr[j] == this->arr[i])
{
isSame[i] = true;
num++;
break;
}
if (j >= s2)
isSame[i] = false;
}
for (i = 0; i < s1; i++)
if (isSame[i] == true)
temp[flag++] = this->arr[i];
if (num == 0)
{
delete[]temp;
temp = NULL;
}
gt.arr = temp;
gt.size = num;
return gt;
}
检验过程其实很简单,只要简单的将结果输出即可。
这里完善一下类内的输出函数
唯一要注意的就是空集,也就是集合元素为0的时候,指针的值是NULL,这时候要直接返回,否则对空地址的范围会报错。
void gather::show_gather()
{
if (this->arr == NULL)
{
cout << "空" << endl;
return;
}
for (int i = 0; i < this->size; i++)
cout << this->arr[i] << ' ';
cout << endl;
}
这里我输入数组时偷了个懒,采用for循环自动输入,其实也可以设置一个seed后用随机数去填充。
#include
#include
#include
using namespace std;
#define str string//头文件 命名空间
class gather //集合
{
public:
gather(int n);
void show_gather();
gather operator+(gather& g);
gather operator-(gather& g);
gather operator*(gather& g);
int* arr;
int size;
};
void test()
{
int n1, n2;
cout << "输入两个集合的元素个数:\n";
cin >> n1 >> n2;
Sleep(500);
system("cls");
gather g1(n1),g2(n2),g3(n1+n2);
for (int i = 0; i < g1.size; i++)
g1.arr[i] = i;
for (int i = 0; i < g2.size; i++)
g2.arr[i] = i;
cout << "g1 的元素:";
g1.show_gather();
cout << "\ng2 的元素:";
g2.show_gather();
g3 = g1 + g2;
cout << "\ng1 与 g2 的并集:";
g3.show_gather();
g3 = g1 - g2;
cout << "\ng1 与 g2 的差集:";
g3.show_gather();
g3 = g1 * g2;
cout << "\ng1 与 g2 的交集:";
g3.show_gather();
}
int main()
{
test();
return 0;
}
gather::gather(int n)
{
arr = new int[n];
size = n;
}
void gather::show_gather()
{
if (this->arr == NULL)
{
cout << "空" << endl;
return;
}
for (int i = 0; i < this->size; i++)
cout << this->arr[i] << ' ';
cout << endl;
}
gather gather::operator+(gather& g)
{
int s1 = this->size, s2 = g.size,i,j,flag=0,num=0;
int* temp = new int[s1+s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for ( i = 0; i < s1; i++)
temp[i] = this->arr[i];
for ( j = 0; j < s2; j++)
{
for (i = 0; i < s1; i++)
if (temp[i] == g.arr[j])
break;
if (i >= s1)
temp[s1 + (flag++)] = g.arr[j];
else
num++;
}
gt.arr = temp;
gt.size = s1 + s2 - num;
return gt;
}
gather gather::operator-(gather& g)//差
{
int s1 = this->size, s2 = g.size, i, j, flag = 0, num = 0;
int* temp = new int[s1 + s2];
bool* isSame = new bool[s1 + s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for (i = 0; i < s1; i++)
{
for (j = 0; j < s2; j++)
if (this->arr[i] == g.arr[j])
{
isSame[i] = true;
break;
}
if (j >= s2)
{
isSame[i] = false;
num++;
}
}
for (i = 0; i < s1; i++)
if (isSame[i] == false)
temp[flag++] = this->arr[i];
if (num == 0)
{
delete[]temp;
temp = NULL;
}
gt.arr = temp;
gt.size = flag;
return gt;
}
gather gather::operator*(gather& g)
{
int s1 = this->size, s2 = g.size, i, j,flag=0, num = 0;
int* temp = new int[s1 + s2];
bool* isSame = new bool[s1 + s2];
gather gt(s1 + s2);
sort(this->arr, this->arr + s1 - 1);
sort(g.arr, g.arr + s2 - 1);
for (i = 0; i < s1; i++)
{
for(j=0;j<s2;j++)
if (g.arr[j] == this->arr[i])
{
isSame[i] = true;
num++;
break;
}
if (j >= s2)
isSame[i] = false;
}
for (i = 0; i < s1; i++)
if (isSame[i] == true)
temp[flag++] = this->arr[i];
if (num == 0)
{
delete[]temp;
temp = NULL;
}
gt.arr = temp;
gt.size = num;
return gt;
}