java - 需要修复的Infix程序的后缀

标签 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:  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

最佳答案

像这样更改您的代码。

                if(input[i].equals("+"))
                {
                    s.push(o1 + "+" + o2);
                }
                else if(input[i].equals("-"))
                {
                    s.push(o2 + "-" + o1);
                }
                else
                {
                    s.push(o1 + "*" + o2);
                }

但是“1 2 3 + *”的结果是“3 + 2 * 1”。
这是另一个问题。

关于java - 需要修复的Infix程序的后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279808/

相关文章:

java - MouseListener 什么都不做

java - 如何从java代码检查mariaDB ODBC驱动程序版本

java - 无法检索 Android 自定义类 arraylist 数据

使用 Oracle 内部 JDBC 驱动程序调用 createClob() 时出现 java.lang.NoSuchMethodError

c++ - 要求成员 'push_back' vector C++

windows - 如何在 Windows 上获取线程堆栈信息?

Javascript 奇怪的 StackOverflow 错误

c++ - 使用 boost asio 编译项目时出错

java - Android Studio Flamingo 出现“compileDebugJavaWithJavac”错误

c++ - 如果堆栈在数值较低的地址增长,为什么指针比较会反转它?