栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

gather operator*(gather &s,gather &t)
{
    gather g;
    int i,j,counter=0;
    for(i=0;i 

差运算:

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

gather operator-(gather &s,gather &t)
{
    gather g;
    int i,j;
    for(i=0;i 

并运算:

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

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

gather operator+(gather &s,gather &t)
{
    gather g;
    int i,j,k=s.num+t.num;
    g.num=k;
    for(i=0;i 

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

#include
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     {
        a[i]=p[i];
    }
}

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

        a[i]='';
    }
}

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


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


gather operator-(gather &s,gather &t)
{
    gather g;
    int i,j;
    for(i=0;i     {
        for(j=0;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<<"差运算:"<     return g;
}


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


int main()
{
    int n1,n2,i,ca;
    cout<<"输入数组1长度"<     cin>>n1;
    int a[n1];
    cout<<"请输入数组1"<     for(i=0;i     {
        cin>>a[i];
    }
    cout<<"输入数组2长度"<     cin>>n2;
    int b[n2];
    cout<<"请输入数组2"<     for(i=0;i     {
        cin>>b[i];
    }
    gather c(a,n1);
    gather d(b,n2);
    gather g;
    cout<<"请选择运算类型:"<     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个以上的就没有用了,这个功能我应该再完善一下。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/974957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号