0

So I have an assignment which works with a Stack and Infix to Postfix calculations. Now I have the infix to Postfix calculation but.. There is this part that it says that I have to make up a calculation of 4 random numbers and random operators.

So I have an array of numbers 1-9, and a char array consisting of +, - and *.

I can make my random calculation and put it in a string object, but I can not seem to store the calculation in an Integer variable in a proper way. With the proper priorities of operators.. I have tried some Switch and If-else statements but I can't find a way out anymore.

I have a Stack and an InToPost class which were given.

The calculation looks like this in code:

String calculation = Integer.toString(numbers[random.nextInt(9)])
            + Character.toString(operators[random.nextInt(2)])
            + Integer.toString(numbers[random.nextInt(9)])
            + Character.toString(operators[random.nextInt(2)])
            + Integer.toString(numbers[random.nextInt(9)])
            + Character.toString(operators[random.nextInt(2)])
            + Integer.toString(numbers[random.nextInt(9)]);

What I want now is to calculate it, but it's hard with the priority of * operator.

All help will be appreciated :)

Burbanana
  • 49
  • 1
  • 3
  • 12

3 Answers3

6

I would suggest parsing your data in to a tree where each node is an operation and the leafs are values, this will make it easier for you to implement order of operations. Make your highest priority operations the lowest and execute bottom up.

enter link description here

Todoy
  • 1,146
  • 1
  • 9
  • 17
  • This is basically the correct answer to the question. OP, doing PEMDAS-based calculations (which is what I assume you mean) based on String input is actually not a simple task. You will have to parse at every step, basically find the innermost highest priority operator and then move outwards each time after calculating the result from there each time. It helps if you can reorganize it like this user is suggesting but it is still not particularly simple. – Radiodef Oct 19 '13 at 20:52
  • Thanks for the good tip really, I will use this as well when needed. What I did was create a stack, and use the postfix value of my calculation to calculate the answer :D – Burbanana Oct 20 '13 at 14:41
0

It is impossible to convert a String to a operator. You need a methode to for what you want. In this link is a example that could help you.

Altough it is in C#, it is pretty much what you want.

Community
  • 1
  • 1
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
  • Yeah basically you want to say `if (op == '*') { /* multiply int to the left against int to the right */ } else if...`. – Radiodef Oct 19 '13 at 20:56
-1

Thanks for all the handy tips guys. But I found a pretty simple way to do it. I had an Infix to Postfix calculator right? So What I did is create the random calculation, put it into a string.

Use the Infix>postfix translator to make it postfix. Then Push the numbers of the postfix calculation on the stack, pop them at an operator and if the operator was a + it would add them together. No concern whatsoever about precedence of operators because in the postfix this is already done for you! :D

Hope this will help people out :) I can post the infix to postfix calculator if needed!

Burbanana
  • 49
  • 1
  • 3
  • 12