栈实现数制转换问题
[code=C/C++][/code]
#include<stdio.h>
#define MAX 50
typedef int ElemType;
typedef struct
{
ElemType data[MAX];
int top;
}sqstack;
int initstack(sqstack *&s)
{
(*s).top=-1;
return 0;
}
int stackempty(sqstack *s)
{
if(-1==(*s).top)
return 1;
return 0;
}
int stackfull(sqstack *s)
{
if(MAX-1==(*s).top)
return 1;
return 0;
}
int push(sqstack *&s,int e)
{
if(stackfull(s))
return -1;
(*s).top=(*s).top+1;
(*s).data[(*s).top]=e;
return 0;
}
int pop(sqstack *&s)
{
if(-1==s->top)
return -1;
printf("%d\n",(*s).data[(*s).top]); //结果显示
(*s).top=(*s).top-1;
return 0;
}
int main()
{
int n,r; //n为十进制整数 r为需要得到的进制数
int k;
sqstack *s;
initstack(s);
printf("请输入十进制整数n:\n");
scanf("%d",&n);
printf("请输入需要的进制数r:\n");
scanf("%d",&r);
while(n)
{
k=n%r;
push(s,k);
}
while(!stackempty(s))
{
pop(s);
}
return 0;
}
用VC编译执行 老出错 怎么修改啊。
我用的是顺序栈 实现数制转换
怎么改,求解释
尤其是顺序栈的初始化
[解决办法]
- C/C++ code
#include<stdio.h>#include <stdlib.h>#define MAX 50typedef int ElemType;typedef struct MY_STACK_{ ElemType data[MAX]; int top;}sqstack;int initstack(sqstack *&s){ (*s).top=-1; return 0;}int stackempty(sqstack *s){ if(-1==(*s).top) return 1; return 0;}int stackfull(sqstack *s){ if(MAX-1==(*s).top) return 1; return 0;}int push(sqstack *&s,int e){ if(stackfull(s)) return -1; (*s).top=(*s).top+1; (*s).data[(*s).top]=e; return 0;}int pop(sqstack *&s){ if(-1==s->top) return -1; printf("%d\n",(*s).data[(*s).top]); //结果显示 (*s).top=(*s).top-1; return 0;}int main(){ int n,r; //n为十进制整数 r为需要得到的进制数 int k; sqstack *s = (sqstack*)malloc(sizeof(MY_STACK_));; initstack(s); printf("请输入十进制整数n:\n"); scanf("%d",&n); printf("请输入需要的进制数r:\n"); scanf("%d",&r); while(n) { k=n%r; push(s,k); n /= r; } while(!stackempty(s)) { pop(s); } return 0;}