1810 Bitset (eden)

来源:互联网 发布:农村淘宝怎么下载 编辑:程序博客网 时间:2024/06/08 07:03

Description:

Please complete this class.

The class bitset is just like a big number which can have bit operations and some other operations.

You can read main.cpp to understand how I check your program.

Hint:

这并不是真正的bitset,只是用一些简单的c++知识来模仿bitset。

#define N 5const int max_length = 32 * N;//这个数字共有32*5位。从第0位到第max_length-1位,第0位是最低位。class bitset {private:int a[N];//一个int32位,所以只用开N个intpublic:bitset();//默认构造函数,所有位初始化为0void set(int pos);//把位置pos设置成1void reset(int pos);//将位置pos设置成0int count() const;//输出一共有多少个为1的位bool test(int pos) const;//位置pos是否是1bool 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.//输出部分已经实现

出题人: 王毅峰

Provided Codes

main.cpp

#include<iostream>#include"Bitset.h"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";    }}

Bitset.h

#ifndef BITSET_H#define BITSET_H#include<iostream>#define N 5const int max_length = 32 * N;class bitset {    private:        int a[N];    public:        bitset();        void set(int pos);        void reset(int pos);        int count() const;        bool test(int pos) const;        bool any() const;        bool none() const;        bool all() const;        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;        friend std::ostream& operator << (std::ostream& os, const bitset& s) {            for (int i = N-1; i >= 0; i--) {                for (int j = 31; j >= 0; j--) {                    if (s.a[i] & (1 << j)) os << 1;                    else os << 0;                }            }            return os;        }};#endif

Submission

Bitset.cpp

#include"Bitset.h"#include<cmath>//默认构造函数,所有位初始化为0bitset::bitset() {    for (int i = 0; i < N; i++)        a[i] = 0;}//把位置pos设置成1void bitset::set(int pos) {    if (!test(pos)) {        if (pos % 32 == 31)            a[pos / 32] -= std::pow(2, 31);        else            a[pos / 32] += std::pow(2, pos % 32);    }}//将位置pos设置成0void bitset::reset(int pos) {    if (test(pos)) {        if (pos % 32 == 31)            a[pos / 32] += std::pow(2, 31);        else            a[pos / 32] -= std::pow(2, pos % 32);    }}//输出一共有多少个为1的位int bitset::count() const {    int count = 0;    for (int i = 0; i < N; i++)        for (int j = 0; j < 32; j++)            count += (a[i] & (1 << j)) ? 1 : 0;    return count;}//位置pos是否是1bool bitset::test(int pos) const {    return (a[pos / 32] & (1 << pos % 32)) ? true : false;}//是否有是1的位bool bitset::any() const {    return (count()) ? true : false;}//是否没有是1的位bool bitset::none() const {    return !any();}//是否所有位都是1bool bitset::all() const {    return (count() == 32 * N) ? true : false;}//位运算部分和普通数字的位运算相同。bitset& bitset::operator&= (const bitset& b) {    for (int i = 0; i < N; i++)        a[i] &= b.a[i];    return *this;}bitset& bitset::operator|= (const bitset& b) {    for (int i = 0; i < N; i++)        a[i] |= b.a[i];    return *this;}bitset& bitset::operator^= (const bitset& b) {    for (int i = 0; i < N; i++)        a[i] ^= b.a[i];    return *this;}bitset& bitset::operator= (const bitset& b) {    for (int i = 0; i < N; i++)        a[i] = b.a[i];    return *this;}bitset& bitset::operator <<= (int pos) {    *this = operator<<(pos);    return *this;}bitset& bitset::operator >>= (int pos) {    *this = operator>>(pos);    return *this;}bitset bitset::operator~() const {    bitset tem;    for (int i = 0; i < N; i++)        tem.a[i] = ~a[i];    return tem;}bitset bitset::operator&(const bitset& b) const {    bitset tem;    for (int i = 0; i < N; i++)        tem.a[i] = a[i] & b.a[i];    return tem;}bitset bitset::operator|(const bitset& b) const {    bitset tem;    for (int i = 0; i < N; i++)        tem.a[i] = a[i] | b.a[i];    return tem;}bitset bitset::operator^(const bitset& b) const {    bitset tem;    for (int i = 0; i < N; i++)        tem.a[i] = a[i] ^ b.a[i];    return tem;}bitset bitset::operator<<(int pos) const {    bitset tem;    for (int i = 0; i < N; i++) {        tem.a[i] = (a[i] << pos);        if (i != 0)            for (int j = 32 * i - 1; j >= 32 * i - pos; j--)                if (test(j))                    tem.set(j + pos);    }    return tem;}bitset bitset::operator>>(int pos) const {    bitset tem;    for (int i = N - 1; i >= 0; i--) {        tem.a[i] = (a[i] >> pos);        if (i != N - 1)            for (int j = 32 * (i + 1); j < 32 * (i + 1) + pos; j++)                test(j) ? tem.set(j - pos) : tem.reset(j - pos);    }    return tem;}bool bitset::operator== (const bitset& b) const {    for (int i = 0; i < N; i++)        if (a[i] != b.a[i])            return false;    return true;}bool bitset::operator!= (const bitset& b) const {    return !operator==(b);}//返回位置pos是不是1bool bitset::operator[] (int pos) const {    return (a[pos / 32] & (1 << pos % 32)) ? true : false;}
原创粉丝点击