I want to find out the answer of the expression (- (+3 3) 2) in c++. The answer of this equation will be 4. Because of 3+3 =6 and 6-2=4.
I try to implement this code with postfix expression but I cannot evaluate the brackets ( and ) so far.
My code:
#include <iostream>
#include <string>
#include <bits/stdc++.h> 
using namespace std;
bool isOperand(char c)
{
// If the character is a digit then it must 
// be an operand 
    return isdigit(c);
}
double evaluatePrefix(string exprsn)
{
    stack<double> Stack;
    for (int j = exprsn.size() - 1; j >= 0; j--)
    {
        // Push operand to Stack 
        // To convert exprsn[j] to digit subtract 
        // '0' from exprsn[j]. 
        if (isOperand(exprsn[j]))
            Stack.push(exprsn[j] - '0');
        else
        {
            // Operator encountered 
            // Pop two elements from Stack 
            double o1 = Stack.top();
            Stack.pop();
            double o2 = Stack.top();
            Stack.pop();
            // Use switch case to operate on o1 
            // and o2 and perform o1 O o2. 
            switch (exprsn[j])
            {
                case '+':
                    Stack.push(o1 + o2);
                    break;
                case '-':
                    Stack.push(o1 - o2);
                    break;
                case '*':
                    Stack.push(o1 * o2);
                    break;
                case '/':
                    Stack.push(o1 / o2);
                    break;
            }
        }
    }
    return Stack.top();
}
// Driver code 
int main()
{
    string exprsn = "(- (+3 3) 2)";
    cout << evaluatePrefix(exprsn) << endl;
    return 0;
}
I did not find any answer for this code. Can any one help me to rectify the error?
 
    