0

I have this code:

#include <iostream>
#include <string>
#include “stack.h”
int main (int argc, char *argv[]) {
   char *a = argv[1]; 
   int N = strlen(a);
   stack<int> polish(N); int el;
   for (int i = 0; i < N; i++){
      if (a[i] == '+'){
         el = polish.readStack(); polish.outofStack();
         polish.inStack( el + polish.readStack()); polish.outofStack()
      }
      if (a[i] == '*'){
         el = polish.readStack(); polish.outofStack();
         polish.inStack(el * polish.readStack()); polish.outofStack()
      }
      if ((a[i] >= '0') && (a[i] <= '9')){
         el = polish.readStack(); polish.outofStack()
         polish.inStack(10 * el + (a[i++]-'0'));
      }
   }
   cout << polish.outofStack() << endl;
}

How does it work? And what does it mean this line?

polish.inStack(10 * el + (a[i++]-'0'));
usr1234567
  • 21,601
  • 16
  • 108
  • 128
m r
  • 11
  • 2
  • Such a strange implementation of `stack`. It is a well-known practice to call methods `push` and `pop` but not `readStack` and `inStack`. Also, `pop` should automatically remove the last element without calling `outOfStack`. – Yeldar Kurmangaliyev Jul 03 '15 at 08:37
  • Correct me if I'm wrong, but this shouldn't be working: The result of the operations us pushed into the stack (without removing the second operand) and then discarded?! – Daniel Jour Jul 03 '15 at 08:45
  • `polish.inStack(10 * el + (a[i++]-'0'));` if you assume `stack::readStack()` returns an `int` this would mean you pass 10 * the result of that call + the next character that was given as input to `stack::inStack();` – Floris Velleman Jul 03 '15 at 08:45
  • @YeldarKurmangaliyev Looks like a standard interface with non-standard names - `inStack` is `push`, `readStack` is `top`, `outOfStack` is `pop`. – molbdnilo Jul 03 '15 at 08:46

1 Answers1

0

It looks like it is an algorithm which reads and calculates Reverse (postfix) Polish notation.

For example,

1 2 3 + 4 - -

means

Add 1; add 2; add 3; sum up the last two; add 4; substract the last two; substract the last two.

i.e.

1 - ((2 + 3) - 4) = 0

This code line:

polish.inStack(10 * el + (a[i++]-'0'));

is supposed to combine a number by appending digits.
(a[i++]-'0') is converting from char digit like '3' to integer 3.

Initially, we have a zero in our stack. For example, if you have "123", it will read them char by char this way:

  1. read 1. Get the last number from stack (0), make 0 * 10 + 1 = 1. Push it back to stack

  2. read 2. Get the last number from stack (1), make 1 * 10 + 2 = 12. Push it back

  3. read 3. Get the last number from stack (12), make 12 * 10 + 3 = 123. Push it back.

  4. Great! 123 number has been read.

However, this code sample is a bunch of bad practices.

  1. It is a well-known practice to name stack functions Pop and Push, but not readStack() and inStack().
  2. Stack structure requires element to be removed after reading. readStack() does not provide it.
  3. Several ifs instead of switch statement.
  4. Never increase your counter within the loop.
  5. This code actually does not work because it does not split values.
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101