[讨论]斑竹看过来--数值转化
这个程序是把十进制转化为八进制,编译成功,可是结果确是错误的.请高手看看,提出建议
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 100
typedef struct
{int *base;
int *top;
int stacksize;
}sqstack;
push(sqstack *s,int e)
{if(s->top-s->base>=s->stacksize)
{s->base=realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int));
if(!s->base)exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return OK;
}
pop(sqstack *s,int *e)
{if(s->top==s->base) return ERROR;
e=*--s->top;
return OK;
}
initstack(sqstack *s)
{s->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!s->base) exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACKINCREMENT;
return OK;
}
emptystack(sqstack *s)
{if(s->top==s->base)
return OK;
return ERROR;
}
void conversion()
{sqstack s;
int n,e;
initstack(&s);
scanf("%d",&n);
while(n)
{push(&s,n%8);
n=n/8;
}
while(!emptystack(&s))
{pop(&s,&e);
printf("%d",e);
}
}
int main()
{ conversion();
getch();
}