Implement four arithmetic expressions in Java

Source: Internet
Author: User

[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.