Data Structures Stacks in (ct) Write a program that can be used to convert a num
ID: 3879723 • Letter: D
Question
Data Structures Stacks in (ct) Write a program that can be used to convert a number from binary to decimal Your program must have a Binary class that implements the methods shown in Table 1 below Your Binary class must include a member variable of type BinStack that stores a binary number, putting the high order bit at the bottom of the stack and the low order bit at the top of the stack. RinStack must be a dynamic stack that implements the standard Stack operations: push, pop, and isEmp ng with a default constructor and a destructor. BinStack should only allow values of 0 and 1 to be added to the stack. Stores a binary value. passed in as a c-style string (null-terminated array of characters showValue Prints the currently stored value with the high-order bit first (on the left) and the low- order bit last (on the right). Non-destructive implementation getDecimal Returns the decimal equivalent of the currently stored value as an int. Non-destructive implementation Returns the hexadecimal equivalent of the currently stored value as a c-style string Non-destructive implementation Empties the current value Creates a Binary object with an empty value Default constructor 2nd Constructor Creates a Binary object with an initial value, passed in as a c-style string (null terminated array of characters Use the names as shown above Write a main program to test your Binary class. It should include a menu that allows the user to: enter a binary value (stored internally as a c-string), get the decimal equivalent of the currently stored value, get the hexadecimal equivalent of the currently stored value, or clear the current value Write your classes and main program. Use a separate h and cpp file for each class. You also need a separate CRR file for your main program. Compile, run and test your classes and program Your output should be well-organized, neat, and easy to readExplanation / Answer
#include <iostream>
using namespace std;
class binary
{
public:
int intInput;
binary(){
}
binary(int number){
intInput=number;
}
void setValue(){
intInput=1010;
}
int getValue(){
return intInput;
}
int getDecimal(int n)
{
int num = n;
int dec_value = 0;
// Initializing base value to 1, i.e 2^0
int base = 1;
int temp = num;
while(temp)
{
int last_digit = temp % 10;
temp = temp/10;
dec_value += last_digit*base;
base = base*2;
}
return dec_value;
}
};
int main ()
{
int bin;
cout << "Please enter a decimal: ";
cin >> bin;
binary(bin);
cout<<getDecimal(bin);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.