整数集合运算(重载)

编写一个程序,通过重载运算符"+"、"-",实现一个整数集合的基本运算:
(1) int1+int2  两个整数集合的并运算
(2) int1-int2  两个整数集合的差运算
输入:5      
     2 5 66 1 79
     3
     2 66 28
结果:int1+int2=2 5 66 1 79 28
       int1-int2=5 1 79
注:第1、3行数为集合元素个数,第2、4行为集合中各个元素

 

#include<iostream>  
using namespace std;  
class Integer  
{     
    public:  
    int a,b[10];  
    void getint() {cin>>a; for(int i=0;i<a;i++) cin>>b[i];}  
    void show();  
    friend Integer operator+(Integer &c1,Integer &c2);  
    friend Integer operator-(Integer &c1,Integer &c2);  
};        
Integer operator+(Integer &c1,Integer &c2)  
{  
    int i,j;  
    for(i=0;i<c2.a;i++)  
    {  
        for(j=0;j<c1.a;j++)  
        {   if(c2.b[i]==c1.b[j]) break;  
            else if(j==c1.a-1)   
            {c1.b[c1.a]=c2.b[i];c1.a++;}  
        }     
    }  
    return c1;  
}  
Integer operator-(Integer &c1,Integer &c2)  
{  
    int i,j;  
    for(i=0;i<c2.a;i++)  
    {  
        for(j=0;j<c1.a;j++)  
        {   if(c2.b[i]==c1.b[j])  
                {for(int m=j;m<c1.a;m++)  
                    c1.b[m]=c1.b[m+1];  
                    c1.a--;  
                }  
        }     
    }  
    return c1;  
}  
void Integer::show()  
    {  if(a!=0)   
        {cout<<b[0];  
            for(int i=1;i<a;i++)  
            cout<<' '<<b[i];  
        }  
        cout<<endl;  
    }  
int main()  
{  
    Integer c1,c2,c3,c4;  
    c1.getint();  
    c2.getint();  
    c3=c1+c2;  
    c4=c1-c2;  
    cout<<"int1+int2=";  
    c3.show();  
    cout<<"int1-int2=";  
    c4.show();  
    return 0;  
} 

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我来为您解答。 我们可以定义一个Set类,用于表示集合,并在其中定义对应的友元函数来实现集合的并、差和交运算。代码如下: ```c++ #include <iostream> #include <set> using namespace std; class Set { private: set<int> data; // 底层数据结构使用set public: // 构造函数 Set() {} // 析构函数 ~Set() {} // 友元函数,计算两个集合的并集 friend Set operator+(const Set &s1, const Set &s2) { Set res; set_union(s1.data.begin(), s1.data.end(), s2.data.begin(), s2.data.end(), inserter(res.data, res.data.begin())); return res; } // 友元函数,计算两个集合的差集 friend Set operator-(const Set &s1, const Set &s2) { Set res; set_difference(s1.data.begin(), s1.data.end(), s2.data.begin(), s2.data.end(), inserter(res.data, res.data.begin())); return res; } // 友元函数,计算两个集合的交集 friend Set operator*(const Set &s1, const Set &s2) { Set res; set_intersection(s1.data.begin(), s1.data.end(), s2.data.begin(), s2.data.end(), inserter(res.data, res.data.begin())); return res; } // 添加元素到集合中 void add(int x) { data.insert(x); } // 输出集合 friend ostream &operator<<(ostream &out, const Set &s) { out << "{ "; for (auto it = s.data.begin(); it != s.data.end(); it++) { out << *it << " "; } out << "}"; return out; } }; int main() { Set s1, s2; s1.add(9); s1.add(5); s1.add(4); s1.add(3); s1.add(6); s1.add(7); s2.add(2); s2.add(4); s2.add(6); s2.add(9); // 输出集合 cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; // 求并集 Set s_union = s1 + s2; cout << "s1 + s2: " << s_union << endl; // 求差集 Set s_diff = s1 - s2; cout << "s1 - s2: " << s_diff << endl; // 求交集 Set s_intersection = s1 * s2; cout << "s1 * s2: " << s_intersection << endl; return 0; } ``` 输出结果如下: ``` s1: { 3 4 5 6 7 9 } s2: { 2 4 6 9 } s1 + s2: { 2 3 4 5 6 7 9 } s1 - s2: { 3 5 7 } s1 * s2: { 4 6 9 } ``` 以上代码定义了一个Set类,其中使用了STL库中的set容器作为底层数据结构,定义了对应的友元函数来实现集合的并、差和交运算。主函数中创建了两个Set对象,添加了对应的元素,然后调用对应的运算重载函数来计算集合的并、差和交,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值