Blog Stats
Posts - 53
Articles - 4
Comments - 59
Trackbacks - 0
News
我使用新博客啦:
http://www.linjunhai.com/
大家到我的新博客上看看吧!
隨筆分類
(28)
JAVA天地(17)
其它相關(11)
文章分類
(4)
心情日志(4)
隨筆檔案
(53)
2011年5月 (2)
2011年4月 (1)
2011年3月 (1)
2010年12月 (1)
2010年9月 (1)
2010年8月 (1)
2010年5月 (1)
2010年3月 (1)
2009年11月 (1)
2009年10月 (1)
2009年7月 (1)
2009年5月 (2)
2009年1月 (2)
2008年12月 (11)
2008年11月 (2)
2008年10月 (1)
2008年9月 (1)
2008年7月 (3)
2008年6月 (1)
2007年10月 (1)
2007年8月 (1)
2007年7月 (3)
2007年4月 (1)
2007年3月 (3)
2006年12月 (4)
2006年11月 (5)
文章檔案
(4)
2007年5月 (2)
2007年4月 (2)
相冊
我的相冊
相關鏈接
Alvin's Blog
我現在使用的新博客.
CSS3 中文手冊
簡單實用的在線 CSS3 中文手冊
Scripts 學盟
不再悲催,體驗編寫腳本的樂趣!
給我留言
BlogJava 的留言板不用了, 用這個啦.
林俊海的博客
超級大菜鳥,每天要自強!
[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++; 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
); 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; } }
posted on 2009-05-03 22:29
林俊海
閱讀(538)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
.Text
and
ASP.NET
- Copyright © 林俊海
主站蜘蛛池模板:
庆云县
|
北安市
|
大名县
|
玛纳斯县
|
雅江县
|
威宁
|
宽甸
|
临城县
|
友谊县
|
扬中市
|
萝北县
|
布尔津县
|
九寨沟县
|
水富县
|
汝城县
|
庆元县
|
张北县
|
达孜县
|
哈尔滨市
|
迭部县
|
神农架林区
|
临江市
|
噶尔县
|
正镶白旗
|
工布江达县
|
金阳县
|
乌什县
|
松滋市
|
渭源县
|
灵石县
|
洪泽县
|
临清市
|
芦溪县
|
宜宾县
|
合江县
|
平江县
|
济源市
|
婺源县
|
尚志市
|
南开区
|
田东县
|