Question

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:

  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”

                    

The following functionality has been provided:

  1. main function which presents the execution logic of the whole program
  2. void menu(); which display the menu of this binary calculator
  3. bool isBinary(string s); which returns true if the given string s consists of only 0s and 1s; false otherwise
  4. int grade(); which returns an integer that represents the student’s grade of this projects.
  5. bool test_binary_to_decimal() which returns true if the student’s implementation of binary_to_decimal function is correct; false otherwise
  6. bool test_decimal_to_binary() which returns true if the student’s implementation of decimal_to_binary function is correct; false otherwise
  7. bool test_add_binaries which returns true if the student’s implementation of add_binaries function is correct; false otherwise

You can't change any of the following code but what needs to be done in the three sections they must all return "0":

#include <iostream>
#include <string>
#include <cassert>

using namespace std;

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: n’s 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 student’s 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();
// returns true if the student’s implementation of binary_to_decimal function
// is correct; false otherwise. Student shall not modify this function

bool test_decimal_to_binary();
// returns true if the student’s implementation of decimal_to_binary function is correct; false otherwise. Student shall not modify this function

bool test_add_binaries();
// which returns true if the student’s implementation of add_binaries 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 << "Enter you choice: ";
        cin >> choice;
        // based on choice to perform tasks
        switch(choice){
            case 1:
                cout << "Enter a binary string: ";
                cin >> b1;
                if(!is_binary(b1))
                    cout << "It is not a binary number\n";
                else
                    cout << "Its decimal value is: " << binary_to_decimal(b1) << endl;
                break;
              
            case 2:
                cout << "Enter a positive integer: ";
                cin >> x;
                if(x <= 0)
                    cout << "It is not a positive integer" << endl;
                else
                    cout << "Its binary representation is: " <<decimal_to_binary(x) << endl;
                break;
              
            case 3:
                cout << "Enter two binary numbers, separated by white space: ";
                cin >> b1 >> b2;
                if(!is_binary(b1) || !is_binary(b2))
                    cout << "At least one number is not a binary" << endl;
                else
                    cout << "The sum is: " << add_binaries(b1, b2) << endl;
                break;
              
            case 4:
                score = grade();
                cout << "If you turn in your project on blackboard now, you will get " << score << " out of 10" << endl;
                cout << "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)" << endl;
                break;
              
            case 5:
                cout << "Thanks for using binary calculator program. Good-bye" << endl;
                break;
            default:
                cout << "Wrong choice. Please choose 1-5 from menu" << endl;
                break;
        }
      
    }while(choice != 5);
    return 0;
}

int binary_to_decimal(string s){
    // you implement this
    return 0;
}

string decimal_to_binary(int n){
    // you implement this
    return "0";
}

string add_binaries(string b1, string b2){
    // you implement this
    return "0";
}

void menu()
{
    cout << "******************************\n";
    cout << "*          Menu              *\n";
    cout << "* 1. Binary to Decimal       *\n";
    cout << "* 2. Decinal to Binary       *\n";
    cout << "* 3. Add two Binaries        *\n";
    cout << "* 4. Grade                   *\n";
    cout << "* 5. Quit                    *\n";
    cout << "******************************\n";
}

int grade(){
    int result = 0;
    // binary_to_decimal function worth 3 points
    if(test_binary_to_decimal()){
        cout << "binary_to_decimal function pass the test" << endl;
        result += 3;
    }
    else
        cout << "binary_to_decimal function failed" << endl;
  
    // decinal_to_binary function worth 2 points
    if(test_decimal_to_binary()){
        cout << "decimal_to_binary function pass the test" << endl;
        result += 2;
    }
    else
        cout << "decimal_to_binary function failed" << endl;
    // add_binaries function worth 3 points
    if(test_add_binaries()){
        cout << "add_binaries function pass the test" << endl;
        result += 3;
    }
    else
        cout << "add_binaries function pass failed" << endl;
    return result;
}

bool is_binary(string s){
    for(int i = 0; i < s.length(); 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(){
    if(binary_to_decimal("0") != 0 || binary_to_decimal("1") != 1)
        return false;
    if(binary_to_decimal("010") != 2 || binary_to_decimal("10") != 2)
        return false;
    if(binary_to_decimal("01101") != 13 || binary_to_decimal("1101") != 13)
        return false;
    return true;
}

bool test_decimal_to_binary(){
    if(decimal_to_binary(0) != "0" || decimal_to_binary(1) != "1")
        return false;
    if(decimal_to_binary(2) != "10" || decimal_to_binary(13) != "1101")
        return false;
    return true;
}

bool test_add_binaries(){
    if(add_binaries("0", "0") != "0") return false;
    if(add_binaries("0", "110101") != "110101") return false;
    if(add_binaries("1", "110111") != "111000") return false;
    if(add_binaries("101", "111011") != "1000000") return false;
    return true;
}

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

-----------------------------------------binary_to_decimal(string s)-----------------------------------------

int binary_to_decimal(string s){


// you implement this
  
int decimal_value=0,i;
//initial value of base
int base_value=1;
int length_of_s=s.length();
for(i=length_of_s-1;i>=0;i--){
   if(s[i]=='1'){
       decimal_value=decimal_value+base_value;
       }
       base_value*=2;
   }
   return decimal_value;


}

------------------------------------string decimal_to_binary(int n)-----------------------------------------

string decimal_to_binary(int n){


// you implement this
string binary_num="";
int binary_number[40],i=0,j;

//Updated

if(n==0)

return "0";


while(n>0){
   //storing the remainder in array
   binary_number[i]=n%2;
   //dividing the number
       n/=2;
       i=i+1;
   }
   //in order to get the binary number
   //we have to reverse the array
   for(j=i-1;j>=0;j--){
       //concating to form a string
       // should include #include<sstream>
       std::string string_data;
       //declares string stream
       std::stringstream ss;
      
       ss<<binary_number[j];
       //converts the data to string
       string_data=ss.str();
       //concating to form a string of binary
       binary_num+=string_data;
   }
return binary_num;


}

--------------------------------------string add_binaries(string b1, string b2)------------------------------

string add_binaries(string b1, string b2){


// you implement this
//To store result of addition
string add_result="";
//stores the sum of digits
int sum_of_digits=0,len_of_b1,len_of_b2;
len_of_b1=b1.size()-1;
len_of_b2=b2.size()-1;
while(len_of_b1>=0 || len_of_b2>=0 || sum_of_digits==1){
   //find sum of two digits
   if(len_of_b1>=0){
       sum_of_digits+=b1[len_of_b1]-'0';
       }
       else{
           sum_of_digits+=0;
       }
      
       if(len_of_b2>=0){
       sum_of_digits+=b2[len_of_b2]-'0';
       }
       else{
           sum_of_digits+=0;
       }
      
       //calculated sum is 3 or 1
       //then ,concatenate the result as
       add_result=char(sum_of_digits%2+'0')+add_result;
      
       //then find the carry
       sum_of_digits=sum_of_digits/2;
       //then decrement len_of_b1 and len_of_b2
       //to go to next digits
       len_of_b1--;
       len_of_b2--;
      
   }
return add_result;


}

CAUsers\svs97\ Desktop\string Binary.exe Menu * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * 4. Grade

CAUsers\svs97\ Desktop\string Binary.exe Menu * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * 4. Grade

CAUsers\svs97\ Desktop\string Binary.exe Menu * 1. Binary to Decimal * 2. Decinal to Binary * 3. Add two Binaries * 4. Grade

-Sublime Text (UNREGISTERED) untitied Eile Edit Selection Find View Goto Tools Praject Preferenes Help ntitled 1 int binary_t

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

-Sublime Text (UNREGISTERED) untitied File Edit Selection Find View Goto Topls Project Preferences Help ntitled string add bi

Add a comment
Know the answer?
Add Answer to:
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...
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++ 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: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

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

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0...

    need help..... sudoku.h class sudoku { public: sudoku(); //default constructor //Postcondition: grid is initialized to 0 sudoku(int g[][9]); //constructor //Postcondition: grid = g void initializeSudokuGrid(); //Function to prompt the user to specify the numbers of the //partially filled grid. //Postcondition: grid is initialized to the numbers // specified by the user. void initializeSudokuGrid(int g[][9]); //Function to initialize grid to g //Postcondition: grid = g; void printSudokuGrid(); //Function to print the sudoku grid. bool solveSudoku(); //Funtion to solve the sudoku problem....

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • 1. Implement an algorithm to convert binary number into an integer. Binary number is represented as...

    1. Implement an algorithm to convert binary number into an integer. Binary number is represented as a string. Ex. int n = to_int("01010"); int to_int(const std::string& b) { } 2. Implement an algorithm to convert a decimal number to binary. The return type is string which holds the binary number as string. std::string to_binary(int n) { } 3. Implement a function to check if the number is positive or negative. The function should return true if number is positive. bool...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

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