Question: The structure of the program has been established. Please help with implementing the following 2 functions (They are in bold below in the code. thank
The structure of the program has been established. Please help with implementing the following 2 functions (They are in bold below in the code. thank you):
#include
#include
#include
using namespace std;
int binary_to_decimal_signed(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: the decimal integer that is represented by s in two's complement
string signed_extension(string s);
// precondition: s is a string that consists of only 0s and 1s that is at most 16 bits
// postcondition: a 16 bit string has been returned as signed extension of s. For instane, if s = "0101" then
// return value will be "00000000000000000101" total 12 0s are added in front of s
string decimal_to_binary_signed(int n);
// precondition: n is an integer
// postcondition: ns two's complement binary representation is returned as a string of 0s and 1s
string add_binaries_signed(string b1, string b2);
// precondition: b1 and b2 are strings that consists of 0s and 1s at most 32 bits, i.e.
// b1 and b2 are two's complement binary representations of two integers. "0" is 0, "1" is still postive 1
// However, "10" will be consider as "1111111111111110" as -2
// postcondition: the sum of b1 and b2 is returned as a 32 bits two's complement reprentation.
// For instance, if b1 = 1101 (-3), b2 = 01 (+1), then the return value is 1111111111111110 (-2)
string twos_complement(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: two's complement of s is returned as an 16 bits binary integer. For instance, if s = "1101", then
// return value will be "1111111111111101"
int binary_to_decimal(string s);
// precondition: s is a string that consists of only 0s and 1s
// postcondition: the positive decimal integer that is represented by s
string decimal_to_binary(int n);
// precondition: n is a positive integer
// postcondition: ns 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
void menu();
// display the menu. Student shall not modify this function
int grade();
// returns an integer that represents the students grade of this projects.
// Student shall NOT modify
bool is_binary(string b);
// returns true if the given string s consists of only 0s and 1s; false otherwise
bool test_binary_to_decimal_signed();
// returns true if the students implementation of binary_to_decimal function
// is correct; false otherwise. Student shall not modify this function
bool test_decimal_to_binary_signed();
// returns true if the students implementation of decimal_to_binary function is correct; false otherwise. Student shall not modify this function
bool test_add_binaries_signed();
// which returns true if the students implementation of add_binaries function
// is correct; false otherwise. Student shall not modify this function
bool test_signed_extension();
// return true if the student's implementation of sign_extension function
// is correct; false otherwise. Student shall not modify this function
bool test_twos_complement();
// return true if the student's implementation of twos_complement function
// is correct; false otherwise. Student shall not modify this function
int main()
{
int choice;
string b1, b2;
int x, score;
do {
// display menu
menu();
cout
cin >> choice;
// based on choice to perform tasks
switch (choice) {
case 1:
cout
cin >> b1;
if (!is_binary(b1))
cout
else
cout
break;
case 2:
cout
cin >> x;
cout
break;
case 3:
cout
cin >> b1 >> b2;
if (!is_binary(b1) || !is_binary(b2))
cout
else
cout
break;
case 4:
cout
cin >> b1;
cout
break;
case 5:
cout
cin >> b1;
cout
break;
case 6:
score = grade();
cout
cout
break;
case 7:
cout
break;
default:
cout
break;
}
} while (choice != 7);
return 0;
}
string signed_extension(string s) {
// you implement this one first
return "0";
}
int binary_to_decimal_signed(string s) {
// you implement this one third
return 0;
}
string decimal_to_binary_signed(int n) {
// you implement this one fourth
int i, a[100];
for (i = 0; n>0; i++)
{
a[i] = n % 2;
n = n / 2;
}
cout
for (i = i - 1; i >= 0; i--)
{
cout
}
return 0;
}
return "0";
}
string add_binaries_signed(string b1, string b2) {
// you implement this one fifth
return "0";
}
string twos_complement(string s) {
// you implement this one second
return "0";
}
int binary_to_decimal(string s) {
assert(is_binary(s));
int result = 0;
for (int i = 0; i
result = result * 2 + (s[i] - 48);
return result;
}
string decimal_to_binary(int n) {
if (n == 0) return string("0"); // specila case 0
string result = "";
while (n > 0) {
result = string(1, (char)(n % 2 + 48)) + result; // add last digit of n in front of the result
n = n / 2;
}
return result;
}
string add_binaries(string b1, string b2) {
// you implement this
assert(is_binary(b1) && is_binary(b2));
string result = "";
int carry = 0;
int i1 = (int)b1.length() - 1;
int i2 = (int)b2.length() - 1;
while (i1 >= 0 || i2 >= 0)
{
int d1 = 0, d2 = 0;
if (i1 >= 0) d1 = b1[i1] - 48;
if (i2 >= 0) d2 = b2[i2] - 48;
int sum = carry + d1 + d2; // signle digit sum
carry = sum / 2; // carry is 1 if sum is 2 or 3; 0 otherwise
result = string(1, (char)(48 + sum % 2)) + result;
i1--;
i2--;
}
if (carry != 0)
result = "1" + result;
return result;
}
void menu()
{
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
}
int grade() {
int result = 0;
// binary_to_decimal function worth 2 points
if (test_binary_to_decimal_signed()) {
cout
result += 2;
}
else
cout
// decinal_to_binary_signed function worth 1 points
if (test_decimal_to_binary_signed()) {
cout
result += 1;
}
else
cout
// add_binaries function worth 2 points
if (test_add_binaries_signed()) {
cout
result += 2;
}
else
cout
// signed_extension function worth 1 point
if (test_signed_extension()) {
cout
result += 1;
}
else
cout
// twos_complement function worth 2 point
if (test_twos_complement()) {
cout
result += 2;
}
else
cout
return result;
}
bool is_binary(string s) {
for (int i = 0; i
if (s[i] != '0' && s[i] != '1') // one element in s is not '0' or '1'
return false; // then it is not a binary nunber representation
return true;
}
bool test_binary_to_decimal_signed() {
if (binary_to_decimal_signed("0") != 0 || binary_to_decimal_signed("1") != -1 || binary_to_decimal_signed("01") != 1)
return false;
if (binary_to_decimal_signed("010") != 2 || binary_to_decimal_signed("10") != -2)
return false;
if (binary_to_decimal_signed("01101") != 13 || binary_to_decimal_signed("1101") != -3)
return false;
return true;
}
bool test_decimal_to_binary_signed() {
if (decimal_to_binary_signed(0) != "0000000000000000" || decimal_to_binary_signed(1) != "0000000000000001")
return false;
if (decimal_to_binary_signed(-1) != "1111111111111111")
return false;
if (decimal_to_binary_signed(-2) != "1111111111111110" || decimal_to_binary_signed(-13) != "1111111111110011")
return false;
return true;
}
bool test_add_binaries_signed() {
if (add_binaries_signed("0", "0") != "0000000000000000") return false;
if (add_binaries_signed("0", "110101") != "1111111111110101") return false;
if (add_binaries_signed("1", "110111") != "1111111111110110") return false;
if (add_binaries_signed("101", "111011") != "1111111111111000") return false;
return true;
}
bool test_signed_extension() {
if (signed_extension("1") != "1111111111111111" || signed_extension("0") != "0000000000000000") return false;
if (signed_extension("10101") != "1111111111110101" || signed_extension("0101") != "0000000000000101") return false;
return true;
}
bool test_twos_complement() {
if (twos_complement("1") != "0000000000000001" || twos_complement("0") != "0000000000000000") return false;
if (twos_complement("01") != "1111111111111111" || twos_complement("10") != "0000000000000010") return false;
if (twos_complement("10101") != "0000000000001011") return false;
return true;
}
_________________________
Part of the output should look like this example :

Menu * 1. Binary to Deci * 2. Decinal to Binary 3. Add two Binaries * * 4. Signed extension *5. Two's complement * 6. Grade * 7. Quit Enter your choice: 1 Enter a binary string: 1001101 Its decimal value is: -51 (the two's complement of 1001101 is 0110011 which is positive 51. So 1001101 is -51) Menu * 1. Binary to Deci * 2. Decinal to Binary * 3. Add two Binaries * 4. Signed extension 5. Two's complement * 6. Grade * 7. Quit Enter your choice: 2 Enter an integer: -51 Its binary representation is: 1111111111001101 (51 is 0110011. So -51 is 1001101. Use sign
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
