Question
Need to code this in c++.
Reverse polish notation using stacks.
out put
(1+2)*3 = 1 2+3*
or 2* (1+2) 2 1 3 + *
Explanation / Answer
#include #include #include #include #include using namespace std ; int main() {stackstack; int i; float num,result,first,second; char op,ch; string str,str1; getline(cin,str); istringstream is(str); for(;is>>str1;){ if(str1.compare("+")==0){ first=stack.top(); stack.pop(); second=stack.top(); stack.pop(); stack.push(first+second); }else if(str1.compare("-")==0){ first=stack.top(); stack.pop(); second=stack.top(); stack.pop(); stack.push(first-second); }else if(str1.compare("*")==0){ first=stack.top(); stack.pop(); second=stack.top(); stack.pop(); stack.push(first*second); }else if(str1.compare("/")==0){ first=stack.top(); stack.pop(); second=stack.top(); stack.pop(); stack.push(first/second); }else{ stack.push(strtof(str1.c_str(),NULL)); } } cout