Need to code this in c++. Reverse polish notation using stacks. out put (1+2)*3
ID: 3836407 • Letter: N
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 + * and do the arithmatic #include <iostream> #include <string.h> #include <stack> #include <sstream> #include <stdlib.h> using namespace std ; int main() {stack<float>stack; 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<<"The result of the expression is:"<<stack.top(); }
Explanation / Answer
Anonymous answered thisEdit Delete
527 answers
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.