跪求大佬教小白,一个简单题弄了好几天了,实在弄不出来了,求教
c吧
全部回复
仅看楼主
level 1
柯南的路 楼主
2018年04月28日 11点04分 1
level 1
柯南的路 楼主
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <cstring>
#include <bits/stdc++.h>
using namespace std;
class settype
{
public:
settype(); //构造函数,默认集合最大容纳20个元素
settype( const settype& B); //拷贝构造函数
~settype(); //析构函数
void getdata(int *a, int& num) const; //读值函数
void setdata(int *a, int num); //设值函数
settype operator+(settype B); //重载运算符+,实现集合并运算
settype operator*(settype B); //重载运算符*,实现集合交运算
settype operator-(settype B); //重载运算符-,实现集合差运算
settype operator=(settype B); //重载运算符=
private:
int *set; //数组指针
int maxSize; //集合最多可包含的元素个数
int n; //当前已有元素的个数
};
settype::settype()
{
maxSize=20;
n=0;
set=new int [maxSize];
for(int i=0;i<maxSize;i++)
{
set[i]=0;
}
}
void settype::getdata(int *a, int& num) const
{
num=n;
for(int i=0;i<num;i++)
{
a[i]=set[i];
}
}
void settype::setdata(int *a, int num)
{
n=num;
for(int i=0;i<n;i++)
{
set[i]=a[i];
}
}
settype::settype(const settype& B)
{
maxSize=B.maxSize;
n=B.n;
set=new int [maxSize];
for(int i=0;i<n;i++)
{
set[i]=B.set[i];
}
}
settype::~settype()
{
delete [] set;
}
settype settype::operator+(settype B)
{
settype temp;
int i,j,k,l,y,x,m;
temp.n=n;
for(k=0;k<temp.n;k++)
{
temp.set[k]=set[k];
}
for(k=temp.n,i=0;i<B.n;i++)
{
for(j=0;j<n;j++)
{
if(B.set[i]==set[j])
{
break;
}
}
if(j!=n)
{
temp.set[k++]=B.set[i];
}
}
return temp;
}
settype settype::operator-(settype B)
{
settype temp;
int i,j,k,l,y,x,m;
for(k=0,i=0;i<n;i++)
{
for(j=0;j<B.n;j++)
{
if(set[i]==B.set[j])
{
break;
}
}
if(j==B.n)
{
temp.set[k++]=set[i];
}
}
return temp;
}
settype settype::operator*(settype B)
{
settype temp;
int i,j,k,l,t,y,c,x,m;
for(k=0,i=0;i<n;i++)
{
for(j=0;j<B.n;j++)
{
if(set[i]==B.set[j])
{
temp.set[k++]=set[i];
break;
}
}
}
return temp;
}
settype settype::operator=(settype B)
{
if(this!=&B)
{
n=B.n;
maxSize=B.maxSize;
delete [] set;
set=new int [n];
for(int i=0;i<n;i++)
{
set[i]=B.set[i];
}
}
return *this;
}
ostream& operator<<(ostream& os,const settype & B)
{
int nn,*p;int i;
B.getdata(p,nn);
os<<"{";
for( i=0;i<nn;i++)
{
if(i!=nn)
os<<p[i]<<",";
else
os<<p[i];
}
os<<"}"<<endl;
return os;
}
istream& operator>>(istream& is,settype& B)
{
int nn,*p;
is>>nn;
p=new int [nn];
for(int i=0;i<nn;i++)
{
is>>p[i];
}
B.setdata(p,nn);
return is;
}
int main()
{
settype A, B, C;
cout<<C;
cin>>A>>B;
cout<<"A="<<A;
cout<<"B="<<B;
C=A+B;
cout<<(A+B);
cout<<"A+B="<<C;
C=A*B;
cout<<"A*B="<<C;
C=A-B;
cout<<"A-B="<<C;
return 0;
}
2018年04月28日 11点04分 2
level 1
柯南的路 楼主
跪求大佬老帮忙找找错误。
2018年04月28日 11点04分 4
1