zuimman
2019-05-08 21:39
采纳率: 20%
浏览 444

利用栈进行进制转换,为什么无法运行

#include
#include
#define stacksize 100
#define stackincrease 10
typedef int elemtype;
typedef struct Stack{
elemtype bottom;
elemtype *top;
int stack_size;
}stack; //定义栈的数据结构
void initstack(stack *s);
void push(stack *s,elemtype e);
void pop(stack *s,elemtype *e);
int stacklength(stack *s);
void initstack(stack *s){
s->bottom=(elemtype *)malloc(sizeof(elemtype)*stacksize);
if(!s->bottom) exit(0);
s->top=s->bottom;
s->stack_size=stacksize;
} //初始化栈
void push(stack *s,elemtype e){
if(s->top - s->bottom>=s->stack_size){
s->bottom=(elemtype *)realloc(s->bottom,(stacksize+stackincrease)*sizeof(elemtype));
if(!s->bottom) exit(0);
s->top=s->bottom+s->stack_size;
s->stack_size+=stackincrease;
}
*(s->top)=e;
s->top++;
}//进栈操作
void pop(stack *s,elemtype *e){
if(s->top==s->bottom)
exit(0);
s->top--;
*e=
(s->top);
} //出栈操作
int main(void){
int n,k,d;
int j=0,i=0;
int *e;
stack *s;
initstack(s);
printf("请输入一个十进制的数:");
scanf("%d",&n);
printf("请输入一个d进制的数",);

scanf("%d",&d);
while(n>0){
k=n%d;
push(s,k);
n=n/d;
j++; //d进制的位数
}
while(i<j){
pop(s,e);
printf("%d",*e);
i++;
}
return 0;
}


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • heymye 2019-05-09 00:40
    已采纳

    有两个编译错误,另外有两个指针使用问题。
    帮你改了下,可以运行了,但是你的代码的逻辑不对,结果是错的,自己检查下吧。

    #include "stdio.h"
    #include "stdlib.h"
    #define stacksize 100
    #define stackincrease 10
    typedef int elemtype;
    typedef struct Stack{
    elemtype bottom;
    elemtype *top;
    int stack_size;
    }stack; //定义栈的数据结构
    void initstack(stack *s);
    void push(stack *s,elemtype e);
    void pop(stack *s,elemtype *e);
    int stacklength(stack *s);
    void initstack(stack *s){
    s->bottom=(elemtype *)malloc(sizeof(elemtype)*stacksize);
    if(!s->bottom) exit(0);
    s->top=s->bottom;
    s->stack_size=stacksize;
    } //初始化栈
    void push(stack *s,elemtype e){
    if(s->top - s->bottom>=s->stack_size){
    s->bottom=(elemtype *)realloc(s->bottom,(stacksize+stackincrease)*sizeof(elemtype));
    if(!s->bottom) exit(0);
    s->top=s->bottom+s->stack_size;
    s->stack_size+=stackincrease;
    }
    *(s->top)=e;
    s->top++;
    }//进栈操作
    void pop(stack *s,elemtype *e){
    if(s->top==s->bottom)
    exit(0);
    s->top--;
    *e=(s->top);
    } //出栈操作
    int main(void){
    int n,k,d;
    int j=0,i=0;
    int e;
    stack st;
    stack *s=&st;
    initstack(s);
    printf("请输入一个十进制的数:");
    scanf("%d",&n);
    printf("请输入一个d进制的数");
    
    scanf("%d",&d);
    while(n>0){
    k=n%d;
    push(s,k);
    n=n/d;
    j++; //d进制的位数
    }
    while(i<j){
    pop(s,&e);
    printf("%d",e);
    i++;
    }
    return 0;
    }
    
    
    打赏 评论

相关推荐 更多相似问题