#include
#include
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW 0
typedef int Status,SElemType;
typedef struct
{
SElemType base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack S) //初始化,构造一个空栈
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW); //存储分配失败
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack S,SElemType e) //入栈
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW ); //存储分配失败
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack S,SElemType e) //出栈
{
if(S.top==S.base) //判断栈空
return ERROR;
e=--S.top;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return TRUE;
else return FALSE;
}
int main()
{
int n,m,e;
SqStack S;
InitStack(S);
printf("请输入一个非负十进制整数:");
scanf("%d",&n);
printf("请输入你要转换的进制:");
scanf("%d",&m);
while(n)
{
Push(S,n%m);
n=n/m;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
return 0;
}
用栈实现的进制转换,编译没问题,运行时总是停止运行
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 小灸舞 2017-04-24 06:38关注
你所有的函数都应该传入指针形式才行,你之前的写法形参和实参代表的并不是同一个变量,所以程序无法运行
#include<stdio.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define OVERFLOW 0 typedef int Status,SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; }SqStack; Status InitStack(SqStack *S) //初始化,构造一个空栈 { S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); if(!S->base) exit(OVERFLOW); //存储分配失败 S->top=S->base; S->stacksize=STACK_INIT_SIZE; return OK; } Status Push(SqStack *S,SElemType e) //入栈 { if(S->top-S->base>=S->stacksize) { S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); if(!S->base) exit(OVERFLOW ); //存储分配失败 S->top=S->base+S->stacksize; S->stacksize+=STACKINCREMENT; } *S->top++=e; return OK; } Status Pop(SqStack *S,SElemType *e) //出栈 { if(S->top==S->base) //判断栈空 return ERROR; *e=*--S->top; return OK; } Status StackEmpty(SqStack *S) { if(S->top==S->base) return TRUE; else return FALSE; } int main() { int n,m,e; SqStack S; InitStack(&S); printf("请输入一个非负十进制整数:"); scanf("%d",&n); printf("请输入你要转换的进制:"); scanf("%d",&m); while(n) { Push(&S,n%m); n=n/m; } while(!StackEmpty(&S)) { Pop(&S,&e); printf("%d",e); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
- ¥15 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?