Problem D: 二进制类(2)——运算符重载-hzauoj
来源:互联网 发布:激光镭射键盘知乎 编辑:程序博客网 时间:2024/06/06 17:58
Problem D: 二进制类(2)——运算符重载
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 28 Solved: 23
[Submit][Status][Web Board]
Description
在实验四二进制类binary的基础上,将+ 、- 运算符定义为binary 类的成员函数。并且重载运算符~ 、& 、| ,分别表示将二进制数按位取反、按位与及按位或。主
函数设计如下:
int main(){
binary n1="1011";
binary n2=int(n1)+15;
binary n3=n1-binary(7);
n1.print();
n2.print();
n3.print();
binary n4=n1&n2;
binary n5=n1|n2;
binary n6=~n1;
n4.print();
n5.print();
n6.print();
return 0;
}
Input
Output
Sample Output
000000000000101100000000000110100000000000000100000000000000101000000000000110111111111111110100
HINT
#include<iostream>#include<string.h>#include<math.h>using namespace std;class binary{public: binary(){ } binary(char *); binary(int n);public: operator int() { int temp = 0; for(int i = 0; i < 16; i++) temp = temp * 2 + bits[i] - '0'; return temp; } void print() { cout << bits << endl; }public: binary operator +(const binary &b) { int temp1 = int(*this); binary tb2 = b; int temp2 = int(tb2); return binary(temp1 + temp2); } binary operator-(const binary &b) { int temp1 = int(*this); binary tb2 = b; int temp2 = int(tb2); return binary(fabs(temp1 - temp2)); }public: binary operator~() { char temp[17]; for(int i = 0; i < 16; i++) if(bits[i] == '0') temp[i] = '1'; else temp[i] = '0'; temp[16] = '\0'; return binary(temp); } binary operator &(const binary & b) { char temp[16]; for(int i = 0; i < 16; i++) if(bits[i] == '1' && b.bits[i] =='1') temp[i] = '1'; else temp[i] = '0'; temp[16] = '\0'; return binary(temp); }public: binary operator | (const binary & b) { char temp[17]; for(int i = 0; i < 16; i++) if(bits[i] == '0' && b.bits[i] =='0') temp[i] = '0'; else temp[i] = '1'; temp[16] = '\0'; return binary(temp); } binary operator =(const binary & b) { for(int i = 0; i < 16; i++) bits[i] = b.bits[i]; bits[16] = '\0'; return *this; }private: char bits[17]; //二进制字模数组,要开16个};binary :: binary(char *p){ int d = strlen(p)-1; int i, j; j = d; for(i = 15; i >= 15 - d; i--,j--) bits[i] = p[j]; for(; i >= 0; i--) bits[i] = '0'; bits[16] = '\0';} binary :: binary(int n){ int i = 15; while(n!=0) { int t; t = n % 2; bits[i] = t + '0'; i--; n=n / 2; } for( int j = 0; j <= i; j++) bits[j] = '0'; bits[16] = '\0';}int main(){ binary n1="1011"; binary n2=int(n1)+15; binary n3=n1-binary(7); n1.print(); n2.print(); n3.print(); binary n4=n1&n2; binary n5=n1|n2; binary n6=~n1; n4.print(); n5.print(); n6.print(); return 0;}
0 0
- Problem D: 二进制类(2)——运算符重载-hzauoj
- Problem F: 二进制类(1)__运算符重载 hzauoj
- Problem D: 整型数组运算符重载
- 第十五周oj刷题——Problem L: C++习题 复数类--重载运算符2+
- OJ第三批——Problem A :实现复数类中的加运算符重载【C++运算符重载】
- 第十七周oj刷题——Problem A: 实现复数类中的加运算符重载【C++运算符重载】
- 第十五周oj刷题—— Problem C: 矩形类中运算符重载【C++】
- 第十五周oj刷题——Problem J: C++习题 复数类--重载运算符+
- 第十五周oj刷题——Problem K: C++习题 复数类--重载运算符+,-,*,/
- 第十五周oj刷题——Problem N: C++习题 复数类--重载运算符3+
- OJ第三批——Problem O:B C++时间类的运算符重载
- 第十七周oj刷题——Problem O: B C++时间类的运算符重载
- Visual C# 2010 Recipes A Problem-Solution Approach 代码6——运算符重载
- 第十五周oj刷题——Problem M: C++习题 矩阵求和--重载运算符
- OJ第三批——Problem J:C++复数运算符重载(+与<<)
- OJ第三批——Problem U:编程题:运算符重载---矩阵求和
- Python——运算符重载(2)
- Codeforces Round #232 (Div. 2) D(运算符重载)
- 小学Git 和 GitHub
- 指针数组,数组指针,存放数组指针的数组,指向存放数组指针数组的指针,函数指针,函数指针数组,指向函数指针数组的指针
- android运行时权限解决办法(含有申请权限已授权、权限被拒绝、权限勾选不在提示的回调)
- unity 控件生命周期
- App开机启动
- Problem D: 二进制类(2)——运算符重载-hzauoj
- 最小生成树_Conscription(POJ.3723)
- 【深入Java虚拟机】之八:Java垃圾收集机制
- react+webpack入门(2)
- Spring Security 与Spring Boot结合的认证授权机制流程(二)
- JAVA(android)序列化与反序列化工具类
- 类变量、成员变量、实例变量、局部变量、静态变量、全局变量的定义与区别
- Sublime Text 3 快捷键总结
- 【C语言常识】mdk:Pragmas recognized by the compiler