-5

I'm working on a calculator, it should receive the input as a String and then perform the calculation, outputting the result.

For example, the input could be

  ((23+17) mod 7 × 4 AND 13

and the output would be 4, as expected.

How can I parse the input, to extract all the operands and perform the calculation ?

woliveirajr
  • 9,433
  • 1
  • 39
  • 49
wasp256
  • 5,943
  • 12
  • 72
  • 119
  • Do you know that there exists the modulo operator in Java? Look for it.. – Maroun Aug 28 '13 at 11:36
  • I would suggest using math to solve such calculations. Also, it's really important here, if you are trying to do this, to formalize what `AND` represents. – SubSevn Aug 28 '13 at 11:37
  • 2
    Are you comfortable with the Java Language? I assume not. Probably less effort could have been spent in finding the answer on google. – dharam Aug 28 '13 at 11:37
  • 2
    I think he's asking how to parse that input string and produce a result. – SubSevn Aug 28 '13 at 11:37
  • 1
    yes, the problem is that I receive the input as a String and I don't really know how to parse it – wasp256 Aug 28 '13 at 11:37
  • @wasp256 Check this question http://stackoverflow.com/questions/13798557/java-expression-parser-calculator-shunting-yard-algorithm – jontro Aug 28 '13 at 11:38
  • Maybe overkill, but contains a chapter about parsing expressions: http://www.pearsonhighered.com/educator/product/Compilers-Principles-Techniques-and-Tools/9780321486813.page – proskor Aug 28 '13 at 11:39
  • OP - What have you already tried? – Dawood ibn Kareem Aug 28 '13 at 11:41
  • @wallace nothing yet, I was asking for the best way to parse the string... – wasp256 Aug 28 '13 at 11:46
  • 1
    Go and get a book on compiler theory and read the chapter on how parsers work. A really good answer to this question would run for several pages; and it's not really suitable for Stack Overflow. – Dawood ibn Kareem Aug 28 '13 at 11:51

3 Answers3

2

The other answers are just "how to set a variable to this result" but if you're actually looking to parse input, you should refer to this:

Equation (expression) parser with precedence?

There are a number of ways to go about solving this kind of problem, and a number of algorithms for doing so. Some of them are stack based, some perform a descent of the "tree", and I can even think of a (somewhat) convoluted way to OOP-ize it. I would start with the link above.

Community
  • 1
  • 1
SubSevn
  • 1,008
  • 2
  • 10
  • 27
1

You can use regular expressions to parse the string.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html

First you have to look for the most important arguments like [() - parentheses], then less [*/] and [+-]. You have to divide the whole string into parts.

Examples:

Simple calculator (bottom of the page)

Another calculator

Both in Java.

Pawel
  • 1,457
  • 1
  • 11
  • 22
  • can you elaborate? The language is not regular (note for example nested parenthesis) so I don't see how you could do that? – Henry Aug 28 '13 at 11:45
0

see this page for your reference.

http://www.tutorialspoint.com/java/java_basic_operators.htm

all operators in java are explained very clearly.

saravanakumar
  • 1,747
  • 4
  • 20
  • 38