Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1937288
  • 博文数量: 219
  • 博客积分: 8963
  • 博客等级: 中将
  • 技术积分: 2125
  • 用 户 组: 普通用户
  • 注册时间: 2005-10-19 12:48
个人简介

文章分类

全部博文(219)

文章存档

2021年(1)

2020年(3)

2015年(4)

2014年(5)

2012年(7)

2011年(37)

2010年(40)

2009年(22)

2008年(17)

2007年(48)

2006年(31)

2005年(4)

分类: Java

2011-02-25 10:10:45

代码先是在网上找的,但有BUG,我加了几句.现在对带括号的四项运算是没问题的.

public class App1
{
    /**
     * @param args
     */

    public static void main(String[] args)
    {
        try
        {
            System.out.println(cal("24+1*2/1-9+(-20+(+3))"));
            System.out.println(cal("1-3*(4-(2+5*3)+5)-6/(1+2)"));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static String cal(String str)
    {
        // 方法内应做除零异常的捕获处理

        if (str.indexOf("(") >= 0)
        {
            String mid = str.substring(str.lastIndexOf("(") + 1);
            String tem = mid.substring(0, mid.indexOf(")"));

            str = str.replace("(" + tem + ")", cal(tem));

            if (str.indexOf("+") > 0 || str.indexOf("-") > 0 || str.indexOf("*") > 0 || str.indexOf("/") > 0
                    || str.indexOf("(") > 0)
            {
                return cal(str);
            }
            else
                return str;
        }
        if (str.indexOf("+") >= 0)
        {
            String str1 = str.substring(0, str.lastIndexOf("+"));
            String str2 = str.substring(str.lastIndexOf("+") + 1);
            if (str1.indexOf("+") > 0 || str1.indexOf("-") > 0 || str1.indexOf("*") > 0 || str1.indexOf("/") > 0)
            {
                str1 = cal(str1);
            }
            if (str2.indexOf("+") > 0 || str2.indexOf("-") > 0 || str2.indexOf("*") > 0 || str2.indexOf("/") > 0)
            {
                str2 = cal(str2);
            }
            // i added

            if (str1 == null || str1.length() == 0)
                str1 = "0";
            return "" + (Float.parseFloat(str1) + Float.parseFloat(str2));
        }
        if (str.indexOf("-") >= 0)
        {
            String str1 = str.substring(0, str.lastIndexOf("-"));
            String str2 = str.substring(str.lastIndexOf("-") + 1);
            if (str1.indexOf("+") > 0 || str1.indexOf("-") > 0 || str1.indexOf("*") > 0 || str1.indexOf("/") > 0)
            {
                str1 = cal(str1);
            }
            if (str2.indexOf("+") > 0 || str2.indexOf("-") > 0 || str2.indexOf("*") > 0 || str2.indexOf("/") > 0)
            {
                str2 = cal(str2);
            }
            // i added

            if (str1 == null || str1.length() == 0)
                str1 = "0";
            return "" + (Float.parseFloat(str1) - Float.parseFloat(str2));
        }
        if (str.indexOf("*") >= 0)
        {
            String str1 = str.substring(0, str.lastIndexOf("*"));
            String str2 = str.substring(str.lastIndexOf("*") + 1);
            if (str1.indexOf("+") > 0 || str1.indexOf("-") > 0 || str1.indexOf("*") > 0 || str1.indexOf("/") > 0)
            {
                str1 = cal(str1);
            }
            if (str2.indexOf("+") > 0 || str2.indexOf("-") > 0 || str2.indexOf("*") > 0 || str2.indexOf("/") > 0)
            {
                str2 = cal(str2);
            }
            // i added

            if (str1 == null || str1.length() == 0)
                str1 = "1";
            return "" + (Float.parseFloat(str1) * Float.parseFloat(str2));
        }
        if (str.indexOf("/") >= 0)
        {
            String str1 = str.substring(0, str.lastIndexOf("/"));
            String str2 = str.substring(str.lastIndexOf("/") + 1);
            if (str1.indexOf("+") > 0 || str1.indexOf("-") > 0 || str1.indexOf("*") > 0 || str1.indexOf("/") > 0)
            {
                str1 = cal(str1);
            }
            if (str2.indexOf("+") > 0 || str2.indexOf("-") > 0 || str2.indexOf("*") > 0 || str2.indexOf("/") > 0)
            {
                str2 = cal(str2);
            }
            return "" + (Float.parseFloat(str1) / Float.parseFloat(str2));
        }
        return str;
    }
}


阅读(1618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~