#include <iostream>
using namespace std;
#define SIZEOFSTACK 200;
//栈的定义
typedef struct
{
int *top;
int *base;
int stacksize;
}SqStack;
//初始化一个栈
int InitStack(SqStack &stack)
{
stack.base=new int;
if (!stack.base)
{
cout<<"存储初始化失败!"<<endl;
return 0;
}
stack.top=stack.base; //空栈
stack.stacksize=SIZEOFSTACK;
return 0;
}
//插入元素
bool Push(SqStack &stack,int value)
{
//栈满情况还未考虑!!
if (stack.top-stack.base>=stack.stacksize)
{
cout<<"栈满!无法插入元素!"<<endl;
return false;
}
*(stack.top)=value; //栈的top指针指向栈顶的上一个元素
stack.top++;
return true;
}
//删除元素
bool Pop(SqStack &stack,int &value) //使用value保存删除的栈的元素的值
{
if (stack.base==stack.top)
{
cout<<"栈为空,无法删除!"<<endl;
return false;
}
stack.top--;
value=*stack.top;
// value=*(--stack.top);
return true;
}
//判断栈是否为空
bool StackEmpty(SqStack &stack)
{
if (stack.base==stack.top)
{
return false;
}else
{
return true;
}
}
void main()
{
SqStack stack;
InitStack(stack);
int num;
cout<<"输入你需要转换的十进制数据:";
cin>>num;
int n;
cout<<"输入要转换为的进制:";
cin>>n;
while (num)
{
Push(stack,num%n);
num=num/n;
}
int k;
while (StackEmpty(stack))
{
Pop(stack,k);
cout<<k<<" ";
}
}