Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <time.h>
using namespace std;
//===============================CLASSE DEFINITION FOR STACK============================================================
template <class T, int n>
class stack
{
private: T elt[n];
int counter;
public:
void clearstack() { counter = -1; } //INITIALIZE COUNTER
bool emptystack() { return counter==-1?true:false; } //EVALUATION IF STACK IS EMPTY
bool fullstack() { return counter== n-1?true:false; } //EVALUATION IF STACK IS FULL
void push(T x) { counter++; elt[counter] = x; } //FUNCTION TO ADD TO STACK
T pop() { T x; x= elt[counter]; counter--;return x; } //FUNCTION TO POP THE STACK
};
//======================================================================================================================
int main()
{
int q, m, p ; //THREE VARIABLES M Q AND P DECLARED FOR PUSH AND POP OPERATIONS
char j; //VARIABLE J TO DETERMINE WETHER TO CONTINUE PROGRAM OR NOT
char k; //VARIABLE K TO DETERMINE TYPE OF OPERATION BINARY OCTAL OR HEXADECIMAL
char z; //VARIALBE Z TO USE IN HEXADECIMAL OPERATIONS SPECIFICALLY
time_t t;
time(&t);
stack <int, 17> s; //OBJECT OF STACK TYPE DECLARED
while(j!='n') //BEGINNING OF WHILE LOOP TO CONTINUE PROGRAM UNTIL VARIABLE J IS EQUAL TO 'n'
{
cout<<"===========================Conversion Menu================================"<<endl;
cout<<"1.Decimal to binary"<<endl;
cout<<"2.Decimal to octal"<<endl;
cout<<"3.Decimal to hexadecimal"<<endl;
cout<<"4.Exit"<<endl;
cout<<"time and date is"<<ctime(&t);
cin>>k; //REQUEST TO USER TO ENTER THE TYPE OF OPERATION
switch(k) //SWITCH STATEMENT USED TO RUN TYPE OF OPERATION CHOSEN BY USER
{
//=======================================================================================================================
case '1': //CASE 1 FOR BINARY OPERATIONS
s.clearstack();
cout<<"Enter a number to convert: ";
cin>>m;
while( m!=0 &&!s.fullstack() )
{
q = m % 2;
s.push(q);
m = m / 2;
}
while(!s.emptystack())
{
p = s.pop();
cout<<p;
}
cout<<"\nPRESS 'y' TO CONTINUE: ";
cin>>j;
while(j !='y')
{
cout<<"\nYou entered an invalid response:";
cout<<"\nPlease re-enter your choice: ";
cin>>j;
}
break;
//=======================================================================================================================
case '2': //CASE 2 FOR OCTAL OPERATIONS
s.clearstack();
cout<<"Enter a number to convert: ";
cin>>m;
while( m!=0 &&!s.fullstack() )
{
q = m % 8;
s.push(q);
m = m / 8;
}
while(!s.emptystack())
{
p = s.pop();
cout<<p;
}
cout<<"\nPRESS 'y' TO CONTINUE: ";
cin>>j;
while(j !='y')
{
cout<<"\nYou entered an invalid response:";
cout<<"\nPlease re-enter your choice: ";
cin>>j;
}
break;
//=======================================================================================================================
case '3': //CASE 3 FOR HEXADECIMAL OPERATIONS
s.clearstack();
cout<<"Enter a number to convert: ";
cin>>m;
while( m!=0 &&!s.fullstack() )
{
z = m % 16;
if(z<10 || z>15) //IF STATEMENT TO DETERMINE WETHER OR NOT CHAR OR INT VALUE DISPLAYED
{
s.push(z);m = m / 16;
}
else if(z>=10 || z<=15)
{
switch (z)
{
case 10: z='A'; s.push(z); m = m / 16; break; ////////////////////////////////////////////////////
case 11: z='B'; s.push(z); m = m / 16; break; // EMBEDDED SWITCH STATEMENT TO CONVERT //
case 12: z='C'; s.push(z); m = m / 16; break; // FROM INTEGER TO CHAR VALUES FOR HEXADECIMAL //
case 13: z='D'; s.push(z); m = m / 16; break; // OUTPUT //
case 14: z='E'; s.push(z); m = m / 16; break; // //
case 15: z='C'; s.push(z); m = m / 16; break; ////////////////////////////////////////////////////
}
}
}
while(!s.emptystack())
{
p = s.pop();
if(p<9) //LAST IF STATEMENT TO DETERMINE IF OUTPUT IS CHAR OR INT
{
cout<<(int)p;
}
else if(p>9)
{
cout<<(char)p;
}
}
cout<<"\nPRESS 'y' TO CONTINUE: ";
cin>>j;
while(j !='y')
{
cout<<"\nYou entered an invalid response:";
cout<<"\nPlease re-enter your choice: ";
cin>>j;
}
break;
case '4':
return 0;
break;
}
}
}
this bit of code is a stack operation that I just put together no to long ago. It is used to convert decimal values to hexadecimal octal and binary values. It works like a charm. Be sure to trace the program line by line. The easiest way to trace this one is to examine one switch statement at a time.