![]() |
#2
ClearningC2017-03-31 22:43
|
operator<() ;
operator<=();
operator>();
operator>=();
operator&=();
operator|=();
operator^=();
operator&();
operator|();
operator[]();
这里有一道题
目的是写出类里面的函数。

#include<iostream>
using namespace std;
int main() {
bitset a, b;
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.set(x);
}
cout << "a.count() is " << a.count() << "\n";
cout << "a.test(5) is " << (a.test(5) ? "true" : "false") << "\n";
cout << "a.any() is " << (a.any() ? "true" : "false") << "\n";
cout << "a.none() is " << (a.none() ? "true" : "false") << "\n";
cout << "a.all() is " << (a.all() ? "true" : "false") << "\n";
b = ~b;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
b.reset(x);
}
cout << a << "\n";
cout << b << "\n";
if (a == b) {
cout << "hello\n";
}
if (a != b) {
cout << "world\n";
}
bitset c;
// test &
c = a;
c &= b;
cout << c << "\n";
c = a & b;
cout << c << "\n";
// test |
c = a;
c |= b;
cout << c << "\n";
c = a | b;
cout << c << "\n";
// test ^
c = a;
c ^= b;
cout << c << "\n";
c = a ^ b;
cout << c << "\n";
// test <<
c = a;
c <<= 2;
cout << c << "\n";
c = a << 2;
cout << c << "\n";
// test >>
c = b;
c >>= 2;
cout << c << "\n";
c = b >> 2;
cout << c << "\n";
// test []
for (int i = 0; i < q; i++) {
int x;
cin >> x;
if (a[i])
cout << "Yes\n";
else
cout << "No\n";
}
}
#define N 5
const int max_length = 32 * N;//这个数字共有32*5位。从第0位到第max_length-1位,第0位是最低位。
class bitset {
private:
int a[N];//一个int32位,所以只用开N个int
public:
bitset();//默认构造函数,所有位初始化为0
void set(int pos);//把位置pos设置成1
void reset(int pos);//将位置pos设置成0
int count() const;//输出一共有多少个为1的位
bool test(int pos) const;//位置pos是否是1
bool any() const;//是否有是1的位
bool none() const;//是否没有是1的位
bool all() const;//是否所有位都是1
//位运算部分和普通数字的位运算相同。
bitset& operator&= (const bitset& b);
bitset& operator|= (const bitset& b);
bitset& operator^= (const bitset& b);
bitset& operator= (const bitset& b);
bitset& operator <<= (int pos);
bitset& operator >>= (int pos);
bitset operator~() const;
bitset operator&(const bitset& b) const;
bitset operator|(const bitset& b) const;
bitset operator^(const bitset& b) const;
bitset operator<<(int pos) const;
bitset operator>>(int pos) const;
bool operator== (const bitset& b) const;
bool operator!= (const bitset& b) const;
bool operator[] (int pos) const;//返回位置pos是不是1.
using namespace std;
int main() {
bitset a, b;
int n, m, q;
cin >> n >> m >> q;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.set(x);
}
cout << "a.count() is " << a.count() << "\n";
cout << "a.test(5) is " << (a.test(5) ? "true" : "false") << "\n";
cout << "a.any() is " << (a.any() ? "true" : "false") << "\n";
cout << "a.none() is " << (a.none() ? "true" : "false") << "\n";
cout << "a.all() is " << (a.all() ? "true" : "false") << "\n";
b = ~b;
for (int i = 0; i < m; i++) {
int x;
cin >> x;
b.reset(x);
}
cout << a << "\n";
cout << b << "\n";
if (a == b) {
cout << "hello\n";
}
if (a != b) {
cout << "world\n";
}
bitset c;
// test &
c = a;
c &= b;
cout << c << "\n";
c = a & b;
cout << c << "\n";
// test |
c = a;
c |= b;
cout << c << "\n";
c = a | b;
cout << c << "\n";
// test ^
c = a;
c ^= b;
cout << c << "\n";
c = a ^ b;
cout << c << "\n";
// test <<
c = a;
c <<= 2;
cout << c << "\n";
c = a << 2;
cout << c << "\n";
// test >>
c = b;
c >>= 2;
cout << c << "\n";
c = b >> 2;
cout << c << "\n";
// test []
for (int i = 0; i < q; i++) {
int x;
cin >> x;
if (a[i])
cout << "Yes\n";
else
cout << "No\n";
}
}
#define N 5
const int max_length = 32 * N;//这个数字共有32*5位。从第0位到第max_length-1位,第0位是最低位。
class bitset {
private:
int a[N];//一个int32位,所以只用开N个int
public:
bitset();//默认构造函数,所有位初始化为0
void set(int pos);//把位置pos设置成1
void reset(int pos);//将位置pos设置成0
int count() const;//输出一共有多少个为1的位
bool test(int pos) const;//位置pos是否是1
bool any() const;//是否有是1的位
bool none() const;//是否没有是1的位
bool all() const;//是否所有位都是1
//位运算部分和普通数字的位运算相同。
bitset& operator&= (const bitset& b);
bitset& operator|= (const bitset& b);
bitset& operator^= (const bitset& b);
bitset& operator= (const bitset& b);
bitset& operator <<= (int pos);
bitset& operator >>= (int pos);
bitset operator~() const;
bitset operator&(const bitset& b) const;
bitset operator|(const bitset& b) const;
bitset operator^(const bitset& b) const;
bitset operator<<(int pos) const;
bitset operator>>(int pos) const;
bool operator== (const bitset& b) const;
bool operator!= (const bitset& b) const;
bool operator[] (int pos) const;//返回位置pos是不是1.