java 解析字符串运算_JAVA解析字符串中的加减乘除四则运算的实现

packagecom.dwtedx.income.utility;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.regex.Matcher;importjava.util.regex.Pattern;/**

* Created by dwtedx(qinyl)(http://dwtedx.com) on 16/9/2.

* Company DD博客

* Description java字符串加法运算

*/publicclassReportUtil{/**

* 格式化数字

* @param obj 数字对象

* @param format 格式化字符串

* @return

*/publicstaticStringformatNumber(Objectobj,Stringformat){if(obj==null)return"";Strings=String.valueOf(obj);if(format==null||"".equals(format.trim())){format="#.00";}try{if(objinstanceofDouble||objinstanceofFloat){if(format.contains("%")){NumberFormatnumberFormat=NumberFormat.getPercentInstance();s=numberFormat.format(obj);}else{DecimalFormatdecimalFormat=newDecimalFormat(format);s=decimalFormat.format(obj);}}else{NumberFormatnumberFormat=NumberFormat.getInstance();s=numberFormat.format(obj);}}catch(Exceptione){}returns;}/**

* 计算字符串四则运算表达式

* @param string

* @return

*/publicstaticStringcomputeString(Stringstring){StringregexCheck="[\(\)\d\ \-\*/\.]*";// 是否是合法的表达式if(!Pattern.matches(regexCheck,string))returnstring;Matchermatcher=null;Stringtemp="";intindex=-1;Stringregex="\([\d\.\ \-\*/] \)";// 提取括号表达式string=string.replaceAll("\s","");// 去除空格try{Patternpattern=Pattern.compile(regex);// 循环计算所有括号里的表达式while(pattern.matcher(string).find()){matcher=pattern.matcher(string);while(matcher.find()){temp=matcher.group();index=string.indexOf(temp);string=string.substring(0,index)computeStirngNoBracket(temp)string.substring(index temp.length());}}// 最后计算总的表达式结果string=computeStirngNoBracket(string);}catch(NumberFormatExceptione){returne.getMessage();}returnstring;}/**

* 计算不包含括号的表达式

* @param string

* @return

*/privatestaticStringcomputeStirngNoBracket(Stringstring){string=string.replaceAll("(^\()|(\)$)","");StringregexMultiAndDivision="[\d\.] (\*|\/)[\d\.] ";StringregexAdditionAndSubtraction="(^\-)?[\d\.] (\ |\-)[\d\.] ";Stringtemp="";intindex=-1;// 解析乘除法Patternpattern=Pattern.compile(regexMultiAndDivision);Matchermatcher=null;while(pattern.matcher(string).find()){matcher=pattern.matcher(string);if(matcher.find()){temp=matcher.group();index=string.indexOf(temp);string=string.substring(0,index)doMultiAndDivision(temp)string.substring(index temp.length());}}// 解析加减法pattern=Pattern.compile(regexAdditionAndSubtraction);while(pattern.matcher(string).find()){matcher=pattern.matcher(string);if(matcher.find()){temp=matcher.group();index=string.indexOf(temp);if(temp.startsWith("-")){string=string.substring(0,index)doNegativeOperation(temp)string.substring(index temp.length());}else{string=string.substring(0,index)doAdditionAndSubtraction(temp)string.substring(index temp.length());}}}returnstring;}/**

* 执行乘除法

* @param string

* @return

*/privatestaticStringdoMultiAndDivision(Stringstring){Stringvalue="";doubled1=0;doubled2=0;String[]temp=null;if(string.contains("*")){temp=string.split("\*");}else{temp=string.split("/");}if(temp.length<2)returnstring;d1=Double.valueOf(temp[0]);d2=Double.valueOf(temp[1]);if(string.contains("*")){value=String.valueOf(d1*d2);}else{value=String.valueOf(d1/d2);}returnvalue;}/**

* 执行加减法

* @param string

* @return

*/privatestaticStringdoAdditionAndSubtraction(Stringstring){doubled1=0;doubled2=0;String[]temp=null;Stringvalue="";if(string.contains(" ")){temp=string.split("\ ");}else{temp=string.split("\-");}if(temp.length<2)returnstring;d1=Double.valueOf(temp[0]);d2=Double.valueOf(temp[1]);if(string.contains(" ")){value=String.valueOf(d1 d2);}else{value=String.valueOf(d1-d2);}returnvalue;}/**

* 执行负数运算

* @param string

* @return

*/privatestaticStringdoNegativeOperation(Stringstring){Stringtemp=string.substring(1);if(temp.contains(" ")){temp=temp.replace(" ","-");}else{temp=temp.replace("-"," ");}temp=doAdditionAndSubtraction(temp);if(temp.startsWith("-")){temp=temp.substring(1);}else{temp="-"temp;}returntemp;}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值