|
|
Re: Need to evaluate a String to make graphing calc
|
Posted: Oct 18, 2004 12:59 AM
|
|
This method was written for a input line parser. It's not the highest art of programming, but it works.
To create something like the calculator for windows is much easier, since you don't have to parse strings doing that. Just start a new operation if someone hits the button for find '(' and combine the result of the new operation with the old operation if someone hits ')'. You don't even have to remember the single steps.
private Double mathParser (String s, final char decimalSeparator) { Vector datOpVector = new Vector(); final boolean doOutput = false; double currentValue = 0; double fractionMultiplier = 1; boolean isFraction = false; boolean valueComplete; boolean opFound = false; boolean valueFound = false; char previousOperation = '+'; java.lang.Character charOperator = null; if ((null == s) || (s.length()<1)) return null; int pos = 0; if (doOutput) System.out.println ("Input: '" + s + "'"); do { char c = s.charAt (pos); valueComplete = true; opFound = false; switch (c) { case '(': //Alles von diesem Zeichen an in eine neue Rechnung int abschlussKlammer = findeAbschlussKlammer (s.substring (pos+1)); if (abschlussKlammer < 0) return null; //Rechnung kann nur ausgewertet werden, wenn alle Klammerrechnungen vollständg sind String newString = s.substring (pos+1, pos+abschlussKlammer+1); Double klammerWert = mathParser (newString, decimalSeparator); pos += (abschlussKlammer+1); if (klammerWert == null) return null; currentValue = klammerWert.doubleValue(); valueComplete = false; valueFound = true; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': //Ziffer currentValue = currentValue * ((!isFraction)?10:1) + ((double)java.lang.Character.getNumericValue(c)) * fractionMultiplier; valueComplete = false; valueFound = true; break; case '-': case '+': case '/': case '*': //Operator opFound = true; charOperator = new java.lang.Character (c); break; default: //ungültiges Zeichen if (doOutput) System.out.println ("Default"); if ((c == ',') || (c == '.')) { if (isFraction) return null; // zwei Kommazeichen in einer Zahl sind unmöglich. valueComplete = (pos >= s.length()-1); if (doOutput) System.out.println ("Kommazeichen : " + valueComplete); isFraction = true; } else { if (doOutput) System.out.println ("minicalc: Error parsing '" + s + "'"); if (doOutput) System.out.println ("minicalc: position: " + pos + "; character: " + c); return null; } } if (valueComplete) { if (doOutput) System.out.println ("previous operation = " + previousOperation); isFraction = false; fractionMultiplier = 1; if (valueFound) switch (previousOperation) { case '-': datOpVector.add(new Double (-currentValue)); if (doOutput) System.out.println ("Adding - " + currentValue + " = " + -currentValue); break; case '/': datOpVector.add(new Double (1 / currentValue)); if (doOutput) System.out.println ("Adding 1 / " + currentValue + " = " + 1/currentValue); break; default: datOpVector.add(new Double (currentValue)); if (doOutput) System.out.println ("Adding " + currentValue); break; } if (opFound) { if (doOutput) System.out.println ("Operator found: " + c);
previousOperation = c; if (valueFound) { switch (c) { case '-': if (doOutput) System.out.println ("Adding '+'"); datOpVector.add (new Character ('+')); break; case '/': if (doOutput) System.out.println ("Adding '*'"); datOpVector.add (new Character('*')); break; default: if (doOutput) System.out.println ("Adding '" + c + "'"); datOpVector.add (charOperator); break; } }
} valueFound = false; charOperator = null; currentValue = 0; } else { if (isFraction) fractionMultiplier = fractionMultiplier / 10; } pos++; } while (pos < s.length()); if (valueFound) switch (previousOperation) { case '-': datOpVector.add(new Double (-currentValue)); if (doOutput) System.out.println ("Adding - " + currentValue + " = " + -currentValue); break; case '/': datOpVector.add(new Double (1 / currentValue)); if (doOutput) System.out.println ("Adding 1 / " + currentValue + " = " + 1/currentValue); break; default: datOpVector.add(new Double (currentValue)); if (doOutput) System.out.println ("Adding " + currentValue); break; } if ((datOpVector != null) && !datOpVector.isEmpty()) datOpVector = doOperation (datOpVector, '/'); if ((datOpVector != null) && !datOpVector.isEmpty()) datOpVector = doOperation (datOpVector, '*'); if ((datOpVector != null) && !datOpVector.isEmpty()) datOpVector = doOperation (datOpVector, '-'); if ((datOpVector != null) && !datOpVector.isEmpty()) datOpVector = doOperation (datOpVector, '+'); if ((datOpVector != null) && !datOpVector.isEmpty()) return (Double) datOpVector.firstElement(); return null; }
|
|