c++ - 评估中缀表达式而不将其转换为后缀

标签 c++ stack infix-notation

<分区>

我试图在 1 遍中评估中缀表达式而不将其转换为后缀,但它没有为某些表达式提供正确的输出。例如:3-5*10/5+10 , (45+5)-5*(100/10)+5

有人可以在 cpp 中为这个问题提供适当的解决方案吗?

上一个问题的链接:How to evaluate an infix expression in just one scan using stacks?

请不要将其标记为重复,因为我已经尝试了上述给定线程中回答的算法但无济于事。

#include<bits/stdc++.h>

int isoperand(char x)
{
    if(x == '+' || x=='-'|| x=='*' || x=='/' || x==')' || x=='(')
        return 0;
    return 1;
}

int Pre(char x)
{
    if(x == '+' || x == '-')
        return 1;
    if(x == '*' || x == '/')
        return 3;
    return 0;
}

int infixevaluation(std::string exp)
{
    std::stack<int> s1; //Operand Stack
    std::stack<char> s2; //Operator Stack
    int i,x,y,z,key;
    i=0;
    while(exp[i]!='\0')
    {

        if(isoperand(exp[i]))
        {
            key = exp[i]-'0';
            s1.push(key);
            i++;
        }
        else if(!isoperand(exp[i]) && s2.empty())
            s2.push(exp[i++]);
        else if(!isoperand(exp[i]) && !s2.empty())
        {
            if(Pre(exp[i])>Pre(s2.top()) && exp[i]!=')')
                s2.push(exp[i++]);
            else if(exp[i]==')' && s2.top() == '(')
            {
                s2.pop();
                i++;
            }
            else if(exp[i]=='(')
                s2.push(exp[i++]);
            else
            {
                x = s1.top();
                s1.pop();
                y = s2.top();
                s2.pop();
                z = s1.top();
                s1.pop();
                if(y == '+')
                    s1.push(z+x);
                else if(y == '-')
                    s1.push(z-x);
                else if(y == '*')
                    s1.push(x*z);
                else if(y == '/')
                    s1.push(z/x);
            } 
        }
    }
    while(!s2.empty())
    {
        x = s1.top();
        s1.pop();
        y = s2.top();
        s2.pop();
        z = s1.top();
        s1.pop();
        if(y == '+')
            s1.push(x+z);
        else if(y == '-')
            s1.push(z-x);
        else if(y == '*')
            s1.push(x*z);
        else if(y == '/')
            s1.push(z/x);
    }
    return s1.top();
}

int main(int argc, char const *argv[])
{
    std::string s;
    getline(std::cin,s);
    std::cout<<infixevaluation(s)<<std::endl;
    return 0;
}

最佳答案

您的代码只能处理个位数的操作数——并且它不会检查格式错误的输入,因此当您有一个多位数的操作数时,它就会偏离轨道。

对于前者,最简单的修复方法就是在看到数字时扫描数字——将 if (isoperand(exp[i]) 子句更改为:

    if (isdigit(exp[i])) {
        int value = 0;
        while (isdigit(exp[i]))
            value = value * 10 + exp[i++] - '0';
        s1.push(value);
    } else ...

对于错误检查,你应该做这样的事情

  • 检查空格和其他无效字符并拒绝或跳过它们
  • 跟踪最后匹配的标记是操作数还是运算符,并给出两个连续操作数或除 ()以外的两个连续运算符的错误

关于c++ - 评估中缀表达式而不将其转换为后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57601266/

相关文章:

c++ - 快板 5 : Check if Joystick is "neutral"

c++ - 将左值引用 move 到成员变量中

c - cortex-m0 上 ISR 的终止函数

autocomplete - Solr Suggester 是否支持中缀搜索?

java - 我现在如何使用扫描仪 "build"树? (代码后有说明)

haskell - 与中缀一起使用时,约束错误中的非类型变量参数

c++ - 解决方案中的链接失败

C++ 堆栈实现

c - 为什么在声明堆栈上的大数组时在C中出现段错误?

c++ - 在 C++ 中递增常量