Question

In: Computer Science

Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...

Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:

  1. Covert a binary string to corresponding positive integers

  2. Convert a positive integer to its binary representation

  3. 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:

  1. 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

  1. 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

  1. 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”

Solutions

Expert Solution

#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<

               

}


Related Solutions

C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
Write a menu program to have the above options for the polynomials. Your menu program should...
Write a menu program to have the above options for the polynomials. Your menu program should not use global data; data should be allowed to be read in and stored dynamically. Test your output with the data below. Poly #1: {{2, 1/1}, {1, 3/4}, {0, 5/12}} Poly #2: {{4, 1/1}, {2, -3/7}, {1, 4/9}, {0, 2/11}} provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1....
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs...
Goals Practice conditional statements Description Write a program to simulate a menu driven calculator that performs basic arithmetic operations (add, subtract, multiply and divide). The calculator accepts two numbers and an operator from user in a given format: For example: Input: 6.3 / 3 Output: 2.1 Create a Calculator class that has a method to choose the right mathematical operation based on the entered operator using switch case. It should then call a corresponding method to perform the operation. In...
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT