-3

I am currently trying to create my own stack without importing java.util.*. I am only allowed to use arrays and loops to convert my infix expression to postfix for it to work in a calculator. I can already split the infix up into terms but couldn't find any articles explaining how to approach this problem. Thank you in advance!

benim bonkers
  • 61
  • 1
  • 3
  • 1
    could you describe your problem further please? Maybe add some code or pseudo-code to explain your ideas? – GameDroids Oct 08 '19 at 12:12
  • What have you tried? The general idea would be to create an array and keep track of the top of the stack after any of your stack operations, possibly "growing" the array of the stack if needed. – xtratic Oct 08 '19 at 12:14
  • actually it sounds like two problems at a time. First you need to create your "Stack" with a simple array. Like for example you could create a class `StackArray` with an array to back your elements. Then you need the methods that a Stack usually has (push, pop, peek...). Once you have your stack implementation you analyse your Infix-postfix-problem. My advice: do it on paper. Once you've split your expression (reading it as "infix") you need to add the "terms" to your Stack in the right order. If you have `arr=['a', '+', 'b']` you do: `stack.put(arr[0]); stack.put(arr[2]);stack.put(arr[1]);` – GameDroids Oct 08 '19 at 12:19
  • The problem is that I understand the principle of the shunting yard algorithm, but I can't find out how to implement it into my code. Right now I have made a new class containing public boolean isEmpty(), public String pop() and public String push() which I will use to replace the stack functions – benim bonkers Oct 08 '19 at 12:29
  • not sure, maybe [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) or some of the algorithms linked there (coincidence I tried it out last week, but sure used `java.util`) – user85421 Oct 08 '19 at 12:35
  • Possible duplicate? https://stackoverflow.com/questions/35914209/infix-to-postfix-conversion-in-java – Jim Mischel Oct 08 '19 at 17:31

1 Answers1

0

here is a nice description of the algorithm (wikipedia/en).

Basically you start by splitting your expression. For example

a + b * c

If it where a String, then you do:

String expression = "a + b * c";
String[] terms = expression.split(" ");  // this will split the string at every whitespace

Now your Array should look like this: ["a", "+", "b", "*", "c"]

You now need an array where you can put your "terms" in for the postfix order:

String[] postFixTerms = new String[expression.length]; // it should have the same size as the original array

So to fill it, you read your terms array from left to right, meaning you start at index 0 and end at n. but you want to filter out the operands and put them in your Stack!

int termsIndex=0;  // you will have two indices because you have two arrays!
for(int i=0; i<terms.length; i++){
   String element = terms[i];
   if(elementIsOperand(element)){
      operandsStack.put(element);
   }else{
      postFixTerms[termsIndex] = element; 
      termsIndex++;
   }
}

At this point you should have an array with your "numbers":

postFixTerms=["a", "b", "c", null, null]; // remember you only added the numbers in it not the operands, so there is still some space left at the end of the array

And you have your operandsStack:

operandsStack=["+","*"];

Now you only need to put them together, but remember that a stack works "FILA" -first in last out, so:

postFixTerms[termsIndex] = operandsStack.pull();  // adds "*" first
termsIndex++;
postFixTerms[termsIndex] = operandsStack.pull();  // then adds the "+"

If all goes right, your final postfix expression should be

["a","b","c","*","+"];
GameDroids
  • 5,584
  • 6
  • 40
  • 59