链栈 进行进制转换时碰到 隐藏的错误

csdaa2010 2011-11-01 01:16:58
程序能运行起来,但是老是报错.....

#include <stdio.h>
#include <malloc.h>

#define size 50
typedef struct Node
{
int elem[size];
int top;
}Stack;


//入栈
int push(Stack *L,int input)
{
if(L->top==size-1)
return 0;
L->top++;
L->elem[L->top]=input;
return 1;
}
//判空
int IsEmpty(Stack *s)
{
if(s->top==-1)
return 0;
else
return 1;
}
//出栈
int pop(Stack *s,int *del)
{
if(s->top==-1)
return 0;

*del=s->elem[s->top];
s->top--;
return 1;
}






//将任意正十进制数N,转换为二进制数输出
void Conver(int N)
{ //S为顺序栈或链栈
Stack S;
int x;
//InitStack(&S);
while(N>0)
//将模2余数入栈
{ x=N%8;
push(&S, x);
N=N/8;
}
//非空继续
while(!IsEmpty(&S))
{
//出栈,输出
pop(&S,&x); printf("%d",&x);
}
}


void main()
{
Conver(1348);
}
...全文
88 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdaa2010 2011-11-01
  • 打赏
  • 举报
回复
没法编辑,上面的convert()的注释没改,再发一次
csdaa2010 2011-11-01
  • 打赏
  • 举报
回复
这是修稿后的代码

#include <stdio.h>
#include <malloc.h>

#define size 50
typedef struct Node
{
int elem[size];
int top;
}Stack;

//初始化
void initialize(Stack *s)
{
s->top=-1;
}


//入栈
int push(Stack *L,int input)
{
if(L->top==size-1)
return 0;
L->top++;
L->elem[L->top]=input;
return 1;
}
//判空
int IsEmpty(Stack *s)
{
if(s->top==-1)
return 0;
else
return 1;
}
//出栈
int pop(Stack *s,int *del)//-------------------此处修改,原来是 int pop(Stack *s,int del)
{
if(s->top==-1)
return 0;

*del=s->elem[s->top];//---------------此处修改 del=s->elem[s->top];
s->top--;
return 1;
}

//将任意正十进制数N,转换为二进制数输出
void Conver(int N)
{ //S为顺序栈或链栈
Stack S;
int x;
initialize(&S);
while(N>0)
{ x=N%8; //将模8余数入栈
push(&S, x);
N=N/8;

}

//非空继续
while(IsEmpty(&S)) //-----------------此处修改 ,原来是while(!IsEmpty(&S))
{
//出栈,输出
pop(&S,&x);//--------------------此处修改 ,原来是 pop(&S,x);
printf("%d",x);
}
}


void main()
{
Conver(1348);
printf("\n");
}
csdaa2010 2011-11-01
  • 打赏
  • 举报
回复
谢谢了,大家真热心

中间convert()的那个函数,原来是老师的,被我拿来改了没改全,下午上机调试终于找到问题来了
感谢
powtxt
xiaozhiwei----------->一眼就看出了问题的所在
pengsheng1988
pengsheng1988 2011-11-01
  • 打赏
  • 举报
回复
首先你这个是将十进制转化为8进制,不是二进制,你的程序有两个错误。
(1)while(!IsEmpty(&S))

你这里这个循环应该是当栈不空时,弹出数据,可是你的IsEmpty()返回值弄反了,改成:
int IsEmpty(Stack *s)
{
if(s->top==-1)
return 1;
else
return 0;
}


(2)printf("%d",&x);
这句错了,这样你输出的是x的地址,改成:printf("%d",x);
xiaozhiwei 2011-11-01
  • 打赏
  • 举报
回复
1.
//判空
int IsEmpty(Stack *s)
{
if(s->top==-1)
return 1; // 为空返回1
else
return 0;
}

2.
pop(&S,&x); printf("%d",x);
csdaa2010 2011-11-01
  • 打赏
  • 举报
回复
但是不能输出
csdaa2010 2011-11-01
  • 打赏
  • 举报
回复
改了

#include <stdio.h>
#include <malloc.h>

#define size 50
typedef struct Node
{
int elem[size];
int top;
}Stack;

//初始化
void initialize(Stack *s)
{
s->top=-1;
}


//入栈
int push(Stack *L,int input)
{
if(L->top==size-1)
return 0;
L->top++;
L->elem[L->top]=input;
return 1;
}
//判空
int IsEmpty(Stack *s)
{
if(s->top==-1)
return 0;
else
return 1;
}
//出栈
int pop(Stack *s,int *del)
{
if(s->top==-1)
return 0;

*del=s->elem[s->top];
s->top--;
return 1;
}






//将任意正十进制数N,转换为二进制数输出
void Conver(int N)
{ //S为顺序栈或链栈
Stack S;
int x;
initialize(&S);
while(N>0)
{ x=N%8; //将模8余数入栈
push(&S, x);
N=N/8;

}
//非空继续
while(!IsEmpty(&S))
{
//出栈,输出

pop(&S,&x); printf("%d",&x);
}
}


void main()
{
Conver(1348);
}
powtxt 2011-11-01
  • 打赏
  • 举报
回复
top未初始化,何来-1

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧