问题:编写一个程序,实现一个整数集合的基本运算: s1+s2 两整数集合的并运算 s1-s2 两整数集合的差运算 s1*s2 两整数集合的交运算 要求:输入整数集合{2 4 1 3 5}和{2 5 1

#include <iostream>




using namespace std;
const int MAX = 50;




/*
问题:编写一个程序,实现一个整数集合的基本运算:
s1+s2 两整数集合的并运算
s1-s2 两整数集合的差运算
s1*s2 两整数集合的交运算
要求:输入整数集合{2 4 1 3 5}和{2 5 10},输出前者的元素个数以及它们进行集合的
并、差和交运算后的结果
*/
class complex
{
int a[MAX];
//int *a;
int len;
public:
complex(int aa[], int length)
{
//a = aa;
for (int i = 0; i < length; i++)
{
a[i] = aa[i];
}


len = length;




}
complex()
{
len = 0;
}
complex operator +(complex &b)
{
int same = 0;
int i = 0;
for (int j = 0; j< b.len; j++)
{
for (; i < len; i++)
{
if (b.a[j] == a[i])
{
//cout << "***************"<<endl;
same = 1;
break;
}
}
if ((same == 0)&&(i==len))
{
a[len] = b.a[j];
len++;
//cout << "-------------------" << endl;
}
same = 0;
}
return *this;
}
complex operator -(complex &b)
{
int same = 0;
int i = 0;
for (int j = 0; j < b.len; j++)
{
for (; i < len; i++)
{
if (b.a[j] == a[i])
{
same = 1;
break;
}
}
if ((same == 1) )
{
for (int k = i; k+1 < len; k++)
{
a[k] = a[k+1];
}
len--;
}
same = 0;
}
return *this;
}


complex operator *(complex &b)
{
int same = 0;
int i = 0;
int l = 0;
int m[20] = { 0 };
for (int j = 0; j < b.len; j++)
{
for (; i < len; i++)
{
if (b.a[j] == a[i])
{
same = 1;
break;
}
}
if (same == 1)
{

m[l] = a[i];
l++;

}
if (j == b.len-1)
{
for (int k = 0; k < l; k++)
{
a[k] = m[k];
}
len = l;
}

same = 0;

}
return *this;




}
void show()
{
cout << "元素个数:" << len << endl;
for (int i = 0; i < len; i++)
{
cout << "  " << a[i] << "  ";
}
cout << endl;
}
};
void main()
{
int m[] = { 2, 4, 1, 3, 5 };
int n[] = { 2, 5, 10 };
int len1 = sizeof(m) / sizeof(int);
int len2 = sizeof(n) / sizeof(int);




complex intm(m, len1), intn(n, len2),intk;
complex i1, i2;
i1 = intm;
i2 = intm;
intm.show();
intn.show();
cout << "集合的并:";
intk = intm + intn;
intk.show();




cout << "集合的差:";
intk = i1 - intn;
intk.show();




cout << "集合的交:";
intm = i2* intn;
intm.show();












































system("pause");
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值