Arithmetic script parsing calculation
1. Single digit addition and subtraction operation
Remember the plus or minus operator of the previous bit, and use the plus or minus sign to change the accumulated value with the number following the operator.
PackageYuki.arithmetic.script; Public classSingledigittest { Public Static voidMain (string[] args) {String S1= "1+2+3+4+5+6+7+8+9"; System.out.println (S1+ "=" +calculate (S1)); String S2= "1-2+3+4-5+6+7-8+9"; SYSTEM.OUT.PRINTLN (S2+ "=" +Calculate (S2)); } Private Static intCalculate (String s) {Char[] cs =S.tochararray (); CharOper = ' + '; intresult = 0; for(inti = 0; i < cs.length; i++) { Charc =Cs[i]; if(c = = ' + ' | | c = = '-')) {oper=C; }Else if(' 0 ' <= C && c <= ' 9 ') { intx = C-' 0 '; Switch(oper) { Case+: Result+=x; Break; Case‘-‘: Result-=x; Break; } } } returnresult; }}
The results of the operation are as follows:
1+2+3+4+5+6+7+8+9=451-2+3+4-5+6+7-8+9=15
2. Using the scripting engine to calculate
Java provides a script parsing tool, if the parsed script is a JavaScript script, the implementation class is Rhinoscriptengine.
PackageYuki.arithmetic.script;ImportJavax.script.ScriptEngine;ImportJavax.script.ScriptEngineManager;Importjavax.script.ScriptException; Public classScriptenginetest { Public Static voidMain (string[] args)throwsscriptexception {Scriptenginemanager Enginemanager=NewScriptenginemanager (); ScriptEngine Engine= Enginemanager.getenginebyname ("javascript"); String Script= "1*2*3*4*5*6*7*8*9"; Object result=engine.eval (script); System.out.println (Script+ " = " +result); System.out.println (Script+ "=" + 1*2*3*4*5*6*7*8*9); }}
The results of the operation are as follows:
1*2*3*4*5*6*7*8*9 = 362880.01*2*3*4*5*6*7*8*9 = 362880
3. Arithmetic with brackets
Convenient to test, the previous line is using the following arithmetic parsing, the next line uses JavaScript script parsing.
To avoid the calculation of incorrect input scripts, the whitespace characters are removed before the calculation, verifying that the script enters the specification.
From the back-to-front parsing script, this is consistent with the normal calculation order, because recursion is used to calculate the previous result and then the result of the subsequent operation.
If there is a minus sign on the way from the parsing script after the trip, the calculation error is caused by the subsequent formula as a whole in parentheses to participate in the subtraction.
PackageYuki.arithmetic.script;ImportJavax.script.ScriptEngine;ImportJavax.script.ScriptEngineManager;Importjavax.script.ScriptException; Public classResolverdemo { Public Static voidMain (string[] args)throwsscriptexception {String S1= "1+2+ (3+4) + (5+ (6*7+8)) +9"; SYSTEM.OUT.PRINTLN (Calculate (read (S1))); System.out.println (Evalscript (S1)); String S2= "1+2+ (3/4) + (5+ (6*7+8/9))"; SYSTEM.OUT.PRINTLN (Calculate (read (S2))); System.out.println (Evalscript (S2)); String S3= "((1+ (3/4) + (5+ (6*78+9)))"; SYSTEM.OUT.PRINTLN (Calculate (Read (S3))); System.out.println (Evalscript (S3)); String S4= "1-(+ (3/4)) + ( -6*7*8+9)"; SYSTEM.OUT.PRINTLN (Calculate (Read (S4))); System.out.println (Evalscript (S4)); String S5= " -12*3 + 4*56-(7 + 8*9)/(2*3)"; SYSTEM.OUT.PRINTLN (Calculate (Read (S5))); System.out.println (Evalscript (S5)); } Public StaticObject Evalscript (String script) {ScriptEngine engine=NewScriptenginemanager (). Getenginebyname ("JavaScript"); Try { returnengine.eval (script); } Catch(scriptexception e) {e.printstacktrace (); return-1; } } Public Staticstring Read (string s) {s= S.replaceall ("\\s+", "" "); Char[] cs =S.tochararray (); for(inti = 0, j = 1; J < Cs.length; ++i, + +j)if((cs[i] = = ' + ' | | cs[i] = = '-' | | cs[i] = = ' * ' | | cs[i] = = '/') &&(Cs[j]= = ' + ' | | CS[J] = = '-' | | CS[J] = = ' * ' | | CS[J] = = '/')) Throw NewRuntimeException ("operator adjacent"); intDeep = 0; for(CharC:cs) { if(c = = ' (') ++Deep ; Else if(c = = ') ') --Deep ; } if(Deep! = 0) Throw NewRuntimeException ("Left and right brackets vary")); returns; } Public Static DoubleCalculate (String s) {Char[] cs =S.tochararray (); if(Cs.length = = 0) return0; intpos =-1; if(pos = Pluspos (cs))! =-1){ CharOper =Cs[pos]; Doubleleft = calculate (s.substring (0, POS)); Doubleright = Calculate (s.substring (pos + 1)); if(Oper = = ' + ')) returnLeft +Right ; Else if(Oper = = '-')) returnLeft-Right ; }Else if(pos = Timespos (cs))! =-1){ CharOper =Cs[pos]; Doubleleft = calculate (s.substring (0, POS)); Doubleright = Calculate (s.substring (pos + 1)); if(Oper = = ' * ') returnLeft *Right ; Else if(Oper = = '/')) returnLeft/Right ; }Else{ if(Cs[0] = = ' (' && cs[cs.length-1] = = ') '){ returnCalculate (s.substring (1, cs.length-1)); }Else{ inty = 0; for(CharC:cs)if(' 0 ' <= C && c <= ' 9 ') y= Y*10 + (c-' 0 ')); if(Cs[0] = = '-') y*=-1; returny; } } return0; } Private Static intPluspos (Char[] CS) { intDeep = 0; for(inti = cs.length-1; I >= 0; --i) {Charc =Cs[i]; Switch(c) { Case‘(‘: ++Deep ; Break; Case‘)‘: --Deep ; Break; Case+: Case‘-‘: if(Deep = = 0) returni; Break; } } return-1; } Private Static intTimespos (Char[] CS) { intDeep = 0; for(inti = cs.length-1; I >= 0; --i) {Charc =Cs[i]; Switch(c) { Case‘(‘: ++Deep ; Break; Case‘)‘: --Deep ; Break; Case‘*‘: Case‘/‘: if(Deep = = 0) returni; Break; } } return-1; } }
The results of the operation are as follows:
74.074.051.63888888888888651.638888888888886485.75485.75331.75331.75174.83333333333334174.83333333333334
This address:http://www.cnblogs.com/kodoyang/p/Arithmetic_ScriptResolve_Calculate.html
Rain Yoko
August 29, 2015
Arithmetic script parsing calculation