java无忧网

标题: 实现一个方法,接收一个字符串类型简单算术表达式,没有括号,数字在0-9之间,返回. [打印本页]

作者: java无忧网    时间: 2016-10-19 14:24
标题: 实现一个方法,接收一个字符串类型简单算术表达式,没有括号,数字在0-9之间,返回.
实现一个方法,接收一个字符串类型简单算术表达式,没有括号,数字在0-9之间,返回计算结果,所有的中间结果化为整形。

例如:接收参数:3+8×2/9-2 ;返回结果:2(不能用第三方的表达式jar包)

(请写出详细的思路,不需要写完整的编程语言。)


public class Arithmetic {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String ex = "3+8*2/9-2";
       int result=(int)complete(ex);
       System.out.println(result);
    }

    public static double complete(String args) {
        String strResult = "";
        char[] operators = {'*','/','+','-'};
        for(int i=0;i<operators.length;i++) {
            strResult = subComplete(args,operators);
            args = strResult;
        }
        return Double.parseDouble(strResult);
    }

    public static String subComplete(String args,char opera) {
        int operateIndex = 0;               //运算符索引
        int temp = 0;                       //临时变量,存放索引值
        double a = 0d;                      //数值a
        double b = 0d;                      //数值b
        String beforeOperaString = "";      //运算符之前的字符串
        String afterOperaString = "";       //运算符之后的字符串
        String strNumA = "";                //字符串类型的数字a
        String strNumB = "";                //字符串类型的数字b
        String strLeft = "";                //正在计算的最简表达式左边的串
        String strRight = "";               //正在计算的最简表达式右边的串
        String result = "";                 //运算结果(运算结果=最简表达式之前的字符串+最简表达式的值+最简表达式之后的字符串)

        operateIndex = args.indexOf(opera);
        if(operateIndex==-1) {
            return args;
        }
        //以运算符为界将字符串分为两节
        beforeOperaString = args.substring(0, operateIndex);           
        afterOperaString = args.substring(operateIndex+1);               
        //取出运算符两边的数,并得到正在计算的最简表达式左右两边的表达式串      
        temp = findCharIndex(beforeOperaString,false);
        strNumA= beforeOperaString.substring(temp==0?temp:temp+1);      
        if(temp!=0) {
            strLeft = beforeOperaString.substring(0, temp+1);     
        }
        temp = findCharIndex(afterOperaString,true);
        strNumB = afterOperaString.substring(0, temp==0?afterOperaString.length():temp);      
        if(temp!=0) {
            strRight = afterOperaString.substring(temp);         
        }
        a = Double.parseDouble(strNumA);
        b = Double.parseDouble(strNumB);
        if(opera=='*') result = strLeft+(a*b)+strRight;
        else if(opera=='/') result = strLeft+(a/b)+strRight;
        else if(opera=='+') result = strLeft+(a+b)+strRight;
        else if(opera=='-') result = strLeft+(a-b)+strRight;

        return subComplete(result,opera);
    }
    /**
     * 获取一个表达式中第一个或者最后一个运算符的索引值
     * @param str - 被检查的字符串
     * @param fromBegin - 如果true,用index查找最简表达式右边第一个运算符;
     *                    如果false,用lastIndex查找最简表达式左边边最后一个运算符。
     * @return 运算符索引
     */
    public static int findCharIndex(String str,boolean fromBegin) {
        int index = 0;
        int temp = 0;
        if(fromBegin) {
            temp = str.indexOf('*');
            index = (temp!=-1)?temp:index;
            temp = str.indexOf('/');
            index = (temp!=-1 && (index==0 || temp<index))?temp:index;
            temp = str.indexOf('+');
            index = (temp!=-1 && (index==0 || temp<index))?temp:index;
            temp = str.indexOf('-');
            index = (temp!=-1 && (index==0 || temp<index))?temp:index;
            return index;
        }
        temp = str.lastIndexOf('*');
        index = (temp!=-1)?temp:index;
        temp = str.lastIndexOf('/');
        index = (temp!=-1 && (index==0 || temp>index))?temp:index;
        temp = str.lastIndexOf('+');
        index = (temp!=-1 && (index==0 || temp>index))?temp:index;
        temp = str.lastIndexOf('-');
        index = (temp!=-1 && (index==0 || temp>index))?temp:index;
        return index;
    }

}




拼图一.png (279.25 KB, 下载次数: 1589)

拼图一.png





欢迎光临 java无忧网 (http://www.javawyw.com/) Powered by Discuz! X3.2