C ++使用堆栈评估后缀 [英] C++ evaluate postfix using stack

查看:98
本文介绍了C ++使用堆栈评估后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <stack>
#include <limits>
#include <string>
using namespace std;

int main() 
{
    string input;
    cout << "Enter a postfix expression: " << endl;
    getline(cin, input);
    int operand1, operand2, result;
    stack<char>operation;
    
    int i=0;
    while (i < input.length()) 
    {
        if (isdigit(input[i])) 
        {
            operation.push(input[i]);
        } 
       
        else 
        {
          operand2 = operation.top();
          operation.pop();
          operand1 = operation.top();
          operation.pop();
          switch(operation.top())
          {
                  case ''+'': result=operand1 + operand2;
                  operation.push(result);
                  break;
                  case ''-'': result=operand1 - operand2;
                  operation.push(result);
                  
                  break;
                  case ''*'': result=operand1 * operand2;
                  operation.push(result);
                  
                  break;
                  case ''/'': result=operand1 / operand2;
                  operation.push(result);
                  
                  break;   
        }
          
        }
        i++;
    }
    
    while (!operation.empty()) 
    {
        cout << operation.top();
        operation.pop();
    }
      
  
    cout << "The result is: "<<result<<endl;
    cin.ignore(numeric_limits<streamsize>::max(), ''\n'');
    return 0;
}



该代码没有任何错误,但是当我运行它时,出现此警告:

~~遇到问题,需要关闭.
不便之处,敬请原谅.

代码有什么问题?



The code did not have any error but when i run it, this warning appear :

~~has encountered a problem and needs to close.
we are sorry for the inconvenience.

what is wrong with the code?

推荐答案

它不起作用?

首先想到的是,您没有对用户输入的内容进行任何有用的检查.

第二个是如果输入的第一个字符不是数字怎么办?

我强烈建议您学习使用调试器.

在行
It doesn''t work?

The first thing that springs to mind, is that you do no useful checking on what the user entered.

The second is that what happens if the first character entered is not a digit?

I strongly suggest that you learn to use the debugger.

Put a breakpoint on the line
while (i < input.length())

上放置一个断点,并逐步执行它,逐步查看变量.在执行每个步骤之前,您应该对预期会发生的事情有个好主意.检查它.如果没有,为什么不呢?更改您的代码,然后重试.这是您要学习的唯一方法.

我建议您先设置一个已知的静态输入字符串,而不是每次都键入它-这样可以简化运行并减少变量的数量.

and single step it through, looking at the variables as you go. Before each step, you should have a good idea in you mind of what you expect to happen. Check it did. If not, why not? change your code, and try again. It is the only way you are going to learn.

I would suggest that you start by setting a known, static input string, rather than typing it each time - that way you can simplify the run and reduce the number of variables.


查找问题最有效的方法是使用调试器查看程序崩溃的位置.

而且您无需在程序中进行任何语法检查.您假定输入字符串的格式正确.这样做可能会很好.例如,此代码中唯一允许的字符是"0123456789 +-*/" .
如果使用空格或浮点值(不处理``.''字符)会发生什么?

顺便说一下,使用此测试您不能处理大于 9 的数字:
The easiest and most efficient way to find your problem is to use the debugger to see where your program crashes.

And you don''t do any syntax checking in your program. You assume that the input string is properly formated. It would probably be good to do it. For example the only allowed characters in this code are "0123456789+-*/".
What will happen if you use spaces or float values (you don''t handle the ''.'' character)?

And by the way, you can''t handle numbers higher than 9 with this test:
if (isdigit(input[i]))
{
     operation.push(input[i]);
}


您没有将运算符压入堆栈,但尝试将其从空堆栈中弹出.

[edit]以防万一,您可能不知道我在说什么,这是switch语句.您的
You didn''t push your operator on the stack, yet you try to pop it from an empty stack.

[edit]Just in case you don''t know what I''m talking about, it''s the switch statement. Your
switch(operation.top())

引用了一个空堆栈,从而导致该错误.用

refers to an empty stack, causing the error. Replace it with

switch (input[i])

替换它可能会起作用.[/edit]

附注:(出于完整性考虑)
在根据此答案简短交换了初步解决方案后(请参阅OP的解决方案),所需的其他修复程序是:

-将stack<char>operation;更改为stack<int>operation;
-将operation.push(input[i]);更改为operation.push(input[i]-''0'');

and it might work.[/edit]

P.S.: (for the sake of completeness)
After a short exchange of a preliminary solution based on this answer (see Solution from OP), the additional fixes required were:

- Change stack<char>operation; to stack<int>operation;
- Change operation.push(input[i]); to operation.push(input[i]-''0'');


这篇关于C ++使用堆栈评估后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆