首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >评价PostFix函数

评价PostFix函数
EN

Stack Overflow用户
提问于 2016-04-21 04:03:28
回答 1查看 913关注 0票数 0

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

代码语言:javascript
复制
/*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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 04:16:47

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

最后得到的是一个与0符号对应的48偏移的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36759435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档