Question

Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities:

  1. Provide sign extension for a binary number
  2. Provide two’s complement for a binary nunber
  1. string signed_extension(string b);

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

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

                    

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

  1. bool isBinary(string s); which returns true if the given string s consists of only 0s and 1s; false otherwise
  2. int grade(); which returns an integer that represents the student’s grade of this projects.
  3. bool test_binary_to_decimal() which returns true if the student’s implementation of binary_to_decimal function is correct; false otherwise
  4. bool test_decimal_to_binary() which returns true if the student’s implementation of decimal_to_binary function is correct; false otherwise
  5. bool test_add_binaries which returns true if the student’s implementation of add_binaries function is correct; false otherwise

Here's the sample code already given I only need help implementing the

  1. Provide sign extension for a binary number
  2. Provide two’s complement for a binary number

using namespace std;

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
   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";
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

-----------------------------------------------string signed_extension(string b);-------------------------------------

string signed_extension(string b){


   //find the length
   int number_of_bits=b.length();
   //to store new string
   string signed_extension_b="";
   //if positive
   if(b[0]=='0'){


       //extending the bits
       for(int i=0;i<(16-number_of_bits);i++){
           signed_extension_b=signed_extension_b+'0';
       }
       signed_extension_b=signed_extension_b+b;
       return signed_extension_b;


   }
   //if negative
   if(b[0]=='1'){


       for(int i=0;i<(16-number_of_bits);i++){
           signed_extension_b=signed_extension_b+'1';
       }
       signed_extension_b=signed_extension_b+b;
       return signed_extension_b;


   }


}

----------------------------------------------string twos_complement(string s);-----------------------------------------------

string twos_complement(string s){


   //s should be at most 16 bits
    int number_of_bits=s.length();
    string ones_complement="";
    string twos_complement_new="";
    string twos_complement_new_temp="";
    string binary_in16bit_form="";
    //conversion to 16 bits
    if(number_of_bits<16){
    /*updated
           */
           binary_in16bit_form=s=signed_extension(s);
       }
       else{
         
          binary_in16bit_form=s;
       }
    //find 1s complement
    for(int i=0;i<16;i++){
        //finding the complement
        ones_complement+=(binary_in16bit_form[i]=='0')?'1':'0';
       }
       twos_complement_new_temp=ones_complement;
       //finding 2s complement
       //adding 1 to 1s complement
       twos_complement_new_temp=twos_complement_new=add_binaries(ones_complement,"1");
       //updated
       if(twos_complement_new_temp.length()>16){
           //makes it 16bit
           twos_complement_new="";
           for(int i=1;i<twos_complement_new_temp.length();i++){
               twos_complement_new+=twos_complement_new_temp[i];
           }
       }
       return twos_complement_new;


      
}

untitled-Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Praject Preferences Help itled string signed_ex

untitled-Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Praject Preferences Help ntitled string twos_co

CAUsers\svs97\ Deskto p\string Binary.exe Signed extension of 10101 2s Complement 1000 : 1111111111111000 1111111111110101 Pr

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT