The Artima Developer Community
Sponsored Link

Java Answers Forum
Need to evaluate a String to make graphing calc

1 reply on 1 page. Most recent reply: Oct 18, 2004 12:59 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Matthew Putnam

Posts: 3
Nickname: mattputnam
Registered: Oct, 2004

Need to evaluate a String to make graphing calc Posted: Oct 17, 2004 7:11 PM
Reply to this message Reply
Advertisement
I'm trying to make a graphing calculator, which will take any entered equation and graph it, even if it's not a function. For this, I'll need to know how to evaluate a mathematical expression stored in a string. For example, if a String variable is "3+4", I need a method that can evaluate that and return 7 as an int (or whatever, as long as it's numerical). If my ti-83+ can do it, I think Java can, too!


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Need to evaluate a String to make graphing calc Posted: Oct 18, 2004 12:59 AM
Reply to this message Reply
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;
}

Flat View: This topic has 1 reply on 1 page
Topic: Binary Tree Previous Topic   Next Topic Topic: Max Heap Allocation Question

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use