[ create a new paste ] login | about

Link: http://codepad.org/Qh7tiZzK    [ raw code | output | fork ]

C++, pasted on Sep 23:
#include <iostream>
    #include <stack>
    #include <string>
    #include <sstream>

    using namespace std;

    stack<char> aStack;
    stringstream result;
    stack<char> operand1;
    stack<char> operand2;


    stringstream &postfixExp(string ch){
      for(unsigned int i =0; i< ch.length(); i++)
      {
        if(ch[i]== '1' || ch[i]== '2' || ch[i]== '3' || ch[i]== '4' || ch[i]== '5' || ch[i]== '6' || ch[i]== '7' || ch[i]== '8' || ch[i]== '9' || ch[i]== '0' )
        {
          aStack.push(ch[i]);
        }

        else if( ch[i]== '+')
        {
          operand2.push(aStack.top());
          aStack.pop();

          operand1.push(aStack.top());
          aStack.pop();

      result << ( operand1.top() + operand1.top());
    }

  }

  return result;
}

int main()
{
    string postfix = " 2+3";

    stringstream* answer = &postfixExp(postfix);
    cout << "Result = " << answer->str() << endl;;


  return 0;
}


Output:
1
2
3
4
5
6
7
8
9
/usr/local/lib/gcc/i686-pc-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_stack.h:165:
    error: attempt to access an element in an empty container.

Objects involved in the operation:
sequence "this" @ 0x0x805c700 {
  type = St5stackIcN15__gnu_debug_def5dequeIcSaIcEEEE;
}

Disallowed system call: SYS_kill


Create a new paste based on this one


Comments: