#include<stdio.h>
#include<malloc.h>
#define S_SIZE 100
typedef struct {
int *base;
int *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=(int *)malloc(S_SIZE*sizeof(int));
S.stacksize=S_SIZE;
S.top=S.base;
}
int StackEmpty(SqStack S)
{
if(S.base==S.top)
return 1;
else
return 0;
}
void GetTop(SqStack S,int &e)
{
e=*(S.top-1);
}
void Push(SqStack &S,int e)
{
if(S.top-S.base<S.stacksize)
*S.top++=e;
}
void Pop(SqStack &S,int &e)
{
if(S.base!=S.top)
e=*--S.top;
}
void convert(SqStack &S,int n)
{
int e;
InitStack(S);
while(n)
{
Push(S,n%2);
n=n/2;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
printf("\n");
}
int main()
{
int i,n;
SqStack s;
InitStack(s);
printf("输入一个十进制数:\n");
scanf("%d",&n);
printf("%转换为二进制后为:\n",n);
convert(s,n);
}