🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
> Dijkstra的双栈算数表达式求值算法 ```java package test; import java.util.Scanner; import ds.impl.LinkedStack; public class StackTest { public static void main(String[] args) { evaluate(); } /** * Dijkstra的双栈算数表达式求值算法 */ public static void evaluate() { String str = "( 1 + ( ( 2.3 + 3 ) / ( sqrt ( 16 ) * ( 5 % 9 ) ) ) )"; LinkedStack<String> opStack = new LinkedStack<>(); LinkedStack<Double> valStack = new LinkedStack<>(); Scanner scanner = new Scanner(str); while (scanner.hasNext()) { String s = scanner.next(); System.out.print(s + ' '); switch (s) { case "+": case "-": case "*": case "/": case "%": case "sqrt": opStack.push(s); break; case "(": break; case ")": String op = opStack.pop(); double v = valStack.pop(); switch (op) { case "+": v = valStack.pop() + v; break; case "-": v = valStack.pop() - v; break; case "*": v = valStack.pop() * v; break; case "/": v = valStack.pop() / v; break; case "%": v = valStack.pop() % v; break; case "sqrt": v = Math.sqrt(v); break; default: break; } valStack.push(v); break; default: valStack.push(Double.parseDouble(s)); break; } } System.out.printf("= %f\n", valStack.pop()); } } ```