[Java]
/**
* Arithmetic expression Calculation
* @ Author penli
*
*/
Public class Arithmetic {
Public static void main (String args []) {
System. out. println (arithmetic ("2.2 + (3 + 4) * 2-22)/2*3.2 "));
}
Public static double arithmetic (String exp ){
String result = parseExp (exp). replaceAll ("[\ [\]", "");
Return Double. parseDouble (result );
}
/**
* Parse and calculate four arithmetic expressions, for example, 2 + (3 + 4) * 2-22)/2*3
* @ Param expression
* @ Return
*/
Public static String parseExp (String expression ){
// String numberReg = "^ ((?! 0) \ d + (\. \ d + (? <! 0 ))?) | (0 \. \ d + (? <! 0) $ ";
Expression = expression. replaceAll ("\ s +", ""). replaceAll ("^ \ (. +) \) $", "$1 ");
String checkExp = "\ d ";
String minExp = "^ (\ d + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ + \-\ * \/] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) $ ";
// Minimum expression Calculation
If (expression. matches (minExp )){
String result = calculate (expression );
Return Double. parseDouble (result)> = 0? Result: "[" + result + "]";
}
// Calculate the four arithmetic operations without parentheses
String noParentheses = "^ [^ \ (\)] + $ ";
String priorOperatorExp = "(\ d + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ * \/] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) ";
String operatorExp = "(\ d + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ + \-] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) ";
If (expression. matches (noParentheses )){
Pattern patt = Pattern. compile (priorOperatorExp );
Matcher mat = patt. matcher (expression );
If (mat. find ()){
String tempMinExp = mat. group ();
Expression = expression. replaceFirst (priorOperatorExp, parseExp (tempMinExp ));
} Else {
Patt = Pattern. compile (operatorExp );
Mat = patt. matcher (expression );
If (mat. find ()){
String tempMinExp = mat. group ();
Expression = expression. replaceFirst (operatorExp, parseExp (tempMinExp ));
}
}
Return parseExp (expression );
}
// Calculates the four arithmetic operations with parentheses.
String minParentheses = "\ ([^ \ (\)] + \\)";
Pattern patt = Pattern. compile (minParentheses );
Matcher mat = patt. matcher (expression );
If (mat. find ()){
String tempMinExp = mat. group ();
Expression = expression. replaceFirst (minParentheses, parseExp (tempMinExp ));
}
Return parseExp (expression );
}
/**
* Minimum unit: Four arithmetic expressions (two numbers)
* @ Param exp
* @ Return www.2cto.com
*/
Public static String calculate (String exp ){
Exp = exp. replaceAll ("[\ [\]", "");
String number [] = exp. replaceFirst ("(\ d) [\ + \-\ * \/]", "$1 ,"). split (",");
BigDecimal number1 = new BigDecimal (number [0]);
BigDecimal number2 = new BigDecimal (number [1]);
BigDecimal result = null;
String operator = exp. replaceFirst ("^. * \ d ([\ + \-\ * \/]). + $ "," $1 ");
If ("+". equals (operator )){
Result = number1.add (number2 );
} Else if ("-". equals (operator )){
Result = number1.subtract (number2 );
} Else if ("*". equals (operator )){
Result = number1.multiply (number2 );
} Else if ("/". equals (operator )){
Result = number1.divide (number2 );
}
Return result! = Null? Result. toString (): null;
}
}
Author: lip009