codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#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; }
Private
[
?
]
Run code
Submit