利用进栈,出栈操作实现十进制数转换为K进制数
求各位大神看看这程序哪错了,执行结果很离谱,让人很无语利用进栈,出栈操作实现十进制数转换为K进制数
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct
{
char *base;
char *top;
int stacksize;
}Sqstack;
int StackInit(Sqstack *s)
{
s->base=(char *)malloc(STACK_INIT_SIZE *sizeof(int));
if(!s->base)
return 0;
s->top=s->base;
s->stacksize=STACK_INIT_SIZE;
return 1;
}
int Push(Sqstack *s,char e)
{
if(s->top-s->base>=s->stacksize)
{
s->base=(char *)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int));
if(!s->base)
return 0;
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=e;
return e;
}
int Pop(Sqstack *s,char e)
{
if(s->top==s->base)
return 0;
e=--*s->top;
return e;
}
int stackempty(Sqstack *s)
{
if(s->top==s->base)
return 1;
else
return 0;
}
int tf(Sqstack *s)
{
int n,i=0,k,m;
char a,e;
printf("输入要转化的十进制数:\n");
scanf("%d",&n);
printf("输入要转化为的进制:\n");
scanf("%d",&k);
printf("将十进制数%d转化为%d进制是:\n",n,k);
while(n)
{
m=n%k;
if(m<10)
a=48+m;
else
a=65+m-10;
Push(s,a);
n=n/k;
}
while(!stackempty(s))
{
Pop(s,e);
printf("%c",e);
}
printf("\n");
return 0;
}
int main()
{
Sqstack s;
StackInit(&s);
tf(&s);
return 0;
}