VC++6.0 Debug assertion Failed! 2进制转化为10进制

chenzxapple 2014-10-13 01:45:22
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define STACK_INIT_SIZE 20
#define STACKINCREMENT 10

typedef char ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stackSize;
}sqStack;

void InitStack(sqStack *s)
{
s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if( !s->base )
{
exit(0);
}

s->base = s->top;
s->stackSize = STACK_INIT_SIZE;
}

void Push(sqStack *s, ElemType e)
{
if( s->top - s->base >= s->stackSize)
{
s->base = (ElemType *)realloc(s->base, (s->stackSize + STACKINCREMENT) * sizeof(ElemType));
if( !s->base )
{
exit(0);
}
}

*(s->top) = e;
s->top++;
}


void Pop(sqStack *s, ElemType *e)
{
if( s->top == s->base )
{
return;
}
*e = *--(s->top);
}

int StackLen(sqStack s)
{
return (s.top - s.base);
}

int main()
{
ElemType c;
sqStack s;
int len, i, sum = 0;

printf("请输入二进制数,输入#符号以表示结束! \n");
scanf("%c", &c);
while( c != '#')
{
Push(&s, c);
scanf("%c", &c);
}

getchar(); //把'\n'从缓存区去掉

len = StackLen(s);
printf("栈的当前容量是: %d\n", len);

for( i=0; i<len; i++ )
{
Pop(&s, &c);
sum = sum +(c-48) * pow(2, i);
}
printf("转化为十进制数是:%d\n", sum);

return 0;
}






把realloc函数注释掉就变成 运行时 错误提示:.exe已停止工作
...全文
202 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-10-13
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
铖邑 2014-10-13
  • 打赏
  • 举报
回复
ElemType *base; ElemType *top; int stackSize; 这些都需要有一个初始值,写程序有的地方是不能偷懒的 此外,话说,你InitStack写得也有问题啊,还是好好检查一下代码吧
chenzxapple 2014-10-13
  • 打赏
  • 举报
回复
sqStack s; 不是这样初始化建立栈么?
铖邑 2014-10-13
  • 打赏
  • 举报
回复
sqStack s; 初始化完全没搞好,哎……

69,371

社区成员

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

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