关于栈的二进制转十进制代码问题
为什么我用vs2013将小甲鱼老师的代码敲上去,黑窗口在输入二进制后就没反应了代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STACK_INIT_SIZE 20
#define STACKINCREMENT10
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->top = s->base;
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;
doublesum = 0;
InitStack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
scanf_s("%c", &c);
while (c != '#')
{
Push(&s, c);
scanf_s("%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;
}
char str;
ElemType c;
sqStack s;
int len, i;
doublesum = 0;
InitStack(&s);
printf("请输入二进制数,输入#符号表示结束!\n");
// scanf_s("%c", &c);
// c = getchar();
// while (c != '#')
// {
// Push(&s, c);
// fflush(stdin);
// c = getchar();
// //scanf_s("%c", &c);
// }
scanf_s("%9s",str,_countof(str));
for (int i=0;i<10;i++)
{
if (str == '#' || str == '\0')
{
break;
}
Push(&s,str);
}
getchar();// 把'\n'从缓冲区去掉
len = StackLen(s);
printf("栈的当前容量是: %d\n", len);
for (i = 0; i < len; i++)
{
Pop(&s, &c);
sum = sum + (c - 48) * pow(2.0, i);
}
printf("转化为十进制数是: %d\n", (int)sum);
return 0;
我把main函数中字符输入改了一下。 把scanf_s 改为 scanf
把sum 定义为int 型 慈慈乱了 发表于 2014-10-12 11:48
把scanf_s 改为 scanf
把sum 定义为int 型
平台用的vs2013,改为scanf和sum改为int的话运行无法通过
错误提示为:
1>------ 已启动生成:项目: ConsoleApplication4, 配置: Debug Win32 ------
1>test.c
1>c:\users\administrator\documents\visual studio 2013\projects\consoleapplication4\consoleapplication4\test.c(67): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> e:\program files\vs2013\vc\include\stdio.h(283) : 参见“scanf”的声明
1>c:\users\administrator\documents\visual studio 2013\projects\consoleapplication4\consoleapplication4\test.c(82): warning C4244: “=”: 从“double”转换到“int”,可能丢失数据
========== 生成:成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
服务器已断开连接,且未发送任何数据。
服务器已断开连接,且未发送任何数据。
小甲鱼的视频又不能下了????? 俺来学习谢谢分享 数据结构吗? 还没学习到呢
页:
[1]