2

For an assignment we are given an equation in reverse polish notation. For this example I will use: 2 3 8 * + $ The $ is for denoting the end of the expression. Using a stack, we output the answer.

I have been using:

getline(cin, input, '&');
input.c_str();

to read in the equation and then turn it into a c_string so I can look at the individual elements in the input.

After, I need to check for a few things. If the element is a digit I need to push it onto the stack. If it is white space I need to skip over it.

if (isdigit(input[i]))
{
    push();
    i++;
}
else if (isspace(input[i]))
{
    i++;
}

Now is what has me stuck. If I hit an operator (in this case *) I need to pop the top two elements off the stack and 'operate them' and push the result back to the stack. However, I don't know of anything that would allow me to recognize that they are operators. It is probably a silly question, but help would be greatly appreciated.

none
  • 11,793
  • 9
  • 51
  • 87
ViscousRandom
  • 179
  • 1
  • 9

4 Answers4

1

You could create a functions map, and match the operators

std::map<char, void (*)(char *)> operators;

operators['*'] = &mult;
operators['+'] = &add;
...

and in your conditionnal statement

if (isdigit(input[i]))
    {
       push();
       i++;
    }
else if (isspace(input[i]))
    {
       i++;
    }
else if (operators.find(input[i]) != operators.end())
    {
       operators[input[i]](input);
    }

This way, you will be able to easily add new operators to your calculator.

tomahh
  • 13,441
  • 3
  • 49
  • 70
0

there are only about 4 or 5 operators. check them with something like:

if (input[i] == '*') {...}
none
  • 11,793
  • 9
  • 51
  • 87
  • pfft. i'd use a switch and strtok(), but odds are runtime-libraries are rather limited for their assignment. Then again, i see isdigit(), isspace(), etc. – WhozCraig Oct 04 '12 at 03:46
0

Well, there's no "built-in" way. I would just write an IsOperator(char ch) function. And then do something like:

int IsOperator(char ch)
{
    // either add more operators or use a table, etc. here
    if (ch == '+' || ch == '-' || ch == '/' || ch == '*')
      return 1;

    return 0;
}

If you have multi-char operators, like '==', it gets a little more complicated because you have to peek ahead, but the idea is the same.

Mark Stevens
  • 2,366
  • 14
  • 9
0

If you are writing reverse polish notation calculator then on stack you have only numbers, or digits like in your example. BTW don't you accept multi-digits numbers?

Consider two cases, in both you need only std::stack<int> numbers;:

  • You have digit character c - just put number on stack:

code:

numbers,push(c - '0');
  • You have operator, let say '+', you replaces top two numbers by their sum:

code:

 if (numbers.size() < 2) {
     throw std::runtime_error("Too little numbers for +");
 }
 int a = numbers.top(); 
 numbers.pop();
 numbers.top() += a;
  • You have '$', check if there is only one number on stack, it is the result:

code:

  if (numbers.size() != 1) {
     throw std::runtime_error("There should be only one!");
   }
   int result = numbers.top();
   numbers.pop(); 
PiotrNycz
  • 23,099
  • 7
  • 66
  • 112