Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple calculator in java parser

Tags:

java

I am currently making a simple calculator parser in Java, to deal only with + and - operators, and whole numbers. I have read about postfix and infix evaluation, and I am wondering if brackets can be used with either of these methods?

like image 777
sutoL Avatar asked Sep 30 '26 09:09

sutoL


1 Answers

The beauty of the postfix/prefix notation is that you do NOT need the brackets. Brackets are used in infix notation because there are ambiguities, e.g:

a + b - c

This can mean either of the following:

(a + b) - c
a + (b - c)

However, in postfix notation, they are clearly different:

a b + c -
a b c - +

There is no need for parantheses/brackets to enforce evaluation order in postfix notation.

See also

  • Wikipedia/Reverse Polish Notation

Conversion from infix

You can take an expression in infix notation with parantheses and convert it to postfix notation, obeying operator precedence. One such algorithm is Edsger Dijkstra's stack-based "shunting-yard algorithm".

See also

  • Wikipedia/Shunting-yard algorithm
like image 153
polygenelubricants Avatar answered Oct 01 '26 23:10

polygenelubricants



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!