oj1561集合类实现流输入输出

#include"iostream"
using namespace std;
class settype
{
public:
settype();      //构造函数,默认空集,n=0, set=NULL;
settype(const settype& B);  //拷贝构造函数
~settype();                                   //析构函数
    void getdata(int *a, int& num) const;     //读值函数
void setdata(int *a, int num);       //设值函数
int get_n()const; // 获取集合当前元素数目
    settype operator+(settype B);    //重载运算符+,实现集合并运算
settype operator*(settype B);    //重载运算符*,实现集合交运算
settype operator-(settype B);    //重载运算符-,实现集合差运算
void operator=(settype B);    //重载运算符= 
private:
int *set;               //数组指针
int n;                    //元素的个数
};
settype::settype()
{
n = 0;
set = NULL;
}
settype::settype(const settype& B)
{
n = B.n;
set = new int[n];
for (int i = 0; i < n; i++)
{
set[i] = B.set[i];
}
}
settype::~settype()
{
delete[] set;
}
int settype::get_n()const
{
return n;
}
settype settype::operator+(settype B)
{
int num1, num2,*D,*E,*F;
settype c;
num1 = get_n();
num2=B.get_n();
D = new int[num1+num2];
E = new int[num1];
F = new int[num2];
getdata(E,num1);
B.getdata(F, num2);
int i,j,k,m=0;
for (i = 0; i < num1; i++)
{
D[i] =E[i];
}
for (i = 0; i < num2; i++)
{
k=0;
for (j = 0; j < num1; j++)
{
if(F[i]==E[j])
{
   break;
}
else if(F[i] != E[j])
{
k++;
}

}
if(k==num1)
{
D[num1+m]=F[i];
m++;
}
}
c.setdata(D, num1+ m);
return c;
}
void settype::operator=(settype B)
{
int i = 0;
n = B.n;
set = new int[n];
for (i = 0; i < n; i++)
{
set[i] = B.set[i];
}
}
settype settype::operator*(settype B)
{
settype c;
int *D=new int[n]; 
int i,j,k=0;
for (i = 0; i < n; i++)
{
for (j = 0; j < B.n; j++)
{
if (set[i] == B.set[j])
{
D[k] = set[i];
k++;
}
}
}
c.setdata(D,k);
return c;
}
settype settype::operator-(settype B)
{
settype c;
int num1, num2,*D,*E,*F;
    num1 = get_n();
num2=B.get_n();
D = new int[num1];
E = new int[num1];
getdata(E,num1);
F = new int[num2];
B.getdata(F,num2);
int i, j, k = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < B.n; j++)
{
if (E[i] == F[j])
{
break;
}
if (j ==B.n-1)
{
D[k] =E[i];
k++;
}
}
}
c.setdata(D,k);
c.n=k;
return c;
}
istream &operator>>(istream& is, settype &s)             //输入时让改变传回对象 ???? 
{
int n;
is >> n;
int *p=new int[n];
for (int i = 0; i < n; i++)
{
is >> p[i];
}
s.setdata(p, n);
    return is;
}
ostream &operator<<(ostream& os, settype s) 
{
int num,i;
num = s.get_n();
if (num == 0)
{
os << "{}" << endl;
return os;
}
int *p = new int[num];
    s.getdata(p, num);
os << '{';
for ( i = 0; i < num-1; i++)
{
os << p[i] << ',';
}
os << p[i] << '}' << endl;
return os;
}
void settype::setdata(int *a, int num)
{
int i = 0;
    set=new int[num];
for (i = 0; i < num; i++)
{
set[i] = a[i];
}
n = num;
}
void settype::getdata(int *p, int &num)const
{
for (int i = 0; i < num; i++)
{
p[i] = set[i];
}
}
int main()
{
   settype A, B, C;
    cin>>A>>B;
    cout << "A=" << A;
    cout << "B=" << B;
    C = A + B;
    cout << "A+B=" << C;
    C = A * B;
    cout << "A*B=" << C;
    C = A - B;
    cout << "A-B=" << C;
   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值