首页 > 解决方案 > 从后缀转换为中缀后如何计算算术转换?

问题描述

我正在编写一个程序,它采用后缀表达式并将其转换为中缀表达式并输出结果。这是我到目前为止编写的代码,但我不知道如何在转换后进行计算。

public String convert(String postfix)
{
    Stack<String> variables = new Stack<>(); // declaring a new stack to 
    hold the variables
    for(int i = 0; i < postfix.length(); i++)
    {
        char c = postfix.charAt(i);
        if(isOperator(c))
        {   
            String a = variables.pop();
            String b = variables.pop();                
            variables.push("("+a+c+b+")");
        }
        else
            variables.push(""+c);
    }
        return variables.pop();
}

我尝试在函数中添加 if/switch 语句,但我得到了“不兼容的堆栈和字符串错误”。我试图做类似的事情:

if (variables == abc++)
{      
     (a + (b + c));
}

我该怎么做呢?

标签: javastackinfix-notation

解决方案


推荐阅读