[JAVA]放个可以做简单数学四则运算的东东
遇到要求一个运算表达式的计算结果的问题
传进的是一个字符串 (字串内容当然是表达式了)
最终要求出运算结果
之前在 JScript 或 VBScript 里通常是 eval 来解决
下面给个 Java 的
只能算 +-*/ 还有括号
/** * (#)Calculator.java 创建时间:Apr 30, 2009 6:14:03 PM<br /> */ package cn.ialvin.util; import java.util.Stack; import java.util.regex.Pattern; /** * @author 林志斌(<b>ialvin.cn</b>) 广东 普宁 里湖 */ public class Calculator { public static void main(String[] args) { String exp = "-3.3 + 1.5 * (- 3 + -5)"; Calculator calculator = new Calculator(); System.out.println(calculator.cal(exp)); } public double cal(String exp) { exp = adj(exp); exp = conver(exp); Stack<Object> stack = new Stack<Object>(); String[] cs = exp.split("[^\\d.+\\-*/]+"); int i = 0; while (i < cs.length) { String c = cs<i>; i++; if ("+".equals(c)) { stack.push((Double)stack.pop() + (Double)stack.pop()); } else if ("-".equals(c)) { stack.push(0 - (Double)stack.pop() + (Double)stack.pop()); } else if ("*".equals(c)) { stack.push((Double)stack.pop() * (Double)stack.pop()); } else if ("/".equals(c)) { stack.push(1 / (Double)stack.pop() * (Double)stack.pop()); } else { stack.push(Double.parseDouble(c)); } } return Double.parseDouble(stack.pop().toString()); } private String adj(String exp) { exp = exp.replaceAll("[^\\d.+\\-*\\/()]+", ""); exp = exp.replaceAll("(^|[(+\\-*\\/])\\-([\\d.]+)", "$1(0-$2)"); return exp.replaceAll("[+\\-*\\/()]", " $0 ").trim(); } private String conver(String exp) { String[] str = exp.split("\\s+"); Stack<String> expStack = new Stack<String>(); for(int i = str.length - 1 ; i >= 0 ; i--) expStack.push(str<i>); Stack<String> outStack = new Stack<String>(); Stack<String> operStack = new Stack<String>(); operStack.push("#"); while (expStack.size()> 0) { String c = expStack.pop().toString(); if (c.matches("^\\d+(?:\\.\\d*)?$")) { outStack.push(c); } else if ("(".equals(c)) { operStack.push(c); } else if (")".equals(c)) { if (operStack.lastElement().equals("(")) { operStack.pop(); } else { expStack.push(c); outStack.push(operStack.pop()); } } else { if (comparison(c, operStack.lastElement())) outStack.push(operStack.pop()); operStack.push(c); } } operStack.remove(operStack.firstElement()); while(!operStack.empty()) outStack.push(operStack.pop()); return outStack.toString().replaceAll("\\[|\\]|\\,", ""); } private int getLevel(Object o) { if ("(".equals(o)) return 1; if ("+".equals(o)) return 2; if ("-".equals(o)) return 2; if ("*".equals(o)) return 3; if ("/".equals(o)) return 3; return -1; } private boolean comparison(String c1 ,Object c2) { return getLevel(c2)-getLevel(c1) >= 0; } }
传进的是一个字符串 (字串内容当然是表达式了)
最终要求出运算结果
之前在 JScript 或 VBScript 里通常是 eval 来解决
下面给个 Java 的
只能算 +-*/ 还有括号
/** * (#)Calculator.java 创建时间:Apr 30, 2009 6:14:03 PM<br /> */ package cn.ialvin.util; import java.util.Stack; import java.util.regex.Pattern; /** * @author 林志斌(<b>ialvin.cn</b>) 广东 普宁 里湖 */ public class Calculator { public static void main(String[] args) { String exp = "-3.3 + 1.5 * (- 3 + -5)"; Calculator calculator = new Calculator(); System.out.println(calculator.cal(exp)); } public double cal(String exp) { exp = adj(exp); exp = conver(exp); Stack<Object> stack = new Stack<Object>(); String[] cs = exp.split("[^\\d.+\\-*/]+"); int i = 0; while (i < cs.length) { String c = cs<i>; i++; if ("+".equals(c)) { stack.push((Double)stack.pop() + (Double)stack.pop()); } else if ("-".equals(c)) { stack.push(0 - (Double)stack.pop() + (Double)stack.pop()); } else if ("*".equals(c)) { stack.push((Double)stack.pop() * (Double)stack.pop()); } else if ("/".equals(c)) { stack.push(1 / (Double)stack.pop() * (Double)stack.pop()); } else { stack.push(Double.parseDouble(c)); } } return Double.parseDouble(stack.pop().toString()); } private String adj(String exp) { exp = exp.replaceAll("[^\\d.+\\-*\\/()]+", ""); exp = exp.replaceAll("(^|[(+\\-*\\/])\\-([\\d.]+)", "$1(0-$2)"); return exp.replaceAll("[+\\-*\\/()]", " $0 ").trim(); } private String conver(String exp) { String[] str = exp.split("\\s+"); Stack<String> expStack = new Stack<String>(); for(int i = str.length - 1 ; i >= 0 ; i--) expStack.push(str<i>); Stack<String> outStack = new Stack<String>(); Stack<String> operStack = new Stack<String>(); operStack.push("#"); while (expStack.size()> 0) { String c = expStack.pop().toString(); if (c.matches("^\\d+(?:\\.\\d*)?$")) { outStack.push(c); } else if ("(".equals(c)) { operStack.push(c); } else if (")".equals(c)) { if (operStack.lastElement().equals("(")) { operStack.pop(); } else { expStack.push(c); outStack.push(operStack.pop()); } } else { if (comparison(c, operStack.lastElement())) outStack.push(operStack.pop()); operStack.push(c); } } operStack.remove(operStack.firstElement()); while(!operStack.empty()) outStack.push(operStack.pop()); return outStack.toString().replaceAll("\\[|\\]|\\,", ""); } private int getLevel(Object o) { if ("(".equals(o)) return 1; if ("+".equals(o)) return 2; if ("-".equals(o)) return 2; if ("*".equals(o)) return 3; if ("/".equals(o)) return 3; return -1; } private boolean comparison(String c1 ,Object c2) { return getLevel(c2)-getLevel(c1) >= 0; } }