#include <iostream>
usingnamespacestd;
constintSTACK_SIZE=100;
classstack{
private:
intcount;// number of items in the stack
intdata[STACK_SIZE];
public:
stack();
~stack();
voidpush(constintitem);// push an item on the stack
intpop(void);// pop item off the stack
};
stack::stack()// constructor
{
count=0;// zero the stack
}
stack::~stack(){}// default destructor
voidstack::push(constintitem)
{
if(count<STACK_SIZE)
{
data[count]=item;
++count;
}
elsecout<<"Overflow!\n";
}
intstack::pop(void)
{
if(count>0)
{
--count;
return(data[count]);
}
else
{
cout<<"Underflow!\n";
return0;
}
}
voidtoBinary(intnums);
voidtoOctal(intnums);
voidtoHex(intnums);
intmain()
{
intnums;
cout<<"Please enter a decimal: ";
cin>>nums;
cout<<"\nBinary format:: ";
toBinary(nums);
cout<<"\nOctal format:: ";
toOctal(nums);
cout<<"\nHexadecimal format::";
toHex(nums);
cout<<"\n";
return0;
}
voidtoBinary(intnum)
{
inttotal=0;
stackreverse;// declare a local stack!!!!!!!!!!!!!!!!!!
intctr=0;// declare a local counter!!!!!!!!!!!!!!!
while(num>0)
{
total=num%2;
num/=2;
//cout << total << " ";
reverse.push(total);// save to stack instead of printing!!!!!!!!!
ctr++;// count the number of digits saved!!!!!!!!!!!!
}
while(ctr>0)
{
cout<<reverse.pop()<<" ";
ctr--;
}
}
voidtoOctal(intnum)
{
inttotal=0;
stackreverse;// declare a local stack!!!!!!!!!!!!!!!!!!
intctr=0;// declare a local counter!!!!!!!!!!!!!!!
while(num>0)
{
total=num%8;
num/=8;
//cout << total << " ";
reverse.push(total);// save to stack instead of printing!!!!!!!!!
ctr++;// count the number of digits saved!!!!!!!!!!!!
}
while(ctr>0)
{
cout<<reverse.pop()<<" ";
ctr--;
}
}
voidtoHex(intnum)
{
intcounter,x,a,hex[100];
//char c[100];
for(counter=0;num!=0;counter++)
{
a=num%16;
hex[counter]=a;
num=num/16;
}
for(x=counter-1;x>=0;x--)
{
if(hex[x]==10)
{
cout<<"A";
}
elseif(hex[x]==11)
{
cout<<"B";
}
elseif(hex[x]==12)
{
cout<<"C";
}
elseif(hex[x]==13)
{
cout<<"D";
}
elseif(hex[x]==14)
{
cout<<"E";
}
elseif(hex[x]==15)
{
cout<<"F";
}
else
{
cout<<hex[x];
}
}
}
Sunday, 1 September 2013
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment