Home > Net >  How to implement a method to evaluate a mathematical expression in java
How to implement a method to evaluate a mathematical expression in java

Time:04-03

I am writing a program to evaluate a mathematical expression. The following conditions apply to the program.

  1. ^ is processed first
  2. *, %, / all hold the same precidence

Expected:

Enter an expression: (5 * 2 ^ 3   2 * 3 % 2) *4
"(5 * 2 ^ 3   2 * 3 % 2) *4 = 160"

Actual:

Enter an expression: (5 * 2 ^ 3   2 * 3 % 2) * 4 
Exception in thread "main" java.lang.NumberFormatException: For input string: "2^3"
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.valueOf(Integer.java:983)
        at Test2.evaluateExpression(Test2.java:87)
        at Test2.main(Test2.java:15)

My attempt

import java.util.Stack;

public class EvaluateExpression {
  public static void main(String[] args) {
    // Check number of arguments passed
    if (args.length != 1) {
      System.out.println(
        "Usage: java EvaluateExpression \"expression\"");
      System.exit(1);
    }

    try {
      System.out.println(evaluateExpression(args[0]));
    }
    catch (Exception ex) {
      System.out.println("Wrong expression: "   args[0]);
    }
  }

  /** Evaluate an expression */
  public static int evaluateExpression(String expression) {
    // Create operandStack to store operands
    Stack<Integer> operandStack = new Stack<>();
  
    // Create operatorStack to store operators
    Stack<Character> operatorStack = new Stack<>();
  
    // Insert blanks around (, ),  , -, /, and *
    expression = insertBlanks(expression);

    // Extract operands and operators
    String[] tokens = expression.split(" ");

    // Phase 1: Scan tokens
    for (String token: tokens) {
      if (token.length() == 0) // Blank space
        continue; // Back to the for loop to extract the next token
      else if (token.charAt(0) == ' ' || token.charAt(0) == '-') {
        // Process all  , -, *, / in the top of the operator stack 
        while (!operatorStack.isEmpty() &&
          (operatorStack.peek() == ' ' || 
           operatorStack.peek() == '-' ||
           operatorStack.peek() == '*' ||
           operatorStack.peek() == '/')) {
          processAnOperator(operandStack, operatorStack);
        }

        // Push the   or - operator into the operator stack
        operatorStack.push(token.charAt(0));
      }
      else if (token.charAt(0) == '*' || token.charAt(0) == '/') {
        // Process all *, / in the top of the operator stack 
        while (!operatorStack.isEmpty() &&
          (operatorStack.peek() == '*' ||
          operatorStack.peek() == '/')) {
          processAnOperator(operandStack, operatorStack);
        }

        // Push the * or / operator into the operator stack
        operatorStack.push(token.charAt(0));
      }
      else if (token.trim().charAt(0) == '(') {
        operatorStack.push('('); // Push '(' to stack
      }
      else if (token.trim().charAt(0) == ')') {
        // Process all the operators in the stack until seeing '('
        while (operatorStack.peek() != '(') {
          processAnOperator(operandStack, operatorStack);
        }
        
        operatorStack.pop(); // Pop the '(' symbol from the stack
      }
      else { // An operand scanned
        // Push an operand to the stack
        operandStack.push(Integer.valueOf(token));
      }
    }

    // Phase 2: process all the remaining operators in the stack 
    while (!operatorStack.isEmpty()) {
      processAnOperator(operandStack, operatorStack);
    }

    // Return the result
    return operandStack.pop();
  }

  /** Process one operator: Take an operator from operatorStack and
   *  apply it on the operands in the operandStack */
  public static void processAnOperator(
      Stack<Integer> operandStack, Stack<Character> operatorStack) {
    char op = operatorStack.pop();
    int op1 = operandStack.pop();
    int op2 = operandStack.pop();
    if (op == ' ') 
      operandStack.push(op2   op1);
    else if (op == '-') 
      operandStack.push(op2 - op1);
    else if (op == '*') 
      operandStack.push(op2 * op1);
    else if (op == '/') 
      operandStack.push(op2 / op1);
  }
  
  public static String insertBlanks(String s) {
    String result = "";
    
    for (int i = 0; i < s.length(); i  ) {
      if (s.charAt(i) == '(' || s.charAt(i) == ')' || 
          s.charAt(i) == ' ' || s.charAt(i) == '-' ||
          s.charAt(i) == '*' || s.charAt(i) == '/')
        result  = " "   s.charAt(i)   " ";
      else
        result  = s.charAt(i);
    }
    
    return result;
  }
}

Any help with this would be greatly appreciated. Thank you!

CodePudding user response:

Your program gets that NumberFormatException because what you're considering as "left" is still an expression, not a resultant number of having evaluated the expression.

Then, Double.parseDouble("5 * 2 ") is not receiving a valid number as String. Instead, it's receiving the substring that still is expression, which is considered not a number.

    // perform exponentiation
    if(s.contains("^")){
      ...
      left = Double.parseDouble(substring[i]); <-- Here substring[i] is "5 * 2 "
      ...
    }

You might consider to remove all whitespaces from the input expression in order to facilitate its parsing.

CodePudding user response:

You are doing a split but you are not taking into account the parentheses and whitespace in the input expression. Trying to convert it to a Double type throws the error you see.

You can add this in the main before call evaluateExpression function:

            expression = expression.replace(" ","");

Also you are trying to do a split with the reserved character ^. If you want to split with this character you should do:

            String[] substring = s.split("\\^");

You can see more information about how to define a pattern here: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

But first I'd recommend looping over the entire expression into evaluateExpression function so that you can break it into chunks.

I hope this help you.

  •  Tags:  
  • java
  • Related