注册 登录
重磅消息:开通VIP会员,获取平台所有项目,永久使用,详细请咨询QQ:3376762016
远程服务部署安装,售后服务,请加QQ1:3376762016,QQ2:3597230140(即将满员),QQ3:1399491757(已满)
查看: 650|回复: 0
打印 上一主题 下一主题

实现一个方法,接收一个字符串类型简单算术表达式,没有括号,数字在0-9之间,返回.

[复制链接]

该用户从未签到

3518

主题

3532

帖子

66万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
668970
QQ
跳转到指定楼层
楼主
发表于 2016-10-19 14:24:27 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
实现一个方法,接收一个字符串类型简单算术表达式,没有括号,数字在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, 下载次数: 115)

拼图一.png
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
java无忧网(http://www.javawyw.com),专业提供Java大作业,小作业,课程设计,毕业设计源码,答辩辅导,作业排错,bug修复,专业解答,远程部署运行项目等服务
本网站所有源码,保证能运行!
QQ:1399491757
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回列表 返回顶部