2

How do you tell Java the order of operands?

Well, I am pretty new to coding and a friend of mine in a higher course got asked to make a calculator and I got interested in how would it would work.

I searched for it and the solutions I found are pretty much just a superbasic calculator, is code I could do my self (and I appreciate that cause there is a lot I do not understand yet); but I am looking for something more complex.

The proposals were something like:

Double result = 0;

System.out.println ("First opperand:");
Num1 = scan.next double();

System.out.println ("Choose operation (+,-,*,/):");
Ope = scan.next();

System.out.println ("Second opperand:");
Num2 = scan.next double();

Switch (ope) {
 Case '+':
  Result = num1 + num2;
  break;

 Case '-':
  Result = num1 - num2;
  break;

//and so on for each different operand that you want to add
}

System.out.println (result);

The thing is, I guess the idea of a calculator is the user to input a operation as a string and then the calculator interprets the operands and opperator. I guess you can do this somewhat easy with the string class, telling it to search the full string for successions of numbers as new opperands variables to create and anything else as a operator and check if they are valid (don't really know how to properly program that yet since I am still very new to this as I told before).

But the actual question is, how could I tell the program the order of operations of something like "20/((10+5)*2)"?

I think I could kinda deal with the basic opperators but how do I deal with parentheses?

I don't know if this is way more advanced and I won't even understand the answer or it is actually kinda simple and I just need some perspective but I am like really intriged by this and that's why I am asking

1 Answers1

4

What you are looking for is a proper parser, that would convert your string into what is known as a parse-tree, so that you can then perform the operations described by the parse tree.

So, a parser for numeric expressions would convert the string "20/((10+5)*2)" into the following parse-tree:

               [/]
              /   \
             20   [*]
                 /   \
               [+]    2
              /   \
             10    5

Then you would perform your calculations on the parse-tree, recursively, as follows:

  • to compute the value of the current node:
    • if the node is a leaf node, (a numeral,) then:
      • use the value of the numeral as the value of the node.
    • if the node is a non-leaf node, (an operand with child nodes,) then:
      • compute the value of the left child node (recursion)
      • compute the value of the right child node (recursion)
      • perform the calculation indicated by the operand on the two computed values.
      • use the result of the calculation as the value of the node.

Parsers are an entire sub-topic of Information Science, (otherwise known as "Computer Science",) so they cannot really be explained in a stackoverflow answer, but at least you have a name now, that you can use to search for more information if you want to.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142