用链式栈进行数制转换
运行不了,麻烦各位仁友指点一下……#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}node,*link;
typedef struct{
link top;
link base;
}*stack;
int initstack(stack s)
{s->base=(link)malloc(sizeof(node));
if(!s->base) return(-1);
s->top=s->base;
s->top->next=NULL;
return 1;
}
int stackempty(stack s)
{if(s->top==s->base) return 1;
else return 0;
}
int push(stack s,int e)
{link p;
p=(link)malloc(sizeof(node));
if(!p) return(-1);
p->data=e;
p->next=s->top;
s->top=p;
return 1;
}
int pop(stack s,int *e)
{if(!s->top) return 0;
link p;
p=s->top;
*e=p->data;
s->top=p->next;
free(p);
return *e;
}
main()
{
int n,i,a;
stack s;
initstack(s);
printf("请输入你要转换的自然数:");
scanf("%d",&n);
printf("请输入你要转换成的进制数:");
scanf("%d",&i);
while(n)
{
push(s,n%i);
n=n/i;
}
while(!stackempty(s))
{
pop(s,&a);
printf("%d",a);
}
}