Answered step by step
Verified Expert Solution
Question
1 Approved Answer
// Project description // Your Name // Date starts the project #include #include #include using namespace std; int binary_to_decimal_signed(string s); // precondition: s is a
// Project description // Your Name // Date starts the project #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 > choice; // based on choice to perform tasks switch(choice){ case 1: cout > b1; if(!is_binary(b1)) cout > x; cout > b1 >> b2; if(!is_binary(b1) || !is_binary(b2)) cout > b1; cout > b1; cout 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 Objectives: 1. Demonstrate basic programming logic 2. Demonstrate how to convert between integers and their binary representation in two's complement format 3. Demonstrate how to add two binary numbers that are in two's complement format Problem Description: Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following five functionalities: 1. Covert a binary string in two's complement format to corresponding integers 2. Convert an integer to its binary representation in two's complement format 3. Add two binary numbers, both numbers are represented as a string of Os and 1s 4. Provide sign extension for a binary number 5. Provide two's complement for a binary nunber This is an update of Project One. To reduce student work load, a start file CSCIProjTwo Handout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following five functions: 1. int binary_to_decimal_signed(string b); // precondition: b is a string that consists of only Os and 1s // postcondition: the decimal integer that is represented by b 2. string decimal_to_binary_signed (int n); // precondition: n is an integer // postcondition: n's binary representation is returned as a string of Os and 1s 3. string add_binaries_signed(string b1, string b2); // precondition: b1 and b2 are strings that consists of Os and 1s, i.e. b1 and b2 are binary two's complement representations of two integers // postcondition: the sum of b1 and b2 is returned. For instance, if b1 = "11", b2 = "01", then the return value is "100" 4. string signed_extension(string b); // precondition: s is a string that consists of only Os 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 // Os are added in front of s 5. string twos_complement(string s); // precondition: s is a string that consists of only Os 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" The following functionality has been provided. Please notice that three functions from Project One are provided. The idea is your new function will somehow call corresponding one in these three functions to do the work: 1. main function which presents the execution logic of the whole program 2. void menu(); which display the menu of this binary calculator 3. int binary_to_decimal(string b); // precondition: b is a string that consists of only Os and 1s // postcondition: the positive decimal integer that is represented by b 4. string decimal_to_binary(int n); // precondition: n is a positive integer // postcondition: n's binary representation is returned as a string of Os and 1s 5. string add_binaries(string b1, string b2); // precondition: b1 and b2 are strings that consists of Os 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" 6. bool is Binary(string s); which returns true if the given string s consists of only Os and 1s; false otherwise 7. int grade(); which returns an integer that represents the student's grade of this projects. 8. bool test_binary_to_decimal() which returns true if the student's implementation of binary_to_decimal function is correct; false otherwise 9. bool test_decimal_to_binary() which returns true if the student's implementation of decimal_to_binary function is correct; false otherwise 10. bool test_add_binaries which returns true if the student's implementation of add_binaries function is correct; false otherwise Work Process: Student shall download the CSC1365ProjTwo Handout.cpp file, saved as Your NameCSC1365 ProjTwo.cpp, and add it to his/her C++ project. Please notice, at this point, your project shall be able to run. However, your project will not pass any test. i.e. if you choose option 6 from menu, it will shows that you get 0 out of 10. Student shall NOT change any name of the declared functions or the given implemented functions. Student shall only implement the bodies of three functions that need to be implemented. Please see sample run at the end of this file. Due Date: It is specified in syllabus and is announced on Blackboard Sample Run: The highlighted ones are user's input. The red part is explanation of result, which doesn't show up in sample run output ****************************** * Menu * * 1. Binary to Decimal * * 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 Decimal * 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 extension to sixteen bits, it is 1111111111001101) ****************************** Menu * * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 3 Enter two binary numbers, separated by white space: 1001101 01101 The sum is: 1111111111011010 (both numbers get sign extension as 1111111111001101 and 0000000000001101. Then add them bits by bits with carry to get 1111111111011010) ****************************** * * * Menu * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * 4. Signed extension * 5. Two's complement * 6. Grade * 7. Quit * ** *********************** Enter your choice: 4 Enter a binary number: 1001101 Its signed extension to 16 bits is: 1111111111001101 (In fact, this one must be right before the first three options can be right) ****************************** * Menu * 1. Binary to Decimal * * 2. Decinal to Binary * * 3. Add two Binaries * * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit **kkkkkkkkkkkkkkkkkkkkkkkkkkkkk Enter your choice: 5 Enter a binary number: 1001101 Its two's complement is: 0000000000110011 In fact, this one must be right before the first three options can be right) ****************************** * Menu * 1. Binary to Decimal * 2. Decinal to Binary * * *3. Add two Binaries * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 6 binary_to_decimal_signed function pass the test decimal_to_binary_signed function passed the test add_binaries_signed function passed the test sign_extension function passed the test twos_complement function passed the test If you turn in your project on blackboard now, you will get 8 out of 10 Your instructor will decide if one-two more points will be added or not based on your program style, such as good commnets (1 points) and good efficiency (1 point) ****************************** * Menu * * 1. Binary to Decimal * 2. Decinal to Binary * * 3. Add two Binaries * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 7 Thanks for using binary calculator program. Good-bye Program ended with exit code: 0 Objectives: 1. Demonstrate basic programming logic 2. Demonstrate how to convert between integers and their binary representation in two's complement format 3. Demonstrate how to add two binary numbers that are in two's complement format Problem Description: Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following five functionalities: 1. Covert a binary string in two's complement format to corresponding integers 2. Convert an integer to its binary representation in two's complement format 3. Add two binary numbers, both numbers are represented as a string of Os and 1s 4. Provide sign extension for a binary number 5. Provide two's complement for a binary nunber This is an update of Project One. To reduce student work load, a start file CSCIProjTwo Handout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following five functions: 1. int binary_to_decimal_signed(string b); // precondition: b is a string that consists of only Os and 1s // postcondition: the decimal integer that is represented by b 2. string decimal_to_binary_signed (int n); // precondition: n is an integer // postcondition: n's binary representation is returned as a string of Os and 1s 3. string add_binaries_signed(string b1, string b2); // precondition: b1 and b2 are strings that consists of Os and 1s, i.e. b1 and b2 are binary two's complement representations of two integers // postcondition: the sum of b1 and b2 is returned. For instance, if b1 = "11", b2 = "01", then the return value is "100" 4. string signed_extension(string b); // precondition: s is a string that consists of only Os 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 // Os are added in front of s 5. string twos_complement(string s); // precondition: s is a string that consists of only Os 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" The following functionality has been provided. Please notice that three functions from Project One are provided. The idea is your new function will somehow call corresponding one in these three functions to do the work: 1. main function which presents the execution logic of the whole program 2. void menu(); which display the menu of this binary calculator 3. int binary_to_decimal(string b); // precondition: b is a string that consists of only Os and 1s // postcondition: the positive decimal integer that is represented by b 4. string decimal_to_binary(int n); // precondition: n is a positive integer // postcondition: n's binary representation is returned as a string of Os and 1s 5. string add_binaries(string b1, string b2); // precondition: b1 and b2 are strings that consists of Os 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" 6. bool is Binary(string s); which returns true if the given string s consists of only Os and 1s; false otherwise 7. int grade(); which returns an integer that represents the student's grade of this projects. 8. bool test_binary_to_decimal() which returns true if the student's implementation of binary_to_decimal function is correct; false otherwise 9. bool test_decimal_to_binary() which returns true if the student's implementation of decimal_to_binary function is correct; false otherwise 10. bool test_add_binaries which returns true if the student's implementation of add_binaries function is correct; false otherwise Work Process: Student shall download the CSC1365ProjTwo Handout.cpp file, saved as Your NameCSC1365 ProjTwo.cpp, and add it to his/her C++ project. Please notice, at this point, your project shall be able to run. However, your project will not pass any test. i.e. if you choose option 6 from menu, it will shows that you get 0 out of 10. Student shall NOT change any name of the declared functions or the given implemented functions. Student shall only implement the bodies of three functions that need to be implemented. Please see sample run at the end of this file. Due Date: It is specified in syllabus and is announced on Blackboard Sample Run: The highlighted ones are user's input. The red part is explanation of result, which doesn't show up in sample run output ****************************** * Menu * * 1. Binary to Decimal * * 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 Decimal * 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 extension to sixteen bits, it is 1111111111001101) ****************************** Menu * * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 3 Enter two binary numbers, separated by white space: 1001101 01101 The sum is: 1111111111011010 (both numbers get sign extension as 1111111111001101 and 0000000000001101. Then add them bits by bits with carry to get 1111111111011010) ****************************** * * * Menu * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * 4. Signed extension * 5. Two's complement * 6. Grade * 7. Quit * ** *********************** Enter your choice: 4 Enter a binary number: 1001101 Its signed extension to 16 bits is: 1111111111001101 (In fact, this one must be right before the first three options can be right) ****************************** * Menu * 1. Binary to Decimal * * 2. Decinal to Binary * * 3. Add two Binaries * * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit **kkkkkkkkkkkkkkkkkkkkkkkkkkkkk Enter your choice: 5 Enter a binary number: 1001101 Its two's complement is: 0000000000110011 In fact, this one must be right before the first three options can be right) ****************************** * Menu * 1. Binary to Decimal * 2. Decinal to Binary * * *3. Add two Binaries * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 6 binary_to_decimal_signed function pass the test decimal_to_binary_signed function passed the test add_binaries_signed function passed the test sign_extension function passed the test twos_complement function passed the test If you turn in your project on blackboard now, you will get 8 out of 10 Your instructor will decide if one-two more points will be added or not based on your program style, such as good commnets (1 points) and good efficiency (1 point) ****************************** * Menu * * 1. Binary to Decimal * 2. Decinal to Binary * * 3. Add two Binaries * 4. Signed extension * 5. Two's complement * * 6. Grade * 7. Quit ****************************** Enter your choice: 7 Thanks for using binary calculator program. Good-bye Program ended with exit code: 0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started