Four arithmetic operations in java and four arithmetic operations in java

Source: Internet
Author: User

Four arithmetic operations in java and four arithmetic operations in java

The idea of the code is to use regular expressions to determine and calculate each smallest computing unit. The following code is used:

1 import java. math. bigDecimal; 2 import java. util. regex. matcher; 3 import java. util. regex. pattern; 4 5/** 6 * calculator tool class 7 * @ author shuqi 8 * @ date 2015-7-23 9 * @ version since 1.0 10 */11 public class CalculatorUtil {12 13 public static BigDecimal arithmetic (string exp) {14 if (! Exp. matches ("\ d +") {15 String result = parseExp (exp ). replaceAll ("[\ [\]", ""); 16 return new BigDecimal (result); 17} else {18 return new BigDecimal (exp ); 19} 20} 21/** 22 * Minimum counter Unit 23*24 */25 private static String minExp = "^ (\ d + (\\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ + \-\ * \/] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) $ "; 26/** 27 * Calculation 28 */29 private static String noParentheses =" ^ [^ \ (\)] + $ "; 30/** 31 * match multiplication or division 32 */33 private static String priorOperatorExp =" (\ d + (\\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ * \/] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) "; 34/** 35 * matching addition and subtraction 36 */37 private static String operatorExp =" (\ d + (\\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) [\ + \-] (\ D + (\. \ d + )?) | (\ [\-\ D + (\. \ d + )? \]) "; 38/** 39 * match the 40 */41 private static String minParentheses = "\ ([^ \ (\)] + \) with only one parentheses \\)"; 42 43/** 44 * parse and calculate four arithmetic expressions, for example, 2 + (3 + 4) * 2-22) /2*3 45 * @ param expression 46 * @ return 47 */48 private static String parseExp (String expression) {49 // enter the method to replace spaces first, remove the () Number 50 expression = expression on both sides of the operation. replaceAll ("\ s + ",""). replaceAll ("^ \ ([^ \ (\)] +) \) $", "$1 "); 51 52 // minimum expression calculation 53 if (expression. matches (m InExp) {54 String result = calculate (expression); 55 return Double. parseDouble (result)> = 0? Result: "[" + result + "]"; 56} 57 // calculate the four arithmetic operations without parentheses 58 if (expression. matches (noParentheses) {59 Pattern patt = Pattern. compile (priorOperatorExp); 60 Matcher mat = patt. matcher (expression); 61 if (mat. find () {62 String tempMinExp = mat. group (); 63 expression = expression. replaceFirst (priorOperatorExp, parseExp (tempMinExp); 64} else {65 patt = Pattern. compile (operatorExp); 66 mat = patt. matcher (expression); 67 68 if (Mat. find () {69 String tempMinExp = mat. group (); 70 expression = expression. replaceFirst (operatorExp, parseExp (tempMinExp); 71} 72} 73 return parseExp (expression); 74} 75 76 // calculates the arithmetic operation 77 Pattern patt = Pattern with parentheses. compile (minParentheses); 78 Matcher mat = patt. matcher (expression); 79 if (mat. find () {80 String tempMinExp = mat. group (); 81 expression = expression. replaceFirst (minParentheses, parseExp (tempMin Exp); 82} 83 return parseExp (expression); 84} 85/** 86 * calculates the minimum unit of four arithmetic expressions (two numbers) 87 * @ param exp 88 * @ return 89 */90 private static String calculate (String exp) {91 exp = exp. replaceAll ("[\ [\]", ""); 92 String number [] = exp. replaceFirst ("(\ d) [\ + \-\ * \/]", "$1 ,"). split (","); 93 BigDecimal number1 = new BigDecimal (number [0]); 94 BigDecimal number2 = new BigDecimal (number [1]); 95 BigDecimal result = Null; 96 97 String operator = exp. replaceFirst ("^. * \ d ([\ + \-\ * \/]). + $ "," $1 "); 98 if (" + ". equals (operator) {99 result = number1.add (number2); 100} else if ("-". equals (operator) {101 result = number1.subtract (number2); 102} else if ("*". equals (operator) {103 result = number1.multiply (number2); 104} else if ("/". equals (operator) {105 // The second parameter is precision, and the third is the four-color five-in mode 106 result = number1.divide (number2, 5, BigDecimal. ROUND_C EILING); 107} 108 109 return result! = Null? Result. toString (): null; 110} 111 112}

The Code was originally a blog, but it cannot be remembered. The original code has no comments and there is a BUG. I have revised it a little and added comments. Make a note here for future use

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.