In: Computer Science
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:
Covert a binary string to corresponding positive integers
Convert a positive integer to its binary representation
Add two binary numbers, both numbers are represented as a string of 0s and 1s
To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:
int binary_to_decimal(string b);
// precondition: b is a string that consists of only 0s and 1s
// postcondition: the positive decimal integer that is represented by b
string decimal_to_binary(int n);
// precondition: n is a positive integer
// postcondition: n’s binary representation is returned as a string of 0s and 1s
string add_binaries(string b1, string b2);
// precondition: b1 and b2 are strings that consists of 0s and 1s, i.e. b1 and b2 are binary
// representations of two positive integers
// postcondition: the sum of b1 and b2 is returned. For instance, if b1 = “11”, b2 = “01”, // then the return value is “100”
#include
#include
#include
using namespace std;
int binary_to_decimal(string b);
string decimal_to_binary(int n);
string add_binaries(string b1, string b2);
int binary_to_decimal(string b) {
int decimal_number=0;
int significant_digit = b.length()-1;
for(int index=0;index
if(b.at(index)=='1'){
decimal_number+=pow(2,significant_digit-index);
}
}
return decimal_number;
}
string decimal_to_binary(int n){
string binary_string="";
while(n!=0){
if(n%2==0){
binary_string="0"+binary_string;
}else{
binary_string="1"+binary_string;
}
n/=2;
}
return binary_string;
}
string add_binaries(string b1, string b2){
int b1_int = binary_to_decimal(b1);
int b2_int = binary_to_decimal(b2);
return decimal_to_binary(b1_int+b2_int);
}
int main(){
string b1="100";
string b2="100";
cout<
}