• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to execute the mathematical string equation?  RSS feed

 
vicky saxena
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:

Int num1 , num2;
Char =�*�;
num1=3;
num2=5;
String equation = ��+num1+char+num2;

now the string will contain �3*5�

my question is how can I get result 15
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create some math parser. Hard job
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a simple math parser in COBOL. I required spaces between terms and left out any "order of precedence" logic to keep it simple so it processes from left to right and uses recursion when it finds parens. The syntax boils down to expecting:

var = term [ operator term ... ]
x = 1
y = 1 + 2
z = ( x + y ) * 3

To do serious parsing you have to recognize signs vs operators and numeric vs string tokens. To support order of precedence you can use some common stack algorithms.

This is frequently a CS assignment. (I was a music major, never did one of these for class.) Is that what you're doing?
[ August 30, 2005: Message edited by: Stan James ]
 
Mahesh Rana
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To start with, take a look at this ECMA Parser in Java
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

a library that is very simple to use is jfunction. The code to evaluate your example would be

or with according static imports:

Kai
 
vicky saxena
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kai Witte,
I have tried to use jfunction library but when I compile it I am getting
this error.
D:\java_test_questions>javac MathEquation.java
MathEquation.java:11: cannot access org.dyndns.kwitte.jfunction.Terms
bad class file: c:\jfunction.jar.(org/dyndns/kwitte/jfunction/Terms.class)
class file has wrong version 49.0, should be 48.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
System.out.println(org.dyndns.kwitte.jfunction.Terms.evaluate(equation));
^
1 error

I have set the classpath to c:\jfunction.jar
 
vicky saxena
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kai Witte,
I have downloaded Jdk 5 , and now my program is compiled successfully.
Thanks a lot
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFunction is pretty cool. It's almost sad that so many of the cool things have already been written.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!