【问题标题】:Evaluating PostFix Function评估 PostFix 函数
【发布时间】:2016-08-14 01:18:46
【问题描述】:

下面我正在尝试编写一个评估后缀表达式的程序。但是,我注意到我设置要操作的两个元素没有设置为正确的值。我仍在学习 c++ 库中的堆栈功能,所以如果有人能解释为什么会发生这种情况,我将不胜感激!

/*Sample output:
Enter an infix or prefix expression:3+4-1
Postfix is: 34+1-
postFix[i]: 3
postFix[i]: 4
element1: 52
element2: 51
val is: 103
postFix[i]: 1
element1: 49
element2: 103
val is: 54
Value is 54*/

int evaluatePostfix (string postFix)
{
     stack<int> operands;

        int length = postFix.length();
        int val = 0;
        for ( int i = 0; i < length; i++ )
        {
                //if the char is a digit push the digit onto the stack
                if( isdigit( postFix[i] ))
                {
                        operands.push( postFix[i] );
                        cout << "postFix[i]: " << postFix[i] << endl;
                }
                else if ( isOperator(postFix[i]) )
                {
                        //
                        //element 1 and element 2 will be the element on the top of the stack
                        //
                        int element1 = operands.top();
                        cout << "element1: " << element1 << endl;
                        operands.pop();
                        int element2 = operands.top();
                        cout << "element2: " << element2 << endl;
                        operands.pop();
                        //
                        //create a switch statement that evaluates the elements based on the operator
                        //
                        switch( postFix[i] )
                        {
                                case '+':
                                        val = element2 + element1;
                                        cout << "val is: " << val << endl;
                                        operands.push(val);
                                        break;
                                case '-':
                                        val = element2 - element1;
                                        cout << "val is: " << val << endl;
                                        operands.push(val);
                                        break;
                                case '*':
                                        val = element2 * element1;
                                        operands.push(val);
                                        break;
                                case '/':
                                        val = element2 / element1;
                                        operands.push(val);
                                        break;
                                default:
                                        return 0;
                        }

                                                                                 }

        }
        return val;
}

【问题讨论】:

  • 你能举一个可靠的例子吗?例如,我按 3 但弹出 42。
  • 我认为这就是问题所在。我将这些元素推入堆栈,但我推入堆栈的元素不是弹出的元素。因此,例如,我按下 3 和 4。然后,程序看到运算符“+”并决定弹出前两个元素,应该是 3 和 4,但它会弹出像 52 和 51 这样的随机数。我不知道为什么会这样

标签: c++ stack postfix-notation


【解决方案1】:

问题是您的后缀是一个字符串,当您推送这些字符(它们是有效整数)时,您会返回 ascii 值(例如 51、52)。这些对应于正确的数字(例如 3、4)

你最终得到的是一个偏移 48 的值,对应于 0 符号。

【讨论】:

  • 我仍在学习 ascii 值,但我还没有完全理解它们是如何工作的。我该如何适应这种情况?
  • 一个简单的解决方案,因为您知道偏移量是否为 48,所以从堆栈返回的内容中或在推送之前减去 48..51 - 48 = 3 和 52 - 48 = 4,并且很快。 @ChynnaHernandez
  • 感谢您的解释
  • 很高兴帮助@ChynnaHernandez!
猜你喜欢
  • 2015-02-14
  • 2021-01-08
  • 2015-01-01
  • 1970-01-01
  • 2019-05-23
  • 1970-01-01
  • 2015-12-01
  • 2013-11-11
相关资源
最近更新 更多