c语言栈实现进制转换问题

以下是我测试的代码

#include  <stdio.h>
#include <iostream.h>
#define MAXSIZE 100
typedef int datatype;
typedef struct seqstack
{   datatype a[MAXSIZE];
    int top;
}seqstack;
void init(seqstack *s)
{
   s->top=-1;
}
int empty(seqstack s)
{
   return(s.top==-1?1:0);
}
void  push(seqstack  *s, datatype  x)
{  if(s->top==MAXSIZE-1)
   {  printf("溢出"); exit(1); }
   else
   {  s->top++; s->a[s->top]=x; }
}
datatype  pop(seqstack  *s )
{   datatype  x;
    if(s->top!=-1)
    {  x=s->a[s->top]; s->top--; return  x; }
    else
    {  printf("栈为空!!! "); exit(1); }
}

datatype getTop(seqstack *s)
{
    if(s->top!=-1)
    return s->a[s->top];
    else
    {
        printf("stack is empty!\n");
        exit(1); 
    }
}
void conversion(int n,int m)
{
    seqstack *s;
    int x;
    init(s);
    while( n )
    {
        push(s,n%m);
        n/m;
    }
    
    printf("转换成目的进制后:");
    while(empty(*s)!=1)
    {
        x = getTop(s);
        pop(s);
        if(x > 9){
            printf("%c",x+87);
        }
        else{
            printf("%d",x);
        }
    }
    printf("\n");
    
}
int main()
{
        int n,d;
        printf("请输入一个正的10进制整数:");
        scanf("%d",&n);
        printf("请输入要转换成的进制(2-16):");
        scanf("%d",&d);
        conversion(n,d);
}


运行时会这样

img

  • 这个函数这样子写,先前对栈的初始化应该有点问题的
    void init(seqstack *&s)
    {
    s=(seqstack *)malloc(sizeof(seqstack));
    s->top=-1;
    }
  • 进栈和出栈函数的形参*s,我觉得改成*&s(我觉得而已……)

  • 改好之后其实也还有些其他问题,我暂时也没有发现()

  • 问题1,cinversion函数中,while(n)循环,假如n是5的话,就是死循环。。

看了好久还是没弄好。。。