Question

Write a C++ program converting an 8-bit binary integer to its decimal and converting a decimal integer to its 8-bit binaries. You can create functions declared as
// ============================================================================
// Function: BinToDec
// Description: This function converts an 8-bit binary integer to its decimal
// Parameter:   s [IN] -- a C-string with 8-bit binary digits received
// Return:      A decimal integer value calculated from s
// ============================================================================

int BinToDec(char* s);

// ============================================================================
// Function: DecToBin
// Description: This function converts a decimal integer to its binary bits
// Parameter:   n [IN] -- a decimal integer received, less than 256
// Return:      A C-string with 8-bit binary digits calculated from n
// ============================================================================

char* DecToBin(int n);
The purpose is to make you understand the number system concept. So you have to manually implement the ideas without making use of some library function, like itoa(), atoi(), pow(), etc. that make the assignment meaningless. Only function allowed to use is strlen().
• For BinToDec(),
1. Multiplication complexity of O(n) is required
2. Using Horner's method is suggested
• For DecToBin(),
1. Correctly using memory to return char* is required
2. The method of dividing by 2 and taking remainders reversely is suggested
Then you call two functions in main() like this:
while (...)
{
    // Show menu and read case action
    ... ...
    switch (n)
    {
       case 1: // How to call BinToDec()
       {   
            // Prompt and Read input
            // Call BinToDec()
            // Output result
       }
       case 2: // How to call DecToBin()
       {            
            // Prompt and Read input
            // Call DecToBin()
            // Output result
       }
       default: // Check others
       {
            ... ...
       }
    }
}
Your program should be run like the following:
>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------
Erter your choice: 1
Please Enter 8-bit binary digits (e.g., 11110000): 10001001
The decimal integer of 10001001b is 137d

>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------
Erter your choice: 2
Please Enter a decimal integer less than 256: 137
The binary of 137d is 10001001b

>>> Please select the conversion type:
1. Binary to Decimal
2. Decimal to Binary
3. Exit
---------------------------------------   
Erter your choice: 3
Bye.
Press any key to continue . .

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <iostream>

char* DecToBin(int n);
int BinToDec(char* s);

using namespace std;

int main()
{
    int choice, val, decConvert;
    char *binary = new char[9];
    char *binConvert;
    while (true)
    {
   // Show menu and read case action
   cout << ">>> Please select the conversion type:" << endl;
   cout << "1. Binary to Decimal:" << endl;
   cout << "2. Decimal to Binary" << endl;
   cout << "3. Exit" << endl;
   cout << "--------------------------------------" << endl;
   cout << "Enter your choice: ";
   cin >> choice;
   switch (choice)
   {
case 1: // How to call BinToDec()
{   
cout << "Please Enter 8-bit binary digits (e.g., 11110000): ";
do{
binary[0] = getchar();
}
while(binary[0] != '0' && binary[0] != '1');
binary[8] = '\0';
for(int i = 1; i < 8; i++)
binary[i] = getchar();
decConvert = BinToDec(binary);
cout << "The decimal integer of " << binary << "b is " << decConvert << "d" << endl;
break;
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Data Processing in C++
Homework Solution
$25.00
Computer Science
Data Processing
C++ Programming
Reservoir Levels
Filters
Conditions
Dates
TSV
Excel
Stream
Objects
Variables
Image Editing in C++
Homework Solution
$75.00
Computer Science
C++ Programming
Image Editing
Binary Files
Destinations
Grayscale
Bitmaps
Pixels
Boundaries
Loops
Patterns
Instructions
C Programming Problems
Homework Solution
$13.00
Computer Science
C Programming
Variables
Pointers
Compilation
Integers
Addresses
Form Specifiers
Characters
Recursive Multiplication in C++
Homework Solution
$15.00
Computer Science
C++ Programming
Recursive Multiplication
Arguments
Functions
User Menu
Statements
Variables
C Programming Problems
Homework Solution
$13.50
Computer Science
C Programming
Linked List
Algorithms
Integers
Structures
Randomization
Nodes
Loops
Average Values
Strings
Pointers
Attributes
Get help from a qualified tutor
Live Chats