Formula parsing class written in Java

xiaoxiao2021-03-06  48

Beginner Java, a class that needs to be parsed in the work. Search from the Internet, the function is slightly lacking, and the four operations can be implemented after the modification. In order to express my gratitude to the netizen Leasion, I hope to help encounter the same problem, the open source is as follows: / ** *

Title: Formula Analysis class *

Description: Implementation "( Expression of the expression for simple four operations. *

Copyright: Copyright (c) 2004 *

Company: * @Author leasion * @version 1.0 * / / ** * originally loaded in: http: / /dev2dev.bea.com.cn/bbs/thread.jspa?forumid=124&thread=14861&tstart=90 * Modifier: LKZ * Modification Description: * 1. Add to negative operation support; * 2. Add to the formula; * 3. Add support for similar formulas (A B) (CD). * 4. Other small changes. * * / import java.util.vector;

public class FormulaParser {private int leftBracket = 0; // number of left parenthesis private int rightBracket = 0; // number of right parenthesis private int startL = 0; private int startR = 0; private double answer = 0; private String strValue = "" "; Private string rightnumber =" 0 "; public string msg ="; private string formula = "; private int [] SYM = new int = new int = Vector ();

public FormulaParser (String calRule) {this.setFormula (calRule);} private int getLeftBracket (String calRule) {leftBracket = 0; startL = calRule.indexOf ( "(");! if (startL = -1) {calRule = calRule .substring (StartL 1, Calrule.Length ());} while (startl! = -1) {Leftbracket ; startl = CALRULE.INDEXOF ("); CALRULE = CALRULE.SUBSTRING (Startl 1, Calrule.Length ());} Return leftbracket;}

private void setLeftBracket (int leftBracket) {this.leftBracket = leftBracket;} public void setFormula (String calRule) {formula = replaceSubtration (calRule.trim ()); formula = "(" formula ")";} / * / * In order to support the negative number in the formula, use "` "to indicate the reduction, use" - "to indicate the negative number * / prizter string replacesis (string vstr) {string tmp ="; string result = "; int Starts = vStr.Indexof ("-"); if (starts! = - 1) {if (starts> 0) {TMP = vstr.substring (Starts - 1, Starts); if (! " ". Equals (tmp) &&! " ". Equals (TMP) &&!" * ". Equals (TMP) &&!" / ". Equals (TMP) &&!" (". Equals (tmp)) {result = result vStr.SUBSTRING (0, Starts) "` ";} Else result = result vstr.substring (0, starts 1);} else result = result vStr.SUBSTRING (0, Starts 1); vStr = vStr.Substring (Starts 1); } While (starts! = -1) {starts = vStr.indexof ("-"); if (Star TS> 0) {TMP = VSTR.SUBSTRING (Starts - 1, Starts); if (! " ". Equals (tmp) &&! "-". Equals (tmp) &&! "*". Equals (TMP) && ! "/". Equals (TMP) &&! "(". Equals (tmp) Result = result vstr.substring (0, starts) "` "; else result = result vStr.SUBSTRING (0, Starts 1);} else result = result

vStr.substring (0, starts 1); vStr = vStr.substring (Starts 1);} result = vStr; return fruit;} public string getformula () {Return Formula.Replace ('`', '-') . Substring (1, Formula.length () - 1);

Private int getRightbracket (String Calrule) {rightbracket = 0; startr = caverrule.indexof (")"); if (startr! = -1) {CALRULE = CALRULE.SUBSTRING (Startr 1, Calrule.Length ());} While (StarTr! = -1) {RightBracket ; Startr = CALRULE.INDEXOF (")"); CALRULE = CALRULE.SUBSTRING (Startr 1, CALRULE.LENGTH ());} RightBracket;}

Private void setRightbracket {this.rightbracket = rightbracket;

/ * / * Comparison left and right brackets * / private boolean comparetolr () {int lb = getleftbracket (formula); int RB = getrightbracket; Boolean CTLR = false; if (lb == rb) {msg = "" CTLR = true;} else if (lb> rb) {msg = "The number of left parentheses is more than the right bracket, please check!"; CTLR = false;} else {msg = "The number of left parentheses is less than right Including the arc, please check! "; CTLR = false;} Return CTLR;} / * / * Check if there is an illegal character in the formula ( , -) * / private boolean checkformula () {boolean isok = true; string [] Bracket = new string [2]; string [] sign = new string [4]; bracket [0] = "("; bracket [1] = ")"; SIGN [0] = " "; SIGN [1] = "` "; SIGN [2] =" * "; SIGN [3] =" / "; string vstr =" "; for (int i = 0; i 0 ) {Msg = "illegal character" vStr; ISOK = FALSE; RETURN ISOK;}}}}}}}}}}}}} for (int i = 0; I

}}} IF (")") ")")> 0) {msg = "formula exists in illegal character ()"; isok = false;} Return Isok;} public boolean checkvalid () {ix ((Formula = = NULL) || (Formula.trim (). Length () <= 0)) {msg = "Please set attribute CALRULE!"; Return False;} Return (Comparetolr () && checkformula ());

/ * / * Return formula Execution Result Return Double * / Public Double getResult () {string formulastr = "", calrule = ""; double value = 0.0; calrule = this.formula; if (checkvalid ()) {for (int) i = 0; i

))). Trim () value calrule.substring (")") 1, CALRULE.LENGTH ()). Trim (); system.out.println ("calrule =" calrule);} }} / * / * Data to the final parentheses to list * / private void symbolpival (string str) {list.clear (); for (int i = 0; i <4; i ) {comparemin (STR); while SYM [I]! = -1) {String INSSTR = Str.Substring (0, SYM [I]). Trim (); list.add (INSSTR); INSSSSSSSTR = Str.Substring (Sym [i], SYM [i ] 1) .trim (); list.add (INSST); str = str.substring (SYM [i] 1, str.length ()). Trim (); Comparemin (STR);}}} [0] == -1 && Sym [1] == -1 && Sym [2] == -1 & Sym [3] == -1) {list.add (str);}} / * / * cycle Comparison substring start value * / private void comparemin (string str) {int sps = str.indexof ("` "); // subtraction Subtration Sym [0] = SPS; int spa = str .index Of (" "); // addition addition sym [1] = spa; int spd = str.indexof ("/"); // division Division Sym [2] = SPD; int spm = str.indexof ("* "); // Multiplication Multiplication SYM [3] = SPM; for (int i = 1; i SYM [J 1]) {INT TEMP = SYM [J]; SYM [J] = SYM [J 1]; SYM [J 1] = Temp;}}}

private double parseString () throws NumberFormatException, StringIndexOutOfBoundsException {try {calculate (); return answer;} catch (Exception e) {Msg = "Error:" e.getMessage (); return 0.0;}}

Private void Calculate () {/ * / * Process division * / int SPD = list.indexof ("/"); while (spd! = -1) {leftNumber = list.get (SPD - 1) .tostring (); Rightnumber = list.get (SPD 1) .tostring (); list.remove (SPD - 1); list.remove (SPD - 1); list.remove (SPD - 1); double ln = double.valueof (LeftNumber ) .doublevalue (); double rn = double.valueof (rightnumber) .doublevalue (); double answer = ln / rn; list.add (SPD - 1, String.Valueof (answer); SPD = list.indexof / ");} / * / * Processing multiplication * / int spm = list.indexof (" * "); while (spm! = -1) {leftNumber = list.get (spm - 1) .tostring (); RightNumber = list.get (SPM 1) .tostring (); list.remove (spm - 1); list.remove (spm - 1); list.remove (spm - 1); double ln = double.valueof (leftnumber) .doublevalue (); Double Rn = double.Valueof (RightNumber) .doublevalue (); double answer = ln * rn; list.add (spm - 1, string.valueof (answer); spm = list.indexof ("*");} / * / * Processing subtraction * / int sps = list.indexof ("` "); while (sps! = -1) {leftnumber = list.get (SPS - 1) .tostring (); rightnumber = list.get (SPS 1) .tostring (); list.remove (SPS - 1); list.remove (SPS - 1); list.remove (SPS - 1); double ln = double.valueof (leftnumber) .doublevalue (); double Rn =

转载请注明原文地址:https://www.9cbs.com/read-74269.html

New Post(0)