m0_74128289 2023-03-25 16:10 采纳率: 87.5%
浏览 29
已结题

如何将这三个代码合成一个

如何将这三个代码合成一个,并成功运行(一个set类集合函数)

#ifndef RSET_SET_H
#define RSET_SET_H
 
#include<iostream>
using namespace std;
 
class Set {
private:
    int Setnum[100];
    int length=0;
public :
    Set();
    Set(int n[100]);
    Set(Set &s);
    ~Set();
 
    void add_num(int num);
    void del_num(int num);
    void show_num();
    Set jiaoji(Set &s2);
    Set bingji(Set &s2);
    Set chaji(Set &s2);
    bool equals1(Set &s2);
    bool ifinclude(Set &s2);
    bool ifempty();
    int num_size();
    void beempty();
 
 
};
 
 
#endif //RSET_SET_H

#include <cstring>
#include "Set.h"
Set::Set()
{
    this->length=0;
    memset(this->Setnum,0,sizeof (Setnum));
}
 
Set::Set(int *n)
{
      for(int i=0;i<_msize(n)/sizeof(*n);i++)
        this->add_num(n[i]);
}
Set::Set(Set &s)
{
    length=s.length;
    for(int i=0;i<s.length;i++)
    {
        Setnum[i]=s.Setnum[i];
    }
}
Set::~Set()
{
 
}
void Set::add_num(int num)
{
    int flag=0;
    for(int i=0;i<length;i++)
    {
        if(Setnum[i]==num)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        Setnum[length]=num;
        length++;
    }
}
void Set::del_num(int num)
{
    int n=length;
    while(n--){
        if(Setnum[n]==num)
            break;
    }
    for(int i=n;i<length-1;i++)
    {
        Setnum[i]=Setnum[i+1];
    }
    length--;
 
}
void Set::show_num()
{
    for(int i=0;i<length;i++)
    {
        cout<<Setnum[i]<<" ";
    }
    cout<<endl;
}
Set Set::jiaoji(Set &s2)
{
    Set s;
    for(int i=0;i<this->length;i++)
    {
        s.Setnum[i]=this->Setnum[i];
        s.length= this->length;
    }
    for(int i=0;i<s2.length;i++)
    {
        s.add_num(s2.Setnum[i]);
    }
    return s;
}
Set Set::bingji(Set &s2)
{
    Set s;
    for(int i=0;i<this->length;i++)
    {
        for(int j=0;j<s2.length;j++)
        {
            if(this->Setnum[i]==s2.Setnum[j])
            {
                s.Setnum[s.length]=s2.Setnum[j];
                s.length++;
            }
        }
    }
    return s;
}
Set Set::chaji(Set &s2)
{
    int flag=0;
    Set s,sbing;
    sbing=this->bingji(s2);
    for(int i=0;i<this->length;i++)
    {
        flag=0;
         for(int j=0;j<sbing.length;j++)
         {
             if(this->Setnum[i]==sbing.Setnum[j])
             {
                 flag=1;
                 break;
             }
         }
         if(flag==0){
             s.Setnum[s.length]=this->Setnum[i];
             s.length++;
         }
    }
    return s;
}
bool Set::equals1(Set &s2)
{
    if(this->length!=s2.length)
        return false;
    for(int i=0;i<this->length;i++)
    {
        if(this->Setnum[i]!=s2.Setnum[i])
            return false;
    }
    return true;
}
 
bool Set::ifinclude(Set &s2)
{
    Set *sbing=new Set();
    *sbing=this->bingji(s2);
    if(sbing->length==this->length||sbing->length==s2.length)
        return true;
    else
        return false;
}
 
bool Set::ifempty()
{
    if(this->length==0)
        return true;
    else
        return false;
}
 
int Set::num_size()
{
    return this->length;
}
 
void Set::beempty()
{
    memset(Setnum,0,sizeof(Setnum));
    this->length=0;
}

#include <iostream>
#include"Set.h"
 
int main()
{
    cout<<"1.创建集合 s1 & s2和s3"<<endl;
    Set *s1=new Set(new int[]{1,10,9,8,7});
    Set *s2=new Set(new int[]{1,8,7});
    Set *s3=new Set(new int[]{1,7,9,6,5});
    Set s4(*s3);
    s1->show_num();
    s2->show_num();
    s3->show_num();
    cout<<"**************************"<<endl;
    cout<<"2.向集合s1中添加一个元素5"<<endl;
    s1->add_num(5);
    s1->show_num();
    cout<<"**************************"<<endl;
    cout<<"3.从集合s3中删除一个元素7"<<endl;
    s3->del_num(7);
    s3->show_num();
    cout<<"**************************"<<endl;
    cout<<"4.计算s1和s2的交集"<<endl;
    Set *sjiao=new Set();
    *sjiao=s1->jiaoji(*s2);
    sjiao->show_num();
    cout<<"**************************"<<endl;
    cout<<"5.计算s1和s2的并集"<<endl;
    Set *sbing=new Set();
    *sbing=s1->bingji(*s2);
    sbing->show_num();
    cout<<"**************************"<<endl;
    cout<<"6.计算s1和s2的差集"<<endl;
    Set *scha=new Set();
    *scha=s1->chaji(*s2);
    scha->show_num();
    cout<<"**************************"<<endl;
    cout<<"7.显示指定的集合"<<endl;
    cout<<"s1集合:";s1->show_num();
    cout<<"s1集合:";s2->show_num();
    cout<<"s1集合:";s3->show_num();
    cout<<"**************************"<<endl;
    cout<<"8.显示s1和s2是否相等"<<endl;
    cout<<s1->equals1(*s2);
    cout<<"**************************"<<endl;
    cout<<"9.显示s1是否包含s2"<<endl;
    cout.setf(ios_base::boolalpha);
    cout<<s1->ifinclude(*s2)<<endl;
    cout<<"**************************"<<endl;
    cout<<"10.清空指定集合s4"<<endl;
    cout<<"清空前";s4.show_num();
    s4.beempty();
    cout<<"清空后";s4.show_num();
    cout<<"**************************"<<endl;
    cout<<"11。显示指定集合是否为空 "<<endl;
    cout<<"s1是否为空:"<<s1->ifempty()<<" 长度:"<<s1->num_size()<<endl;
    cout<<"s2是否为空:"<<s2->ifempty()<<" 长度:"<<s2->num_size()<<endl;
    cout<<"s3是否为空:"<<s3->ifempty()<<" 长度:"<<s3->num_size()<<endl;
    cout<<"s4是否为空:"<<s4.ifempty()<<" 长度:"<<s4.num_size()<<endl;
    cout<<"**************************"<<endl;
    cout<<"12.Quit program"<<endl;
    exit(0);
}


  • 写回答

3条回答 默认 最新

  • IT_service_mesh 2023-03-25 16:23
    关注

    参考GPT和自己的思路:这三个代码已经组成了一个完整的程序,你可以直接编译和运行这个程序。不需要将它们再次合并。在运行程序之前,你需要确保 Set.h 和 Set.cpp 文件是正确的,并且存在于相应的目录下。然后,你就可以在命令行或者集成开发环境(IDE)中编译和运行程序了。请注意,这个程序实际上是使用了一个 main 函数作为整个程序的入口。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 创建了问题 3月25日

悬赏问题

  • ¥15 一道ban了很多东西的pyjail题
  • ¥15 关于#r语言#的问题:如何将生成的四幅图排在一起,且对变量的赋值进行更改,让组合的图漂亮、美观@(相关搜索:森林图)
  • ¥15 C++识别堆叠物体异常
  • ¥15 微软硬件驱动认证账号申请
  • ¥15 有人知道怎么在R语言里下载Git上的miceco这个包吗
  • ¥15 GPT写作提示指令词
  • ¥20 根据动态演化博弈支付矩阵完成复制动态方程求解和演化相图分析等
  • ¥20 关于DAC输出1.000V对分辨率和精度的要求
  • ¥15 华为超融合部署环境下RedHat虚拟机分区扩容问题
  • ¥15 哪位能做百度地图导航触点播报?