a function evaluateCountdown
that takes a string containing a mathematical expression written in Reverse Polish Notation, and returns the result of evaluating the expression, as a double.
In reverse polish notation (RPN), operators are written after rather than between what they act upon. For instance:
3.0 4.0
is the same as the usual (infix) notation (3.0 4.0). Or the below expression in RPN
format:
3.0 4.0 2.0 *
is the same as (3.0 4.0) * 2
I am getting a logic error, the input "3.0 4.0 - 2.0 *"
is supposed to give the output -2.0
however the output I am receiving when calling the main function is 0
, the input string must be in the format where the numbers are doubles, the function works when the input string is, for example, "3 4 - 2 *"
(returns a correct answer of -2
), but not when in the form "3.0 4.0 - 2.0 *"
the function evaluateCountdown
iterates through the input string and when it finds a number it pushes that number into the stack as a double, so for example when tokens[i]
is 3
then 3 tokens[i 1] tokens[i 2]
, in this case '3' '.' '0'
is pushed into the stack as a double. However, this isn't happening and I'm not sure why.
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <iostream>
#include <iterator>
using std::vector;
using std::ostream;
using std::string;
using std::cout;
using std::stack;
using std::iterator;
using std::endl;
using std::stod;
double evaluateCountdown(string & tokens) {
// this method does not work if the string is empty or a string of just spaces, string must be a expression in RPN
stack<double> stack;
for (int i = 0; i < tokens.size(); i ) {
if (tokens[i] == ' ')
{
double val1 = stack.top();
stack.pop();
double val2 = stack.top();
stack.pop();
stack.push((val1 val2));
}
else if (tokens[i] == '-')
{
double val1 = stack.top();
stack.pop();
double val2 = stack.top();
stack.pop();
stack.push((val2 - val1));
}
else if (tokens[i] == '*')
{
double val1 = stack.top();
stack.pop();
double val2 = stack.top();
stack.pop();
stack.push(val1 * val2);
}
else if (tokens[i] == '/')
{
double val1 = stack.top();
stack.pop();
double val2 = stack.top();
stack.pop();
stack.push((val2 / val1));
}
else if (tokens[i] == ' ')
{
// if char is a space do nothing and move onto the next char.
}
else
{
string str = "";
char c1, c2, c3;
c1 = tokens[i];
c2 = tokens[i 1];
c3 = tokens[i 2];
// concat chars togther to get required string eg '3' '.' '0'
auto ans = string(1, c1) c2 c3;
str = ans;
double val = stod(str);
stack.push(val);
}
}
double val = stack.top();
stack.pop();
return val;
}
int main() {
string s = "3.0 4.0 - 2.0 *";
double ans = evaluateCountdown(s);
cout << ans << endl;
}
CodePudding user response:
When the code finds a number, it should increment i
by two, unless doing that the code does not work.
Therefore, the else
condition could be written as follows(The rest of the else
statement will remain intact):
c1 = tokens[i];
c2 = tokens[ i];
c3 = tokens[ i];
If you are a new programmer, I would recommend that when you encounter a problem like that, you use a debugger to debug your program (even the most professional programmers need to debug their code).