#define STACK_INIT_SIZE 8 #define STACKINCRMENENT 4 #include <stdio.h> #include<math.h> typedef struct{ int *base; int *top; int stacksize; }sqstack; typedef sqstack *stacklist; stacklist p; stacklist initstack (stacklist p) { p->base=(int *)malloc(STACK_INIT_SIZE *sizeof(int)); if(!p->base) { printf("overflow!\n"); exit(0); } p->top=p->base; p->stacksize=STACK_INIT_SIZE ; printf("succeed in applying for a room!\n"); return(p); } stacklist push (stacklist p,int m) { if(p->top-p->base>=p->stacksize) { p->base=(int *)realloc(p->base,(p->stacksize+STACKINCRMENENT)*sizeof(int)); if(!p->base) { printf("overflow leads to failure!\n"); exit(0); } p->top=p->base+p->stacksize; p->stacksize+=STACKINCRMENENT; } *p->top++=m; printf("\nok!\n"); return(p); } int stackempty(stacklist s) { if(s->top<=s->base) return(0); else return(1); } int pop(stacklist x) { int y; y=*--(x->top); return(y); } main() { stacklist L; int n,e; L=initstack(p); printf("please input a decimal number:"); scanf("%d",&n); if(n<=0) printf("error due to the wrong input!\n"); while(n) { push(L,n%8); n=n/8; } printf("the changed number is:\n"); while(stackempty(L)) { e=pop(L); printf("%d",e); } }

评论