Works perfectly!!! Got it within 2 tries...but had to put 'p' and 'm' for '+' and '-' respectively.....Any idea why '+' and ' - ' characters are not accepted by the program? TIA
If possible please compile the code and see if '+' and '-' do the job in your case..
Code:#include<string> #include<sstream> #include<iostream> #include<deque> using namespace std; int main() { string input; getline(cin,input); deque<double> stack; istringstream in; in.str(input); double opn; char opt; for(;;) { if(in>>opn) { stack.push_back(opn); continue; } else { in.clear(); if (in>>opt) { switch (opt) { case 'p': { opn = stack.back(); stack.pop_back(); opn += stack.back(); stack.pop_back(); stack.push_back(opn); break; } case 'm': { opn = stack.back(); stack.pop_back(); opn=stack.back()-opn; stack.pop_back(); stack.push_back(opn); break; } case '*': { opn = stack.back(); stack.pop_back(); opn *= stack.back(); stack.pop_back(); stack.push_back(opn); break; } case '/': { opn = stack.back(); stack.pop_back(); opn=stack.back() / opn; stack.pop_back(); stack.push_back(opn); break; } default: cout<<"sorry._ :( _."; } } else break; } } opn = stack.back(); stack.pop_back(); if(stack.empty()) cout<<opn; else cout<<"More Operands than Operators"; cin.get(); return 0; }
Also searching for a better way to implement the operator selection than the cumbersome switch-case which would grow in complexity with increase in operators .



LinkBack URL
About LinkBacks




).