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

【问题描述】
在上一题的基础上,将 + 、 - 运算符定义为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;
}
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小天才才

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值