0

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?

user4581301
  • 33,082
  • 7
  • 33
  • 54
Encipher
  • 1,370
  • 1
  • 14
  • 31
  • 1
    Unrelated: Your includes suggest you don't know what bits/stdc++.h does. [Give this a read](https://stackoverflow.com/questions/25311011/how-does-include-bits-stdc-h-work-in-c). Then, if you haven't already noticed, [read this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) to learn why you should never include it directly. – user4581301 Mar 13 '20 at 01:53
  • Related: Run the program in whatever debugging utility came with your development environment. With the debugger you can control the execution of the program, advancing it in as tight a grain as necessary, and view the contents of variables in order to help you spot logical errors. Setp through the program and watch what it does as it does it. When the program does something you don't expect, figure out why. Usually the unexpected is a bug. – user4581301 Mar 13 '20 at 01:59
  • Recommendation: Be damn sure you have an item in the stack before you `double o1 = Stack.top();` I'm pretty sure you hit this before adding anything to the stack. – user4581301 Mar 13 '20 at 02:02
  • Recommendation: expression trees. – woz Mar 13 '20 at 02:16
  • @woz expression tree does not work at all. – Encipher Mar 13 '20 at 02:34
  • I agree that an ordinary implementation would not work, as your expression is not on the "regular" format. But it does not mean that you can't create an expression tree out of your expression and evaluate it. It also would come very in handy in case you want to implement operation precedence. – woz Mar 13 '20 at 02:39
  • 1
    Search the internet for "C++ shunting yard algorithm". – Thomas Matthews Mar 13 '20 at 03:46
  • Note: it looks like Normal Polish Notation (NPN). Is it exactly the case ? Difficult to see without more examples. Note that in your examples the parenthesis seem useless and can be ignored, except if you want to test the validity of the input expression – Damien Mar 13 '20 at 10:33
  • @ThomasMatthews Sounds good. – Encipher Mar 13 '20 at 17:45

0 Answers0