簡體   English   中英

將infix轉換為postix並評估Java中的表達式

[英]Convert infix to postix and evaluate expression in Java

我應該編寫程序,使用Java中的堆棧將中綴符號轉換為后綴符號。 我已經做完了,但是遇到了錯誤。 這是到目前為止的代碼:

import java.util.Scanner;
import java.util.Stack;

public class InfixToPostfix {
  private String infix;
  private String postfix = "";

  public void convertString (String a){
    String str = "";
    infix = a;
    Stack<String> stack = new Stack<String>();

    for (int i = 0; i < infix.length(); i++){
      str = infix.substring(i,i+1);
      if(str.matches("[a-zA-Z]|\\d"))
        postfix += str;
      else if (isOperator(str)){
        if (stack.isEmpty()){
          stack.push(str);
        }
        else{
          String stackTop = stack.peek();
          while (getPrecedence(stackTop,str).equals(stackTop)
                 && !(stack.isEmpty())){
            postfix += stack.pop();
            if (!(stack.isEmpty()))
              stackTop = stack.peek();
          }
          stack.push(str);
        }
      }
    }
    while(!(stack.isEmpty()))
      postfix += stack.pop();
    System.out.println("The postfix form of the expression you entered is: " +
                       postfix);
  }

  private boolean isOperator(String ch){
    String operators = "*/%+-";
    if (operators.indexOf(ch) != -1)
      return true;
    else
      return false;
  }

  private String getPrecedence(String op1, String op2){
    String multiplicativeOps = "*/%";
    String additiveOps = "+-";
    if ((multiplicativeOps.indexOf(op1) != -1) && (additiveOps.indexOf(op2) != -1))
      return op1;
    else if ((multiplicativeOps.indexOf(op2) != -1) && (additiveOps.indexOf(op1) != 
                                                        -1))
      return op2;
    else if((multiplicativeOps.indexOf(op1) != -1) && (multiplicativeOps.indexOf   
                                                       (op2) != -1))
      return op1;
    else
      return op1;
  }
  public static void main(String[] args) {

    System.out.println("Enter an expression in the Infix form:");
    Scanner scanner = new Scanner(System.in);

    String expression = scanner.nextLine();
    new convertString (expression);

  } 
}

錯誤在最后一行,並說:

“線程”主“中的異常” java.lang.Error:未解決的編譯問題:convertString無法解析為一種類型

at InfixToPostfix.main(InfixToPostfix.java:62)"

關於如何解決此問題的任何想法? 我究竟做錯了什么?

編輯:我讓我的代碼工作,並且它成功地將中綴轉換為后綴,但是有什么辦法可以使我也對表達式求值並吐出答案? 例如,如果輸入為2 + 3,則它將轉換為23+,然后吐出5。

您正在執行new InfixToPostfix (expression) ,但這需要調用帶有String參數的構造函數。 但是您還沒有編寫這樣的構造函數。

您沒有提供接受字符串的構造函數。

給定您編寫的代碼,您應該做的是

new InfixToPostfix().convertString(expression);

您正在嘗試使用從未在類InfixToPostfix中設置的構造函數。 您要使用的是convertString(String)方法。 有兩種方法可以解決此問題...

您可以使用String參數創建構造函數,然后僅在構造函數中調用方法:

public InfixToPostfix(String a) {
   convertString(a);
}

或使用InfixToPostfix實例從主方法調用convertString函數:

InfixToPostfix ip = new InfixToPostfix();
ip.convertString(expression);

您需要提供一個帶有String參數的構造函數。 看起來像 :

public InfixToPostfix(String infix) {

this.infix = infix;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM