#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;
}
这个是整个程序的代码,实现了那三个函数,其他的都没有改动。
虽然我实现的方法不算是很聪明,但是应该还是很好理解的