C++利用运算符重载实现集合运算

基本思路就是,类中数据成员为一定长度的整形数组,然后类外一个对象就代表着一个集合,往数组里赋值,再通过三个运算符重载,实现交差并的运算:

交运算:2个for去一个一个对应寻找,看2个集合里是否有相等的元素,如果找到,直接跳出,根据集合的性质,不可能再有相同的了

gather operator*(gather &s,gather &t)
{
    gather g;
    int i,j,counter=0;
    for(i=0;i<s.num;i++)
    {
        for(j=0;j<t.num;j++)
        {
            if(s.a[i]==t.a[j])
                {
                    g.a[i]=s.a[i];
                    g.num=i;
                    break;
                }
        }
    }
    g.num++;
    cout<<"交运算:"<<endl;
    return g;
}

差运算:

就是交运算的一个反向,思路一样:

gather operator-(gather &s,gather &t)
{
    gather g;
    int i,j;
    for(i=0;i<s.num;i++)
    {
        for(j=0;j<t.num;j++)
        {
            if(s.a[i]==t.a[j])
                break;
            if(j==t.num-1)
                g.a[i]=s.a[i];
                    g.num=i;
        }
    }
    g.num++;
    cout<<"差运算:"<<endl;
    return g;
}

并运算:

属这个运算不好写,我也想了好长时间,最终想到一种方案:

把两个集合写在一起,即(两个数组并在一起),然后对这一个数组进行操作,每次找到相同的元素就把数组的元素前移一位,达到删除某一元素的目的,具体代码如下:

gather operator+(gather &s,gather &t)
{
    gather g;
    int i,j,k=s.num+t.num;
    g.num=k;
    for(i=0;i<s.num;i++)
        {
            g.a[i]=s.a[i];
        }
    for(i=0;i<t.num;i++)
        {
            g.a[s.num+i]=t.a[i];
        }
    for(i=0;i<k;i++)
        for(j=0;j<k;j++)
        {
            if(i!=j)
                if(g.a[i]==g.a[j])
                {
                    for(int q=j;q<k;q++)
                    {
                        g.a[q]=g.a[q+1];
                    }
                }
        }
    cout<<"并运算:"<<endl;
    return g;
}

整体程序如下(包含测试):

#include <iostream>
using namespace std;
class gather
{
public:
    gather(int *p,int n);
    gather();
    friend gather operator+(gather &s,gather &t);
    friend gather operator-(gather &s,gather &t);
    friend gather operator*(gather &s,gather &t);
    void display();
private:
    int a[20],num;
};

gather::gather(int *p,int n)
{
    int i;
    num=n;
    for(i=0;i<num;i++)
    {
        a[i]=p[i];
    }
}

gather::gather()
{
    for(int i=0;i<20;i++)
    {

        a[i]='\0';
    }
}

void gather::display()
{
    for(int i=0;i<num;i++)
    {
        if(a[i]!='\0')
        cout<<a[i]<<" ";
    }
}


gather operator+(gather &s,gather &t)
{
    gather g;
    int i,j,k=s.num+t.num;
    g.num=k;
    for(i=0;i<s.num;i++)
        {
            g.a[i]=s.a[i];
        }
    for(i=0;i<t.num;i++)
        {
            g.a[s.num+i]=t.a[i];
        }
    for(i=0;i<k;i++)
        for(j=0;j<k;j++)
        {
            if(i!=j)
                if(g.a[i]==g.a[j])
                {
                    for(int q=j;q<k;q++)
                    {
                        g.a[q]=g.a[q+1];
                    }
                }
        }
    cout<<"并运算:"<<endl;
    return g;
}


gather operator-(gather &s,gather &t)
{
    gather g;
    int i,j;
    for(i=0;i<s.num;i++)
    {
        for(j=0;j<t.num;j++)
        {
            if(s.a[i]==t.a[j])
                break;
            if(j==t.num-1)
                g.a[i]=s.a[i];
                    g.num=i;
        }
    }
    g.num++;
    cout<<"差运算:"<<endl;
    return g;
}


gather operator*(gather &s,gather &t)
{
    gather g;
    int i,j,counter=0;
    for(i=0;i<s.num;i++)
    {
        for(j=0;j<t.num;j++)
        {
            if(s.a[i]==t.a[j])
                {
                    g.a[i]=s.a[i];
                    g.num=i;
                    break;
                }
        }
    }
    g.num++;
    cout<<"交运算:"<<endl;
    return g;
}


int main()
{
    int n1,n2,i,ca;
    cout<<"输入数组1长度"<<endl;
    cin>>n1;
    int a[n1];
    cout<<"请输入数组1"<<endl;
    for(i=0;i<n1;i++)
    {
        cin>>a[i];
    }
    cout<<"输入数组2长度"<<endl;
    cin>>n2;
    int b[n2];
    cout<<"请输入数组2"<<endl;
    for(i=0;i<n2;i++)
    {
        cin>>b[i];
    }
    gather c(a,n1);
    gather d(b,n2);
    gather g;
    cout<<"请选择运算类型:"<<endl<<"1:并运算"<<endl<<"2:差运算"<<endl<<"3:交运算"<<endl;
    cin>>ca;
    switch(ca)
    {
    case 1:
         g=c+d;//根据用户需要自行选择那种运算
         g.display();
         break;
    case 2:
         g=c-d;//根据用户需要自行选择那种运算
         g.display();
         break;
    case 3:
         g=c*d;//根据用户需要自行选择那种运算
         g.display();
         break;
    }
    return 0;
}
 

 这个代码完全是自己想出来的,所以会有很多错误,也有很多不妥当的地方,例如数据成员是数组就欠加,可以用new动态规划一下,还有功能仅限2个集合做运算,n个以上的就没有用了,这个功能我应该再完善一下。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值