实验12
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define NO 0
#define ADD_SIZE 20
#define INIT_SIZE 10
typedef int Status;
typedef struct
{
int *top;
int *base;
int stacksize;
}SqStack;
Status InitStack(SqStack *S)
{
S->base=(int *)malloc(INIT_SIZE*sizeof(int));
if(!S->base)
printf("ERROR!\n");
S->top=S->base;
S->stacksize=INIT_SIZE;
return OK;
}
Status Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(int *)malloc((INIT_SIZE+ADD_SIZE)*sizeof(int));
if(!S->base)
printf("ERROR!\n");
S->top=S->base+S->stacksize;
S->stacksize+=ADD_SIZE;
}
*S->top++=e;
return OK;
}
Status Pop(SqStack *S)
{
int e;
e=*--S->top;
return e;
}
int main()
{
int N;
int m;
int out_stack_value;
SqStack S;
InitStack(&S);
printf("输入十进制数:");
scanf("%d",&N);
while(N!=0)
{
m=N%8;
Push(&S,m);
N=N/8;
}
while(S.top-S.base>0)
{
out_stack_value=Pop(&S);
printf("%d",out_stack_value);
}
printf("为八进制数\n");
return 0;
}
浙公网安备 33010602011771号