Problem F: 二进制类(1)__运算符重载 hzauoj
来源:互联网 发布:蛋鸡存栏量数据 编辑:程序博客网 时间:2024/05/17 12:03
Problem F: 二进制类(1)__运算符重载
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 7
[Submit][Status][Web Board]
Description
将一个16 位二进制数表示成0 和1 的字符序列,即用一个字符数组来存放这个
二进制数。在这个类中设置两个构造函数,一个是传递整数参数的,另一个是传递字符串参数的。因为用户在创建对象时传递的二进制数,可能是以整数形式给出,也可能是以数字串形式给出,系统应该都能接受。另外有一个类型转换函数int(),用来将类类型向整型转换。两个重载运算符“+”,“-”,用来完成两个二进制数之间的加减运算。
class binary { //定义二进制类
char bits[16]; //二进制字模数组
public:
binary(char *); //字符串参数构造函数
binary(int); //整型参数构造函数
friend binary operator +(binary,binary); //重载“+”
friend binary operator -(binary,binary); //重载“-”
operator int(); //类类型转换函数
void print();
};
主函数设计如下:
int main()
{
binary n1="1011";
binary n2=int(n1)+15;
binary n3=n1-binary(7);
n1.print();
n2.print();
n3.print();
cout<<int(n2)+5<<endl;
cout<<n2-binary(5)<<endl;
cout<<n3+binary(5)<<endl;
cout<<int(n3)-5<<endl;
return 0;
}
Input
Output
Sample Output
00000000000010110000000000011010000000000000010031219-1
HINT
#include<iostream>#include<string.h>using namespace std;class binary{public: binary(){ } binary(char *); binary(int n); friend binary operator + (const binary &b1, const binary &b2); friend binary operator - (const binary &b1, const binary &b2); operator int() { int temp = 0; for(int i = 0; i < 16; i++) temp = temp * 2 + bits[i] - '0'; return temp; } void print() { for(int i=0; i < 16; i++) { cout << bits[i]; } cout << endl; }private: char bits[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'; } 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';}binary operator +(const binary &b1, const binary &b2){ binary tb1 = b1; binary tb2 = b2; int temp1 = int(tb1); int temp2 = int(tb2); return binary(temp1 + temp2);// char temp[16];// for(int i=0; i<16; i++)// temp[i] = '0';// for(int i = 15; i >=0; i--)// if( temp[i] + b1.bits[i] + b2.bits[i] - '0' - '0' - '0' == 0 )// temp[i] = '0';// else if (temp[i] + b1.bits[i] + b2.bits[i] - '0' - '0' - '0' == 1)// temp[i] = '1';// else if (temp[i] + b1.bits[i] + b2.bits[i] - '0' - '0' - '0' == 2)// {// temp[i] = '0';// temp[i - 1] = '1';// }// else if (temp[i] + b1.bits[i] + b2.bits[i] - '0' - '0' - '0' == 3)// {// temp[i] = '1';// temp[i - 1] = '1';// }// return binary(temp);}binary operator-(const binary &b1, const binary &b2){ binary tb1 = b1; binary tb2 = b2; int temp1 = int(tb1); int temp2 = int(tb2); return binary(temp1 - temp2);// char temp[16];// char b1[16];// char b2[16];// for(int i = 0; i < 16; i++)// {// temp[i] = '0';// b1[i] = b1_.bits[i];// b2[i] = b2_.bits[i];// }// for(int i = 15; i >=0; i--)// if( b1[i] - b2[i] - '0' - '0' == 0 )// temp[i] = '0';// else if ( b1[i] - b2[i] - '0' - '0' == 1)// temp[i] = '1';// else if ( b1[i] - b2[i] - '0' - '0' == -1)// {// int j = i ;// while(b1[j] == '0' && j >= 0) j--;// b1[j] = '0';// temp[i] = '1';// for(int t = j - 1; t < i; t++)// b1[t] = '1';// }// return binary(temp);} int main(){ binary n1="1011"; binary n2=int(n1)+15; binary n3=n1-binary(7); n1.print(); n2.print(); n3.print(); cout<<int(n2)+5<<endl; cout<<n2-binary(5)<<endl; cout<<n3+binary(5)<<endl; cout<<int(n3)-5<<endl; return 0;}
之前一直想用注释部分,利用字符串来解决问题,结果没有出现想要的输出值,然后突然灵机一动想到可以想把二进制转化为十进制,然后在把十进制转化为二进制,结果问题很简单的解决了。
- Problem F: 二进制类(1)__运算符重载 hzauoj
- Problem D: 二进制类(2)——运算符重载-hzauoj
- C++__运算符重载
- 重载抽取运算f符<<
- 第八周C++上机报告(项目三__实现Time类中的运算符重载)
- Problem D: 整型数组运算符重载
- Problem A: 整型数组运算符重载
- Problem F: 向量的运算
- OJ第三批——Problem A :实现复数类中的加运算符重载【C++运算符重载】
- 第十七周oj刷题——Problem A: 实现复数类中的加运算符重载【C++运算符重载】
- Problem F: 时间类的加、减法赋值运算
- 类运算符重载
- 运算符重载1
- 运算符重载1
- 运算符重载(1)
- Problem F: 重载字符的加减法
- 第八周C++上机报告(项目3__分数运算符重载)
- 4-1 复数类的运算符重载--成员运算符重载、友元运算符重载
- 输出位数的填充C++
- Heaters
- HDOJ--2032 杨辉三角问题(水题)
- 在java中使用jxl操作excel
- Codeforces 801C Voltage Keepsake
- Problem F: 二进制类(1)__运算符重载 hzauoj
- 设计一个只能在堆上或栈上的类
- uva12545比特变换器
- Visual Studio 2010 使用
- 编译安装PHP,解决问题 Don't know how to define struct flock on this system, set --enable-opcache=no
- 对大数(BigInteger)进行开方运算
- C#之Switch分支语句
- 20153724——2
- 【LeetCode】Student Attendance Record I 解题报告