#include <iostream>
#include <stack>
#include <string>
#include <cctype>
using namespace std;
int main()
{
int i;
string expression;
char token;
int value, value1, value2;
stack<char> myStack;
cout << "Please enter the RPN expression to be evaluated: " << endl <<
"(Note: Each digit or operator should be separated by a space, and end with :)/>"
<< ": " << endl;
getline(cin, expression);
i = 0;
token = expression[i];
while((i < expression.size()) && (token != '='))
{
if (isdigit(token))
{
value = token - '0';
myStack.push(value);
}
else
{
value2 = myStack.top();
myStack.pop();
value1 = myStack.top();
myStack.pop();
switch(token)
{
case '+': value = value1 + value2;
break;
case '-': value = value1 - value2;
break;
case '*': value = value1*value2;
break;
case '/': value = value1/value2;
break;
}
myStack.push(value);
}
i++;
token = expression[i];
}
value = myStack.top();
myStack.pop();
cout << expression << " " << value << endl;
cout << endl;
return 0;
}
Sorry I posted it by mistake before I could ask my question. When I build it comes up fine however when I attempt to debug it the error "deque iterator not deferencable" keeps appearing. I've tried looking this up but can't find anything conclusive as to why this issue is occurring. Help would be much appreciated.
Also I'm not sure why it appears that way in the code I posted, but everywhere it says
exp<b></b>ression is meant to be the variable string expression.

New Topic/Question
Reply



MultiQuote






|