<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="skins/JinnStyleV1/article.xslt"?><blog view="689810" processed="0.085" queries="8" focusMarkup="" xslt="skins/JinnStyleV1/article.xslt"><panels><panel id="1" sn="0" sys="true"><name>Blog Main</name><content><article id="167" disComm="false"><title>[JAVA]放个可以做简单数学四则运算的东东</title><category id="20"><name>学习随笔</name><icon>images/icons/3.gif</icon></category><content><![CDATA[&#36935;&#21040;&#35201;&#27714;&#19968;&#20010;&#36816;&#31639;&#34920;&#36798;&#24335;&#30340;&#35745;&#31639;&#32467;&#26524;&#30340;&#38382;&#39064;<br />&#20256;&#36827;&#30340;&#26159;&#19968;&#20010;&#23383;&#31526;&#20018; (&#23383;&#20018;&#20869;&#23481;&#24403;&#28982;&#26159;&#34920;&#36798;&#24335;&#20102;)<br />&#26368;&#32456;&#35201;&#27714;&#20986;&#36816;&#31639;&#32467;&#26524;<br /><br />&#20043;&#21069;&#22312; JScript &#25110; VBScript &#37324;&#36890;&#24120;&#26159; eval &#26469;&#35299;&#20915;<br />&#19979;&#38754;&#32473;&#20010; Java &#30340;<br /><br />&#21482;&#33021;&#31639; +-*/ &#36824;&#26377;&#25324;&#21495;<br /><br /><textarea name='code' class='js' cols='50'>/**
 * (#)Calculator.java&#12288;&#12288;&#12288;&#12288;&#21019;&#24314;&#26102;&#38388;&#65306;Apr 30, 2009 6:14:03 PM&lt;br /&gt;
 */
package cn.ialvin.util;

import java.util.Stack;
import java.util.regex.Pattern;

/**
 * @author &#26519;&#24535;&#25996;(&lt;b&gt;ialvin.cn&lt;/b&gt;) &#24191;&#19996; &#26222;&#23425; &#37324;&#28246;
 */
public class Calculator {
    
    public static void main(String[] args) {
        String exp = &quot;-3.3 + 1.5 * (- 3 + -5)&quot;;
        Calculator calculator = new Calculator();
        System.out.println(calculator.cal(exp));
    }
    
    public double cal(String exp) {
        exp = adj(exp);
        exp = conver(exp);
        Stack&lt;Object&gt; stack = new Stack&lt;Object&gt;();
        String[] cs = exp.split(&quot;[^\\d.+\\-*/]+&quot;);
        int i = 0;
        while (i &lt; cs.length) {
            String c = cs<i>; i++;
            if (&quot;+&quot;.equals(c)) {
                stack.push((Double)stack.pop() + (Double)stack.pop());
            } else if (&quot;-&quot;.equals(c)) {
                stack.push(0 - (Double)stack.pop() + (Double)stack.pop());
            } else if (&quot;*&quot;.equals(c)) {
                stack.push((Double)stack.pop() * (Double)stack.pop());
            } else if (&quot;/&quot;.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(&quot;[^\\d.+\\-*\\/()]+&quot;, &quot;&quot;);
        exp = exp.replaceAll(&quot;(^|[(+\\-*\\/])\\-([\\d.]+)&quot;, &quot;$1(0-$2)&quot;);
        return exp.replaceAll(&quot;[+\\-*\\/()]&quot;, &quot; $0 &quot;).trim();
    }
    
    private String conver(String exp) {
        String[] str = exp.split(&quot;\\s+&quot;);
        Stack&lt;String&gt; expStack = new Stack&lt;String&gt;();
        for(int i = str.length - 1 ; i &gt;= 0 ; i--)
            expStack.push(str<i>);
        Stack&lt;String&gt; outStack = new Stack&lt;String&gt;();
        Stack&lt;String&gt; operStack = new Stack&lt;String&gt;();
        operStack.push(&quot;#&quot;);
        while (expStack.size()&gt; 0) {
            String c = expStack.pop().toString();
            if (c.matches(&quot;^\\d+(?:\\.\\d*)?$&quot;)) {
                outStack.push(c);
            } else if (&quot;(&quot;.equals(c)) {
                operStack.push(c);
            } else if (&quot;)&quot;.equals(c)) {
                if (operStack.lastElement().equals(&quot;(&quot;)) {
                    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(&quot;\\[|\\]|\\,&quot;, &quot;&quot;);
    }
    
    private int getLevel(Object o) {
        if (&quot;(&quot;.equals(o)) return 1;
        if (&quot;+&quot;.equals(o)) return 2;
        if (&quot;-&quot;.equals(o)) return 2;
        if (&quot;*&quot;.equals(o)) return 3;
        if (&quot;/&quot;.equals(o)) return 3;
        return -1;
    }
    
    private boolean comparison(String c1 ,Object c2) {
        return getLevel(c2)-getLevel(c1) &gt;= 0;
    }
}</textarea>]]></content><publish>2009-05-03 22:29:23</publish><update>2010-09-15 03:51:11</update><comment>0</comment><view>2232</view></article><comments/><previous id="165"><title>写了个 JavaScript  烟花</title></previous><next id="168"><title>[JAVA]大数开平方(模拟手算的方法)</title></next></content></panel></panels><modules><module id="4" sn="3" sys="true"><name>Category</name><title>日志分类</title></module><module id="6" sn="4" sys="true"><name>Archive</name><title>日志归档</title></module><module id="5" sn="5" sys="true"><name>User Panel</name><title>控制面板</title></module><module id="10" sn="6" sys="true"><name>Recent Article</name><title>最新日志</title></module><module id="8" sn="7" sys="true"><name>Search</name><title>查询搜索</title></module><module id="7" sn="8" sys="true"><name>Recent Comments</name><title>最新评论</title></module><module id="2" sn="9" sys="true"><name>Statistics</name><title>统计信息</title></module></modules><user><usn></usn><status>3</status><login>false</login></user></blog>
