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
  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

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

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c...

    Needed in C please Write the implementation file, priority_queue.c, for the interface in the given header file, priority_queue.h. Turn in your priority_queue.c file and a suitable main program, main.c, that tests the opaque object. priority_queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a heap will not receive any credit. #ifndef...

  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • I need this to be in C Write the implementation file, priority queue.c, for the interface...

    I need this to be in C Write the implementation file, priority queue.c, for the interface in the given header file, priority queue.h. Turn in your priority queue.c file and a suitable main program, main.c, that tests the opaque object. priority queue.h is attached as a file to this assignment but is also listed here for your convenience. Your implementation file should implement the priority queue using a heap data structure. Submissions that implement the priority queue without using a...

  • Write a C++ program that will count the number of words and vowels in a sentence....

    Write a C++ program that will count the number of words and vowels in a sentence. Create a bool function called isVowel that accepts a character as a parameter and returns a true if it’s a vowel (aeiou) and false otherwise. Create an int function named countVowels that will accept a string variable as a parameter and will return the number of vowels in it. Call the isVowel function as part of this process. Write an int function named countWords...

  • Complete an Array-Based implementation of the ADT List including a main method and show that the...

    Complete an Array-Based implementation of the ADT List including a main method and show that the ADT List works. Draw a class diagram of the ADT List __________________________________________ public interface IntegerListInterface{ public boolean isEmpty(); //Determines whether a list is empty. //Precondition: None. //Postcondition: Returns true if the list is empty, //otherwise returns false. //Throws: None. public int size(); // Determines the length of a list. // Precondition: None. // Postcondition: Returns the number of items in this IntegerList. //Throws: None....

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

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