白日做梦嘞 2022-04-09 10:30 采纳率: 86.5%
浏览 13
已结题

栈的后缀表达式中不懂

问题遇到的现象和发生背景

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
#define max 100

typedef struct STACK
{
int date[max];
int top;
}stack;

stack* initStack()//创建线性存储结构的栈结构
{
stack* s;
s = (stack*)malloc(sizeof(stack));
s->top = -1;
return s;
}

void destroyStack(stack* s)//摧毁栈
{
free(s);
printf("清空成功!");
}

stack* clearStack(stack* s)//清空栈
{
s->top = -1;
printf("清理成功!");
return s;
}

int stackEmpty(stack* s)//判断链表是否为空
{
if (s->top >= max)
return FALSE;
if (s->top == -1)
return TRUE;
else
return FALSE;
}

int getTop(stack* s, int* e)//获取栈顶元素
{
if (s->top > -1 && s->top < max)
{
*e = s->date[s->top];
return OK;
}
else
return FALSE;
}

int push(stack* s, int e)//进栈操作
{
if (s->top == max - 1)
{
return ERROR;
}
s->top++;
s->date[s->top] = e;
return OK;
}

int pop(stack* s, int* e)//出栈操作
{
if (s->top == -1)
return ERROR;
*e = s->date[s->top];
s->top--;
return OK;
}

int stackLength(stack* s)//获取链表长度
{
return s->top++;
}

int main()
{
char str[100];
int i = 0, a, b, c, sum;
stack* s;
s = initStack();
gets_s(str);
while (str[i] != '\0')
{
if (str[i] >= '0' && str[i] <= '9')//1. 将字符串型的数字转换为整型(或者浮点型)。
{
sum = 0;
sum = str[i] - '0';
i++;
while (str[i] >= '0' && str[i] <= '9')
{
sum = sum * 10 + str[i] - '0';
printf("%d\n",sum);
i++;
}
push(s, sum);
}
if (str[i] == '+' || str[i] == '-' || str[i] == '' || str[i] == '/')//3. 遇到运算符(#),则将栈中的前两个数字(a和b)出栈,并将b#a的值压入栈中。
{
pop(s, &a);
pop(s, &b);
if (str[i] == '+')
push(s, b + a);
else if (str[i] == '-')
push(s, b - a);
else if (str[i] == '
')
push(s, b * a);
else if (str[i] == '/')
push(s, b / a);
else
printf("error\n");
}
i++;
}
pop(s, &c);//栈顶的数字出栈
printf("%s=%d\n", str, c);
return 0;
}

img


这是怎么变的 sum是整形 我不应该 用sum等于 str{i}-0 就行了吗 为什么sum*10还要加str

  • 写回答

1条回答 默认 最新

  • 对象被抛出 2022-04-09 10:33
    关注

    你得算上前面的结果啊, 比如12+3, 遍历到2的时候sum是1, 应该是1*10+2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 4月9日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格