So I was asked to write a function for a Reverse Polish Notation. I\'ve gotten v
ID: 652404 • Letter: S
Question
So I was asked to write a function for a Reverse Polish Notation. I've gotten very close with my code but there is one problem.
#ifndef A6_HPP
#define A6_HPP
#include "token.hpp"
#include <iostream>
#include <stack>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
#include <cctype>
// implement your function RPN
template <typename Iter> double RPN(Iter first, Iter last) {
std::stack<double> s;
std::vector<token> tokens;
while(first!=last){
if(first->is_operand()){
s.push(first->as_operand());
}
else if(first->is_operator()){
double a= s.top();
s.pop();
double b= s.top();
s.pop();
if(first->as_operator()== '+'){
s.push(a+b);
}
if(first->as_operator()== '-'){
s.push(b-a);
}
if(first->as_operator()== '*'){
s.push(b*a);
}
if(first->as_operator()== '/'){
s.push(b/a);
}
}
first++;
}
return s.top();
} // RPN
#endif // A6_HPP
For an input of "2.71 3.14 +2.68 *", I get 8.4152. It's supposed to be 15.678. It basically skips the first operator. I've been stuck on this for hours and I can't seem to figure it out. Please help.
Explanation / Answer
#include #include void push(char); void pop(); char stack[300]; unsigned long top=0; int main() { unsigned long len, test,i=0,j=0; char expr[400]; scanf("%u ",&test); for(;jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.