利用栈输入十进制数显示相应的八进制数
#include<stdio.h>#include<stdlib.h>
#include<conio.h>
#define OK 1
#define ERROR 0;
#define stack_init_size 5
#define stackincrement 2
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
int initstack(SqStack*s)
{
s->base=(int*)malloc(stack_init_size*sizeof(int));
if(!s->base)exit(0);
s->top=s->base;
return OK;
}
int push(SqStack*s,int x)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(int*)realloc(s->base,(s->stacksize+stackincrement)*sizeof(int));
if(!s->base)exit(0);
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*s->top++=x;
return OK;
}
int pop(SqStack*s,int *x)
{
if(s->top==s->base) return ERROR;
x=*--s->top;
printf("%d",x);
return OK;
}
void main()
{
int N;
SqStack *s;
int*x;
clrscr();
initstack(s);
printf("\nplease input a number:");
scanf("%d",&N);
while(N)
{
push(s,N%8);
N=N/8;
}
printf("\n8jin zhi numbers is:");
while(s->top!=s->base)
{
pop(s,x);
}
getch();
}