#include <iostream>
#include <fstream>
using namespace std;
const int maxstack = 51;
class stack_type
{
public:
void clear_stack();
bool empty_stack();
bool full_stack();
void push (int numb);
void pop (int& numb);
int stack[maxstack];
int top;
};
void main()
{
stack_type remainder_stack;
int n, number, remainder;
char response;
remainder_stack.clear_stack();
do
{
cout << "Enter positive integer to convert to base 16: \n\n";
cin >> number;
n = number;
cout << number << "\n";
while (number != 0)
{
remainder = number % 16;
remainder_stack.push(remainder);
number /= 16;
if (remainder==10)
{
remainder='A';
}
else if (remainder==11)
{
remainder='B';
}
else if (remainder==12)
{
remainder='C';
}
else if (remainder==13)
{
remainder='D';
}
else if (remainder==14)
{
remainder='E';
}
else if (remainder==15)
{
remainder='F';
}
}
cout << "Base 16 representation of " << n << " is ";
while (!remainder_stack.empty_stack())
{
remainder_stack.pop(remainder);
cout << remainder;
}
cout << endl;
cout << "\nMore (Y or N)? :";
cin >> response;
}
while (response=='Y' || response=='y');
}
void stack_type::clear_stack()
{
top=0;
}
bool stack_type::empty_stack()
{
if (top==0)
return true;
else
return false;
}
bool stack_type::full_stack()
{
if (top==maxstack-1)
return true;
else
return false;
}
void stack_type::push (int numb)
{
top = top + 1;
stack[top] = numb;
}
void stack_type::pop (int& numb)
{
numb = stack[top];
top = top - 1;
}
Decimal to hexadcimal using stacks
Page 1 of 12 Replies - 191 Views - Last Post: 04 October 2012 - 09:00 PM
#1
Decimal to hexadcimal using stacks
Posted 04 October 2012 - 06:08 PM
My teacher wants us to use stacks to convert decimal to hexadecimal. I get the right answer but when the remainder is 10-15 it doesn't output the letter it does the number. I moved the if statements around and still didn't work.
Replies To: Decimal to hexadcimal using stacks
#2
Re: Decimal to hexadcimal using stacks
Posted 04 October 2012 - 06:16 PM
Any help would be greatly appreciated. Thank you
#3
Re: Decimal to hexadcimal using stacks
Posted 04 October 2012 - 09:00 PM
Look at the following snippet:
What is the purpose of your series of if/else/else if that ends with the first part of the above code? Why do you go through all these if/elses to assign a value to remainder and then replace that variable (remainder) with another value before you actually use the value, your pop() alters the parameter (remainder).
There is probably other problems with your printout because remainder is a int not a character.
Jim
else if (remainder==15)
{
remainder='F';
}
}
cout << "Base 16 representation of " << n << " is ";
while (!remainder_stack.empty_stack())
{
remainder_stack.pop(remainder);
cout << remainder;
}
What is the purpose of your series of if/else/else if that ends with the first part of the above code? Why do you go through all these if/elses to assign a value to remainder and then replace that variable (remainder) with another value before you actually use the value, your pop() alters the parameter (remainder).
There is probably other problems with your printout because remainder is a int not a character.
Jim
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|