c++ 二进制问题请教
以下程序针对二进制补码算术中整数的算术运算问题,要求先确定一个bit pattern size ,然后第一个函数要把输入的两个整数转化为二进制输出,第二个函数把两个二进制数相加输出,第三个函数再把结果转化回十进制。
只使用c++字符串数据类型,不使用任何数组变量。不改变主程序也不更改函数名,实现缺失函数,使程序运行正确。可编写额外的帮助函数,从函数中调用但不能对程序做修改。
需要使用c++字符串数据类型来表示这两个二进制补码的表示形式,不能使用任何数组变量,可使用字符串变量作为一个整体或使用索引访问字符串中变量中的字符。
程序示例运行如下
输入位模式大小的正整数:5
输入整数A:9
输入整数B:-14
十进制 9 + -14 = -5
二进制补码9 是 01001
二进制补码-14 是 10010
01001和10010的二进制和是11011。
在二进制补码计算中,9+ -14=-5。
#include iostream>
#include cmath>
#include string>
using namespace std;
int main()
{
//Read in the bit pattern size
int L;
do {
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
}while (L <= 0);
//Read in two integers a and b
int a, b;
cout << "Enter an integer a ";
cin >> a;
cout << "Enter an integer b ";
cin >> b;
//Calculate the decimal arithmetic sum of a and b and print the result int
c1 = a + b;
cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
//Compute the two's complement representations of a and b
//Each integer must be represented in L-bits pattern
//Also these two's complement representations must be returned as string data types
string A = decimalToTwocomplementString(a, L);
string B = decimalToTwocomplementString(b, L);
//Print the two's complement representations of a and b
cout << "The two's complement of " << a << " is\t " << A << endl;
cout << "The two's complement of " << b << " is\t " << B << endl;
//Compute the binary sum of the two's complement representations of a and b
//The result must be returned as L-bit pattern string data type
string C = TwoComplementStringAddition(A, B);
//Print the two's complement representation binary sum
cout << "The binary sum of " << A << " and " << B << " is " << C << endl;
//Convert the two's complement representation binary sum to decimal and print
int c2 = TwoComplementStringToDecimal(C);
cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;
//Print some concluding results
if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
else
{
cout << c1 << " is not equal to " << c2 << endl;
cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
}
system("Pause");
return 0;
}
原程序拷屏如下:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string decimalToTwocomplementString(int num, int length) {
string binary;
int positive_num = abs(num);
while (positive_num != 0) {
if (num >= 0){
binary.insert(0,to_string(positive_num & 1));
positive_num >>= 1;
} else {
binary.insert(0,to_string(!(positive_num & 1)));
positive_num >>=1;
}
}
if (num < 0) {
for (reverse_iterator it = binary.rbegin(); it != binary.rend(); it++) {
if (*it == '1') {
*it = '0';
} else {
*it = '1';
break;
}
}
}
while (binary.length() < length) {
if (num >= 0) {
binary.insert(0, "0");
} else {
binary.insert(0, "1");
}
}
return binary;
}
string TwoComplementStringAddition (const string& a, const string& b) {
int carry = 0;
string result;
for (reverse_iterator ita = a.rbegin(), itb = b.rbegin(); ita != a.rend(); ita++, itb++) {
int num_a = *ita - '0';
int num_b = *itb - '0';
result.insert(0,to_string(num_a ^ num_b ^ carry));
if (num_a == 1 && num_b == 1) {
carry = 1;
} else if (num_a == 0 && num_b == 0) {
carry = 0;
}
}
return result;
}
int TwoComplementStringToDecimal(string binary) {
int result = 0;
string::iterator it = binary.begin();
bool negative = *it == '1';
if (negative) {
for (; it != binary.end(); it++) {
*it = *it == '1' ? '0' : '1';
}
for (reverse_iterator iterator = binary.rbegin(); iterator != binary.rend(); iterator++) {
if (*iterator == '1') {
*iterator = '0';
} else {
*iterator = '1';
break;
}
}
}
for (it = binary.begin(); it != binary.end(); it++) {
result += (*it - '0') * pow(2,distance(it,binary.end()) - 1);
}
return negative ? -result : result;
}
int main()
{
//Read in the bit pattern size
int L;
do {
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
}while (L <= 0);
//Read in two integers a and b
int a, b;
cout << "Enter an integer a ";
cin >> a;
cout << "Enter an integer b ";
cin >> b;
//Calculate the decimal arithmetic sum of a and b and print the result int
int c1 = a + b;
cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
//Compute the two's complement representations of a and b
//Each integer must be represented in L-bits pattern
//Also these two's complement representations must be returned as string data types
string A = decimalToTwocomplementString(a, L);
string B = decimalToTwocomplementString(b, L);
//Print the two's complement representations of a and b
cout << "The two's complement of " << a << " is\t " << A << endl;
cout << "The two's complement of " << b << " is\t " << B << endl;
//Compute the binary sum of the two's complement representations of a and b
//The result must be returned as L-bit pattern string data type
string C = TwoComplementStringAddition(A, B);
//Print the two's complement representation binary sum
cout << "The binary sum of " << A << " and " << B << " is " << C << endl;
//Convert the two's complement representation binary sum to decimal and print
int c2 = TwoComplementStringToDecimal(C);
cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;
//Print some concluding results
if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
else
{
cout << c1 << " is not equal to " << c2 << endl;
cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
}
return 0;
}
这个是整个程序的代码,实现了那三个函数,其他的都没有改动。
虽然我实现的方法不算是很聪明,但是应该还是很好理解的
这个有点难的。首先decimal可以以不同类型表示,也可以理解成文件描述符(file descriptor)。其次需要做索引解耦(index decoupling), 效果好坏需要工业界的验证。
11011是补码,反码就是11010,源码就是10101=-5,哪里不懂吗?
#include <string>
#include <stdlib.h>
string decimalToTwocomplementString(int a, int L)
{
string result = "";
while(L > 0)
{
result += std::toString(a % 2);
a /= 2;
L--;
}
}
int TwoComplementStringToDecimal(string C)
{
return strtol(C.c_str(), NULL, 2);
}
string TwoComplementStringAddition(string A, string B)
{
return decimalToTwocomplementString(TwoComplementStringToDecimal(A) + TwoComplementStringToDecimal(B));
}
PS:
1.题目中要求不能用数组变量,但没有规定不能用C函数,
所以函数TwoComplementStringToDecimal偷懒用了c_str()和strtol()。
如果不能用,你就得用String的枚举变量一个个算了,反正无论如何按这题的规定,运行效率都不会高。
2.要是支持C++11就更简单了,参考C++11 stol函数和fmt::FormatInt。
题主的意思应该是想实现这三个函数吧。我假设题主理解什么是原码补码反码,也知道怎么互相转换,但是难以用编程语言来描述这个过程。如果是这样,那可以看看我的答案。
看了题目,题目中只让用string
类型,连数组都不让用。所以题目的意思让自己来实现这些算法。虽然调用C++库可以非常轻松的实现这个要求,那出这道题也失去了意义。
首先是实现十进制的整数类型转换为字符串形式的二进制格式
这个最常见的就是采用*"除2取余,逆序排列"*法
string decimalToTwocomplementString(int num, int length) {
string binary;
int positive_num = abs(num);
//判断正负,正数的补码就是原码,而负数的补码是在原码的基础上按位取反再加1
while (positive_num != 0) {
//如果是正数,直接取其原码
if (num >= 0){
binary.insert(0,to_string(positive_num & 1));
positive_num >>= 1;
//如果是负数,先取它的反码
} else {
binary.insert(0,to_string(!(positive_num & 1)));
positive_num >>=1;
}
}
//如果是负数,将之前获取到的反码加1,得到补码
//由于这个是字符串,所以没有真正意义上的加1,而是根据二进制数的性质,从右向左遍历
//找到第一个是0的数,把这个0变成1,再把之前遍历到的所有1改成0,就相当于加1了
if (num < 0) {
for (reverse_iterator it = binary.rbegin(); it != binary.rend(); it++) {
if (*it == '1') {
*it = '0';
} else {
*it = '1';
break;
}
}
}
//如果当前的字符串长度小于输入的二进制位数
//是正数则前面补0,负数则前面补1
while (binary.length() < length) {
if (num >= 0) {
binary.insert(0, "0");
} else {
binary.insert(0, "1");
}
}
return binary;
}
接下来是两个字符串相加
string TwoComplementStringAddition (const string& a, const string& b) {
//代表是否有进位
int carry = 0;
string result;
//从右向左遍历这俩字符串
//因为这俩字符串长度一定是一样的,所以终止条件写谁都无所谓
for (reverse_iterator ita = a.rbegin(), itb = b.rbegin(); ita != a.rend(); ita++, itb++) {
//把当前遍历到的位的值转换成整型
int num_a = *ita - '0';
int num_b = *itb - '0';
//将当前这俩数值与进位数异或,获得当前位置相加后的结果
result.insert(0,to_string(num_a ^ num_b ^ carry));
//当这俩字符串当前数值都是1时,肯定发生进位
//而且在有进位的前提下,只要当前这俩数值不是0和0那么一定会再次发生进位
if (num_a == 1 && num_b == 1) {
carry = 1;
} else if (num_a == 0 && num_b == 0) {
carry = 0;
}
}
return result;
}
最后是把字符串转为十进制的整数类型
其实和第一个函数大同小异
int TwoComplementStringToDecimal(string binary) {
int result = 0;
string::iterator it = binary.begin();
//判断这个二进制数的符号位是1还是0
//来决定这个数是正还是负
bool negative = *it == '1';
//如果是负数,要把补码转换为原码
//和原码转补码是同样的过程
//这里我为了偷懒,把符号位也给变成0了,但实际上负数的原码符号位是1
if (negative) {
for (; it != binary.end(); it++) {
*it = *it == '1' ? '0' : '1';
}
for (reverse_iterator iterator = binary.rbegin(); iterator != binary.rend(); iterator++) {
if (*iterator == '1') {
*iterator = '0';
} else {
*iterator = '1';
break;
}
}
}
//加权求值
for (it = binary.begin(); it != binary.end(); it++) {
result += (*it - '0') * pow(2,distance(it,binary.end()) - 1);
}
return negative ? -result : result;
}
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
/*Implement code begin */
string decimalToTwocomplementString(int a, int L)
{
string result = "";
//In our computer, int type save as complement itself! So we can save it to string directly with enough length.
//For example in 32bit computer, -14 saved as 0xFFFFFFFFFFFFFFF2,
//We only need 5 length to record in this question which is 0x12 (= binrary10010)
//And as a positive number, 0x5(=01001) don't have sign bit.
while(L-- > 0) {
result += (a & 0x1) ? string("1"):string("0");
a >>= 1;
}
return result;
}
int TwoComplementStringToDecimal(string C)
{
int len = C.length();
int num = 0;
//Translate string to positive number
for(int i = 0; i < len; i++)
{
num <<= 1;
if('0' != C[i])
num |= 1;
}
if(len > 0 && '1' == C[0]) {
//It's negative number, we can change it back.
//in this case, our number is 0x12, we need to change back to 0xFFFFFFFFFFFFFFF2
num |= (~0 << len);
}
cout << "transfer" << C << "to" << num;
return num;
}
string TwoComplementStringAddition(string A, string B)
{
return decimalToTwocomplementString(TwoComplementStringToDecimal(A) + TwoComplementStringToDecimal(B), A.length());
}
/*Implement code End */
int main()
{
//Read in the bit pattern size
int L;
do {
cout << "Enter positive integer for the bit pattern size ";
cin >> L;
}while (L <= 0);
//Read in two integers a and b
int a, b;
cout << "Enter an integer a ";
cin >> a;
cout << "Enter an integer b ";
cin >> b;
//Calculate the decimal arithmetic sum of a and b and print the result int
int c1 = a + b;
cout << "In decimal " << a << " + " << b << " is " << c1 << endl;
//Compute the two's complement representations of a and b
//Each integer must be represented in L-bits pattern
//Also these two's complement representations must be returned as string data types
string A = decimalToTwocomplementString(a, L);
string B = decimalToTwocomplementString(b, L);
//Print the two's complement representations of a and b
cout << "The two's complement of " << a << " is\t " << A << endl;
cout << "The two's complement of " << b << " is\t " << B << endl;
//Compute the binary sum of the two's complement representations of a and b
//The result must be returned as L-bit pattern string data type
string C = TwoComplementStringAddition(A, B);
//Print the two's complement representation binary sum
cout << "The binary sum of " << A << " and " << B << " is " << C << endl;
//Convert the two's complement representation binary sum to decimal and print
int c2 = TwoComplementStringToDecimal(C);
cout << "In two's complement arithmetic, " << a << " + " << b << " is " << c2 << endl;
//Print some concluding results
if (c1 == c2) cout << c1 << " is equal to " << c2 << ". Good Job!" << endl;
else
{
cout << c1 << " is not equal to " << c2 << endl;
cout << "Either " << c1 << " cannot be represented by the given bit pattern OR we have made some mistake!" << endl;
}
//system("Pause");
return 0;
}
PS: 突然发现下午看题忘记填补码,而且include缺少stdlib.h,楼上代码应该是个解(不细看啦),先赞一个。
强迫症还是要把自己的解法改正写清楚,检查完符合题目,编译运行也过了.(linux g++ 5.4.0-6)
补码是原码反码后+1只是一种计算方法,带符号整形在内存中就是补码存在的,如题不需要这么大的数(32bit)去计算,那只要去掉符号扩展的位数就行了,反之加回即可。
如果有问题敬请读到的指出来看看,代码的乐趣在于能够聪明地使用它,换个思路简化些,Have Fun。
当然这个题目本身也有点小瑕疵啦,比如L取值如果小于能容纳的位数怎么办(大于等于是没问题的)。