上海科技艺术教育中心:啊 烦人的C++ 谁能帮我编个程序啊

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/02 13:07:26
编个程序可以完成1~500内集合间的简单运算( 交,并,差)求某一个集合是否属于另 一个
集合

#include <bitset>
using namespace std;

class Set500
{
public:
bitset<500> set;
bool operator[](int i){return set[i];}
Set500 operator&(Set500& another); //交
Set500 operator|(Set500& another); //并
Set500 operator-(Set500& another); //差
bool operator>(Set500& another); //大于
}

Set500 Set500::operator&(Set500& another)
{
Set500 result;
for(int i=1;i<500;i++)
result.set(i,(*this)[i] && another[i]);
return result;
}

Set500 Set500::operator|(Set500& another)
{
Set500 result;
for(int i=1;i<500;i++)
result.set(i,(*this)[i] || another[i]);
return result;
}

Set500 Set500::operator-(Set500& another)
{
Set500 result;
for(int i=1;i<500;i++)
result.set(i,(*this)[i]?(*this)[i]&&another[i]: false);
return result;
}

bool Set500::operator>(Set500& another)
{
Set500 result;
for(int i=1;i<500;i++)
if(another[i] && !(*this)[i]) //别人有但自己//没有则别人肯定不是自己的子集
return false;
return true;
}

这样可以吗,仓促一点,可能有bug

用stl可以轻松搞定的,你等等,我看看