Apr 8, 2013

GWC CS G175 C++ Assignment 9

Requirement
Create a program that will accept positive integers in decimal and display them in either binary, octal or hexadecimal. Use a switch statement to select the option (binary, octal, or hex) picked from the menu and make use of modulo (%) and divide operators. A sample run of the program is shown:
Enter a number in decimal:
254
Convert the number from decimal into:
0 – Binary
1 – Octal
2 – Hexadecimal
2
The number 254 (decimal) is FE in hexadecimal.
Store the base to convert to chosen by the user as an integer and use a while() loop that performs integer division and modulo operations on the number entered by the user. As the result would be reversed if printed in the order of the computation, your program will need the capability to reverse the order of the digits as part of the number conversion process. To do this, create a stack class based on the “last-in, first-out” concept that allows you to push (write) and pop (read) integers (code for a stack class (myStack) has been posted to the website for you to use). Using a stack object, you will push the results of your computation (loop using % and /) as you get them (one digit at a time, as an integer). You can refer to our document “Number Systems” (pages 4 and 5) that was posted on our website. Once done, you will pop the values from the stack to get them in the right order and use another loop to “cout” them. In this output loop (while “popping” the digits, you will need to handle the values (intBuffer) that may be greater then 9 when dealing with hexadecimal. This can be done using code similar to: 
if (intBuffer > 9)
cout << char('A' + intBuffer - 10); // displays digit > 9 as char
else
cout << intBuffer;
Disclaimer
  • There are many ways to solve a problem.
  • I do not post codes before the due date, since that does not help you.
  • Codes may be incorrect because I am also a learner like you. Use it at your discretion.
Guideline
#include <iostream>
using namespace std;
<myStack class code goes here>
// Declare a class to convert and display number
// Function that prompt user to input a variable
print "Enter a positive integer: "
set input integer to aNumber
while aNumber is less than or equal to 0
    prompt user to enter again
endwhile
// Function that computes, stores numbers to a temporary stack and prints out from stack
void DisplayNumber::getInput()
{
print "Enter a positive integer: "
set input integer to aNumber
while aNumber is less than or equal to 0
    prompt user to enter again
endwhile
}
//// Computes, stores and prints out
void Number::printOut(int baseNumber)
{
// Declare and initialize a stack
Push values into stack
while aNumber is greater than 0
    push aNumber % baseNumber to stack
    divide aNumber by baseNumber
endwhile
// Print values from stack in LIFO order
while aStack.getCount() is greater than 0
    pop numbers from stack and check with hexadecimal condition
    print out numbers
endwhile
int main()
{
// Create object of type Number
// Prompt user to input a positive integer
cout << "Convert the number from decimal into, please choose an option:" << endl;
cout << "1 - Binary" << endl;
cout << "2 - Octal" << endl;
cout << "3 - Hexadecimal" << endl;
cout << "0 - Exit" << endl;
// Prompt user to input a selection
// Use switch case to check select and call the according funtion to print out numbers based on baseNumber
}