📜  在Java中用逆波兰表示法计算算术表达式的值

📅  最后修改于: 2022-05-13 01:55:33.949000             🧑  作者: Mango

在Java中用逆波兰表示法计算算术表达式的值

Reverse Polish 'Notation是后缀符号,就数学概念而言,它表示运算符数后面的运算符。让我们以问题陈述来实现RPN

问题陈述:任务是使用有效的运算符(如 +、-、*、/)找到数组中存在的算术表达式的值。每个操作数可以是整数或其他表达式。

笔记:

  1. 两个整数之间的除法应该向零截断。
  2. 给定的 RPN 表达式始终有效。这意味着表达式将始终评估为结果,并且不会有任何除以零操作。

RPN的外行工作如图

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: 
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

方法:

该问题的基本方法是使用堆栈。

  • 访问数组中的所有元素,如果元素与特殊字符('+'、'-'、'*'、'/')不匹配,则将该元素压入堆栈。
  • 然后每当找到特殊字符时,从堆栈中弹出前两个元素并执行操作,然后再次将元素推入堆栈。
  • 对数组中的所有元素重复以上两个过程
  • 最后从堆栈中弹出元素并打印结果

执行:

Java
// Java Program to find the
// solution of the arithmetic
// using the stack
import java.io.*;
import java.util.*;
 
class solution {
    public int stacky(String[] tokens)
    {
 
        // Initialize the stack and the variable
        Stack stack = new Stack();
        int x, y;
        String result = "";
        int get = 0;
        String choice;
        int value = 0;
        String p = "";
 
        // Iterating to the each character
        // in the array of the string
        for (int i = 0; i < tokens.length; i++) {
 
            // If the character is not the special character
            // ('+', '-' ,'*' , '/')
            // then push the character to the stack
            if (tokens[i] != "+" && tokens[i] != "-"
                && tokens[i] != "*" && tokens[i] != "/") {
                stack.push(tokens[i]);
                continue;
            }
            else {
                // else if the character is the special
                // character then use the switch method to
                // perform the action
                choice = tokens[i];
            }
 
            // Switch-Case
            switch (choice) {
            case "+":
 
                // Performing the "+" operation by poping
                // put the first two character
                // and then again store back to the stack
 
                x = Integer.parseInt(stack.pop());
                y = Integer.parseInt(stack.pop());
                value = x + y;
                result = p + value;
                stack.push(result);
                break;
 
            case "-":
 
                // Performing the "-" operation by poping
                // put the first two character
                // and then again store back to the stack
                x = Integer.parseInt(stack.pop());
                y = Integer.parseInt(stack.pop());
                value = y - x;
                result = p + value;
                stack.push(result);
                break;
 
            case "*":
 
                // Performing the "*" operation
                // by poping put the first two character
                // and then again store back to the stack
 
                x = Integer.parseInt(stack.pop());
                y = Integer.parseInt(stack.pop());
                value = x * y;
                result = p + value;
                stack.push(result);
                break;
 
            case "/":
 
                // Performing the "/" operation by poping
                // put the first two character
                // and then again store back to the stack
 
                x = Integer.parseInt(stack.pop());
                y = Integer.parseInt(stack.pop());
                value = y / x;
                result = p + value;
                stack.push(result);
                break;
 
            default:
                continue;
            }
        }
 
        // Method to convert the String to integer
        return Integer.parseInt(stack.pop());
    }
}
 
class GFG {
   
    public static void main(String[] args)
    {
        // String Input
        String[] s
            = { "10", "6", "9",  "3", "+", "-11", "*",
                "/",  "*", "17", "+", "5", "+" };
       
        solution str = new solution();
        int result = str.stacky(s);
        System.out.println(result);
    }
}


输出 :

22

时间复杂度:O(n)