• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create a java program that can print the value of any expresion ?  RSS feed

 
Jui Mahajan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to create a java program that can print the value of any expresion ?
e.g 2=3, or any complex example like ((5+3)/2)-(8-7).

I have been able to input an expression from user. But after that I am lost. I also dont know how exactly to use the Stringtokenizer class.

Please let me know. It is urgent.

Thanks
Jui
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A StringTokenizer is probably not going to be a good enough mechanism for your problem. I reckon you'll need to look at regular expressions.

So first you'll have to define the limits of "any expression" - is this just the basic arithmetic operations (+-*/)? Will it include comparison (== != etc.)? How about other mathematical operations (x^, log, cos etc.)? Once you know that you can write expression to match each supported expression.

Then you'll have to write some sort of validation - so you don't try to solve invalid expressions ( e.g. (1+(3)))+1 or expressions which contain unknown operators)

Then you have the problem of parenthesis and operator precedence. You'll need to solve the expressions nested inside parenthesis before you can solve the outer expression, if you follow? I reckon you will probably have to do this by splitting the expression into a character array and keeping track of where you are in relation to the parenthesis.

This is a good candidate for recursion.
 
Andris Jekabsons
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to do a similar exercise for my class a while ago (just for basic arithmetic operations (+-*/), though). Used stack to deal with precedence - looked at one character at a time, and either pushed on or poped from the stack, or evaluated depending on what the character was.
[ April 14, 2005: Message edited by: Andris Jekabsons ]
 
s choukse
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have written a similar programs to evaluate dynamic rules for my application. The program breaks the input string into Operands and Operators. I have implemented the logic for each operator, considering both Binary and Unary Operator. As the input strinf is an Infix notation, implement logic to resolve it.

I think this is a school assignment level of a problem. So I would advice to put a little more effort solving the problem before posting it on the forum.

s choukse
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did a something similar for spreadsheet app. It handled ()-+*/^ and cell references. I used one stack for operators and another stack for values. Doing a search on polish notation should help. The basic approach is:

1) Break the string into tokens.
2) grab a token.
3) check if it is an operator or value
4) put it on the proper stack.

As you go when you get an operator you check if you should evaluate what has come before. If you need to evaluate then you do and place the result on the value stack.

So something like this: 3+4*2 vs (3+4)*2
3+4*2:
1) get 3 place in value stack
2) get + place in operator stack
3) get 4 place in value stack
4) get * place in operator stack
5) get 2 place in value stack
end of equation
6) pop top operator and top two values
7) evaluate
8) place on value stack
9) repeate 6-8

(3+4)*2:
1) get ( place on operator stack
2) get 3 place in value stack
3) get + place in operator stack
4) get 4 place in value stack
5) get ) triggers precidence rule
6) pop operator and two values
7) evaluate
8) place value on value stack
9) pop operator, matching parens, continue
10) get * place on operator stack
11) get 2 place on value stack
12) end of line evaluate.

I've left out alot of how to do the logic, but that should get you headed in the right direction.
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I developed such a term parser. It is called jfunction, and it is available for download here.

I use an interpreter pattern to compile the expression to a tree of nodes (a state machine). You can see a fully described example in "Design Patterns" (interpreter pattern). There it is used for regular expressions. An example where it is applied for regular expressions is java.util.regex.

Because of the license (LGPL) it is likely that you can just use my library for your project. Have a look at the documentation.

If you want to dig into this in detail I recommend the reading Programming Languages at the Florida State University (online).

Conan
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may generate a temporary java-file too, and compile it at runtime, with the library tools.jar.
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
You may generate a temporary java-file too, and compile it at runtime, with the library tools.jar.


Oooh, clever. Of course that's a BIG security risk.

Ryan
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jui Mahajan:
how to create a java program that can print the value of any expresion ?
e.g 2=3, or any complex example like ((5+3)/2)-(8-7).

I have been able to input an expression from user. But after that I am lost. I also dont know how exactly to use the Stringtokenizer class.

Please let me know. It is urgent.

Thanks
Jui


When I had an assignment like this in my data structures class, the teacher gave us pseudocode telling us exactly how to implement this. Did your instructor (or the textbook) give something similar? If so, what problems have you encountered trying to implement it.

I see that many people have tried to help you along the way. It seems that many of the replies are just guesses trying to describe what you need to do. However, from your original post, you seem to have written some of the code. It would save a lot of time if you posted what you have so far. You should also describe what you are trying to do next and the problems you have encountered up to this point. This would let us get straight to the point and help you with exactly what you need, rather than making us guess how to best help you.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!