如何将这三个代码合成一个,并成功运行(一个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);
}