Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculator

Status
Not open for further replies.

fmaurer

Technical User
May 15, 2007
11
PL
I have to write a calculator which will be able to calculate expressions similar to ((2+4)*4)/((3+2)-(5*5)) so I need a parser to parse such thing and compute the result. My question is as follows. Is there any built-in class which is able to parse such expression or do I have to use regular expressions to validate the expression and then write a function which perform analysis and returns the result. Or perhaps there is some other way.
 
A google search shows some examples of source code. There are also classes in the java.text package that can help you a lot.

Cheers,
Dian
 
You can use a Stack class to create your calculator stack.
The algorithm to evaluate arithmetic expressions is relatively simple. A complete example is given in Chapter 3 of Robert Sedgewick'S Algorithms in C (or the latest book, in Java), or Chapter 9 of Herbert Schildt's The art of C++ (or his latest book The art of Java).
You should be able to find other examples by Googling, for example, "expression parsing examples" or anything like that.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top