编译无错误但是无法正常运行
#include<stdlib.h>#include<stdio.h>
#include<ctype.h>
#define stack_init_size 100
#define stackincrement 20
#define MaxBuffer 10
typedef double elemtype;
typedef struct
{
elemtype* top;
elemtype* base;
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)
{
printf("failed to realloc stack");
exit(1);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack* s, elemtype* e)
{
if (s->top == s->base)
{
printf("栈已经为空");
exit(1);
}
*e = *--(s->top);
}
int Len(sqStack s)
{
return (s.top - s.base);
}
int main()
{
char c;
sqStack s;
InitStack(&s);
double d, e;
char str;
int i = 0;
printf("输入反波兰算式,输入#停止: ");
scanf_s("%c", &c);
while (c != '#')
{
while (isdigit(c) || c == '.')
{
str = c;
str = '\0';
if (i >= 10) return -1;
scanf_s("%c", &c);
if (c == ' ')
{
d = atof(str);
Push(&s, d);
i = 0;
break;
}
}
switch (c)
{
case '+':
Pop(&s, &d);
Pop(&s, &e);
Push(&s, d + e);
break;
case '-':
Pop(&s, &d);
Pop(&s, &e);
Push(&s, e - d);
break;
case '*':
Pop(&s, &d);
Pop(&s, &e);
Push(&s, d * e);
break;
case '/':
Pop(&s, &d);
Pop(&s, &e);
if (d != 0) Push(&s, e / d);
else
{
printf("除数不能为0");
exit(0);
}
break;
}
scanf_s("%c", &c);
}
Pop(&s, &d);
printf("\n结果为:%f ", d);
return 0;
}
在编译时无报错,但是执行到输入数据后无法正常得出结果 在你的代码中,有几个问题导致程序无法正常运行。
首先,你没有包含头文件`<stdio.h>`和`<stdlib.h>`,这会导致一些函数的定义不被识别。添加以下两行代码到开头:
#include <stdio.h>
#include <stdlib.h>
其次,你在初始化栈的时候,将`s->base`指针赋值给`s->top`,这是错误的。正确的初始化栈的方法应该是:
s->top = s->base;
再次,你在读取数字字符序列的时候,用变量`str`来保存字符序列,并将其赋值为`\0`,这是错误的。正确的保存字符序列的方法应该是:
str = c;
同时,在将字符序列转换为浮点数时,应该使用`atof`函数,并将其传递整个字符序列作为参数。修改相应的代码如下:
// 声明一个字符数组,用于保存字符序列
char str;
int i = 0;
printf("输入反波兰算式,输入#停止: ");
scanf_s("%c", &c);
while (c != '#')
{
while (isdigit(c) || c == '.')
{
str = c;
if (i >= MaxBuffer) return -1;
scanf_s("%c", &c);
if (c == ' ')
{
str = '\0';// 在字符序列最后添加'\0'作为结束标志
d = atof(str); // 使用atof将字符序列转换为浮点数
Push(&s, d);
i = 0;
break;
}
}
// ...
最后,你在打印结果时使用了`%f`作为格式化字符串,但是变量`d`的类型是`double`,应该使用`%lf`作为格式化字符串。修改相应代码如下:
Pop(&s, &d);
printf("\n结果为:%lf ", d);
综上所述,修复了以上问题的完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#define stack_init_size 100
#define stackincrement 20
#define MaxBuffer 10
typedef double elemtype;
typedef struct
{
elemtype* top;
elemtype* base;
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->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)
{
printf("failed to realloc stack");
exit(1);
}
}
*(s->top) = e;
s->top++;
}
void Pop(sqStack* s, elemtype* e)
{
if (s->top == s->base)
{
printf("栈已经为空");
exit(1);
}
*e = *--(s->top);
}
int Len(sqStack s)
{
return (s.top - s.base);
}
int main()
{
char c;
sqStack s;
InitStack(&s);
double d, e;
char str; // 修改为字符数组
int i = 0;
printf("输入反波兰算式,输入#停止: ");
scanf_s("%c", &c);
while (c != '#')
{
while (isdigit(c) || c == '.')
{
str = c;
if (i >= MaxBuffer) return -1;
scanf_s("%c", &c);
if (c == ' ')
{
str = '\0';
d = atof(str);
Push(&s, d);
i = 0;
break;
}
}
switch (c)
{
case '+':
Pop(&s, &d);
Pop(&s, &e);
Push(&s, d + e);
break;
case '-':
Pop(&s, &d);
Pop(&s, &e);
Push(&s,e - d);
break;
case '*':
Pop(&s, &d);
Pop(&s, &e);
Push(&s, d * e);
break;
case '/':
Pop(&s, &d);
Pop(&s, &e);
if (d != 0) Push(&s, e / d);
else
{
printf("除数不能为0");
exit(0);
}
break;
}
scanf_s("%c", &c);
}
Pop(&s, &d);
printf("\n结果为:%lf ", d); // 修改为%lf
return 0;
}
希望这样修改后的代码能够解决你的问题。如果还有其他问题,请随时提问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]