#include<iostream>
using namespace std;
const int StackSize=10;
template<class DataType>
class SeqStack
{
public:
SeqStack();
~SeqStack(){}
void Push(DataType x);
DataType Pop();
void Decimaltor(int num,int r);
private:
DataType data[StackSize];
int top;
};
template<class DataType>
SeqStack<DataType>::SeqStack()
{
top=-1; //初始化一个栈
}
template<class DataType> //入栈算法
void SeqStack<DataType>::Push(DataType x)
{
if(top==StackSize-1)throw"上溢";
top++;
data[top]=x;
}
template<class DataType> //出栈算法
DataType SeqStack<DataType>::Pop()
{
DataType x;
if(top==-1)throw"下溢";
x=data[top--];
return x;
}
template<class DataType>
void SeqStack<DataType>::Decimaltor(int num,int r) //进制转换算法
{
int k;
top=-1;
while(num!=0)
{
k=num%r; //得到余数
Push(k);
num=num/r; //得到商
}
while(top!=-1)
{
cout<<Pop();
}
}
int main()
{
int r,num;
SeqStack<int>S;
cout<<"请输入一个十进制的数字:";
cin>>num;
r=2;
cout<<"转换成2进制的数为:";
S.Decimaltor(num,r);
cout<<endl;
return 0;