且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C/C++---二进制类的完善(特殊运算符的重载)

更新时间:2022-08-12 19:31:26

【问题描述】
在上一题的基础上,将 + 、 - 运算符定义为binary类的成员函数。并且重载运算符 ~ 、 & 、 | ,分别表示将二进制数按位取反、按位与及按位或。主函数设计如下,请勿修改:

int main(){
binary n1=“1011”;

binary n2=int(n1)+15;

binary n3=n1-binary(7);

cout<<n1<<endl;

cout<<n2<<endl;

cout<<n3<<endl;

binary n4=n1&n2;

binary n5=n1|n2;

binary n6=~n1;

cout<<n4<<endl;

cout<<n5<<endl;

cout<<n6<<endl;

return 0;

}

【样例输出】

0000000000001011

0000000000011010

0000000000000100

0000000000001010

0000000000011011

1111111111110100

代码如下

#include<iostream>
#include<string.h>
#include<cmath>
using namespace std;
class binary {
    char bits[16];
public:
    binary(char *s);
    binary(int n);
    operator int();
    friend ostream & operator<<(ostream &out, binary &b);
    void print();
    binary operator+(binary b2);
    binary operator-(binary b2);
    binary operator~();
    binary operator&(binary b2);
    binary operator|(binary b2);
};
binary::binary(char *s)
{
    int i, size = strlen(s);
    for (i = 0; i < 16 - size; i++)
    {
        bits[i] = '0';
    }
    for (int j = 0; j < size; j++,i++)
    {
        bits[i] = s[j];
    }
}

binary::binary(int n)
{
    int re, i = 0;
    while (n != 0)
    {
        re = n % 2;
        bits[i++] = re + 48;
        n /= 2;
    }
    while (i<16)
    {
        bits[i++] = '0';
    }
    for (int k = 0, j = 15; k < j; k++, j--)
    {
        re = bits[k];
        bits[k] = bits[j];
        bits[j] = re;
    }
}

binary::operator int()
{
    int j = 0, ans = 0;
    for (int i = 15; i >= 0; i--,j++)
    {
        ans += (bits[i] - 48)*pow(2, j);
    }
    return ans;
}

void binary::print()
{
    cout << int(*this) << endl;
}

binary binary::operator+(binary b2)
{
    binary temp(int(*this)+int(b2));
    return temp;
}

binary binary::operator-(binary b2)
{
    binary temp(int(*this)-int(b2));
    return temp;
}

binary binary::operator~()
{
    for (int i = 0; i < 16; i++)
    {
        if (this->bits[i] == '0')
            this->bits[i] = '1';
        else
            this->bits[i] = '0';
    }
    return *this;
}

binary binary::operator&(binary b2)
{
    binary temp = *this;
    for (int i = 0; i < 16; i++)
    {
        if (this->bits[i] == '1'&&b2.bits[i] == '1')
            temp.bits[i] = '1';
        else
            temp.bits[i] = '0';
    }
    return temp;
}

binary binary::operator|(binary b2)
{
    binary temp = *this;
    for (int i = 0; i < 16; i++)
    {
        if (this->bits[i] == '0'&&b2.bits[i] == '0')
            temp.bits[i] = '0';
        else
            temp.bits[i] = '1';
    }
    return temp;
}

ostream & operator<<(ostream & out, binary & b)
{
    for (int i = 0; i < 16; i++)
        out << b.bits[i];
    return out;
}


int main()
{//主函数设计如下,请勿修改:
    binary n1 = const_cast<char*>("1011");
    binary n2 = int(n1) + 15;
    binary n3 = n1 - binary(7);
    cout << n1 << endl;
    cout << n2 << endl;
    cout << n3 << endl;
    binary n4 = n1&n2;
    binary n5 = n1 | n2;
    binary n6 = ~n1;
    cout << n4 << endl;
    cout << n5 << endl;
    cout << n6 << endl;
    return 0;
}