Java 需要修复的中缀程序的后缀

Java 需要修复的中缀程序的后缀,java,compiler-errors,stack,postfix-notation,infix-notation,Java,Compiler Errors,Stack,Postfix Notation,Infix Notation,我需要这个程序的帮助,因为它编译不正确 该程序应该做到这一点: java PostfixToInfix 1 2 3 + * 1*(2+3) 我在编译时遇到以下错误: PostfixToInfix.java:64: error: bad operand types for binary operator '-' s.push(o2 - o1); ^ first type

我需要这个程序的帮助,因为它编译不正确

该程序应该做到这一点:

java PostfixToInfix
1 2 3 + *
1*(2+3)
我在编译时遇到以下错误:

    PostfixToInfix.java:64: error: bad operand types for binary operator '-'
                        s.push(o2 - o1);
                                  ^
  first type:  String
  second type: String

PostfixToInfix.java:68: error: bad operand types for binary operator '*'
                        s.push(o1 * o2);
                                  ^
  first type:  String
  second type: String
2 errors
我应该编写多少代码才能正常工作?我不确定我的代码有什么问题,它不允许它正确地执行功能。 这是我的代码:

import java.util.Scanner;
import java.util.Stack;
public class PostfixToInfix
{
    public static void main(String[] args)
    {
        String[] input = readExpr();        
        if(checkSyntax(input) == true)
        {
            int k = 0;
            Stack<String> s = new Stack<>(); 
            for(int i = 0; i < input.length; ++i)
            {
                if(isOperator(input[i]))
                {
                String o1;
                String o2;
                    if(!(s.empty()))
                    {
                        o1 = s.pop();
                    }
                    else
                    {
                    for(int j = 0; j < i; ++j)
                        {
                        k += input[j].length() + 1;
                        }
                    System.out.println("Too few operands for " + input[i]);
                    writeExpr(input);
                        for(int l = 0; l < k; ++l)
                        {
                        System.out.print(" ");
                        }
                    System.out.println("^");
                    return;
                    }
                    if(!(s.empty()))
                    {
                        o2 = s.pop();
                    }
                    else
                    {
                        for(int j = 0; j < i; ++j)
                        {
                        k += input[j].length() + 1;
                        }
                    System.out.println("Too few operands for " + input[i]);
                    writeExpr(input);
                    for(int l = 0; l < k; ++l)
                    {
                    System.out.print(" ");
                    }
                    System.out.println("^");
                    return;
                    }
                    if(input[i].equals("+"))
                    {
                        s.push(o1 + o2);
                    }
                    else if(input[i].equals("-"))
                    {
                        s.push(o2 - o1);
                    }
                    else
                    {
                        s.push(o1 * o2);
                    }
                }
                else
                {
                s.push(input[i]);
                }
            }
            String Result = s.pop();
            if(!(s.empty()))
            {
                System.out.println("Too few operators to produce a single result");
            }
            else
            {
                System.out.println(Result);
            }

        }


    } // end main

    static String[] readExpr()
    {
        Scanner stdin = new Scanner(System.in);
        String s = stdin.nextLine();

        String[] sA = s.split(" ");
        return sA;

    }

    static void writeExpr(String[] expr)
    {
        for(int i = 0; i < expr.length; ++i)
        {
            System.out.print(expr[i] + " ");
        }
        System.out.println();

    }

    static boolean isOperator(String s)
    {
        if(s.equals("+") || s.equals("-") || s.equals("*"))
        {
            return true;
        }
        return false;
    }

    static boolean checkSyntax(String[] expr)
    {
        int k = 0;
        for(int i = 0; i < expr.length; ++i)
        {
            if(!(isOperator(expr[i])))
            {
                try
                {
                Double.parseDouble(expr[i]); 
                }
                catch (Exception e)
                { 
                for(int j = 0; j < i; ++j)
                {
                    k += expr[j].length() + 1;
                }
                writeExpr(expr);
                for(int l = 0; l < k; ++l)
                {
                System.out.print(" ");
                }
                System.out.println("^");
                System.out.println("Not a number or valid operator");
                return false; 
                }
            }

        }
    return true;
    }

} // end Postfix2


class StringStack
{
    int top;
    String[] pancake;

    StringStack() //constructor for a new empty stack
    {
    top = 0;
    pancake = new String[1000];

    } // end DoubleStack

    boolean empty() //whether the stack is empty
    {
        return top == 0;

    } // end empty

    String pop() //remove and return the top element; throw an error if empty
    {
        if(empty())
        {
            throw new Error("Error");
        }
        top -= 1;
        return pancake[top];

    } // end pop

    void push(String x) //add x to the top of the stack
    {
        if(top < 1000)
        {
        pancake[top] = x;
        top += 1;
        }
        else{
        throw new Error("Error");
        }
    } // end push

} // end StringStack
import java.util.Scanner;
导入java.util.Stack;
公共类postfix
{
公共静态void main(字符串[]args)
{
字符串[]输入=readExpr();
if(检查语法(输入)==true)
{
int k=0;
堆栈s=新堆栈();
对于(int i=0;i
像这样更改代码

                if(input[i].equals("+"))
                {
                    s.push(o1 + "+" + o2);
                }
                else if(input[i].equals("-"))
                {
                    s.push(o2 + "-" + o1);
                }
                else
                {
                    s.push(o1 + "*" + o2);
                }
但“123+*”的结果是“3+2*1”。
这是另一个问题。

当只有少数代码需要重现问题时,请不要发布200行代码。从错误消息中您不明白什么?当应用于
字符串
值时,您认为使用
*
-
意味着什么?阅读错误,它给出了您需要的所有提示。行,代码和原因。你真的没有问题。在为你的特定问题寻求解决方案之前,要仔细考虑。避免被否决。我会错误地说这不是一个声明应为,或“')”为expected@anderkat97你在说什么不是陈述时出错了?您真的应该能够自己解决像这样的琐碎编译错误,而不必在这里或其他地方匆忙处理。