2

Similar question has been asked here, However, still remains unanswered (at least no code provided, though the suggestion is good)

My code unlike the hyperlinked one, only evaluates the entered expression I am not being able to fabricate a logic to accept negative numbers. Note that in my code, the expression to be entered has to be in proper postfix format, and the multi-digit numbers have to be surrounded with parenthesis.

Here is my code:

float expression::eval()
{
    std::stack<float> s;
    float op1, op2;
    for (int i = 0; i < expr.length(); i++)
    {
        if (expr[i] == '(')
        {
            float sum = 0;
            while (expr[++i] != ')')
            {
                sum = sum * 10.0 + (float(expr[i]) - float('0'));
            }
            s.push(sum);
            continue;
        }
        else if (!isOperator(expr[i]))
        {
            s.push(float(expr[i]) - float('0'));
        }
        else
        {
            op2 = s.top();
            s.pop();
            op1 = s.top();
            s.pop();
            switch (expr[i])
            {
            case '+':
                s.push(op1 + op2);
                break;
            case '-':
                s.push(op1 - op2);
                break;
            case'*':
                s.push(op1*op2);
                break;
            case '/':
                s.push(op1 / op2);
                break;

            default:
                std::cerr << "Wrong operator" << std::endl;
                return 0;
            }
        }
    }
    return s.top();
}

Note that expression is an object in my code. my class:

class expression
{
    //constructors and destructors
public:
    expression();
    ~expression();
    //member properties
private:
    std::string expr;
    //member methods
public:
    // accepts the expression from the user
    void input();
    //prints the accepted expression
    void output();
    //evaluates the accepted expression
    float eval();
    //
    friend bool isOperator(char);
};

eg:- 12+12*2 needs to be written as

(12)(12)+2*

This is what I tried. But, it doesn't work. Can anyone explain what's wrong in this logic?:

    if (expr[i] == '(')
        {
            float sum = 0;
            bool flag = false;
            while (expr[++i] != ')')
            {
                if (expr[i] == '-')
                    flag = true;
                sum = sum * 10.0 + (float(expr[i]) - float('0'));
            }
            if (flag)
                sum = -sum;
            s.push(sum);
            continue;
        }
  • 2
    You only support binary operators so you would need to write your expression as (0) (12)-. If you want to support - on its own, add support for unary minus. ie if there is only one operand on the stack then either evaluate it as unary, or just provide the zero and can the regular binary operator. – Josh Homann Apr 19 '19 at 16:49
  • That's a nice idea, but doesn't seem to be very efficient. Btw, what's wrong with the flag logic? –  Apr 19 '19 at 16:51
  • 1
    @kesarling *This is what I tried. But, it doesn't work* -- [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – PaulMcKenzie Apr 19 '19 at 16:57
  • got it!!!. just needed to increment I if flag is true. That's what I was missing. Debugging helped me a lot :) Thanks a lot. Finally, I guess, now the hyperlink also has an answer –  Apr 19 '19 at 16:58
  • It seems, my reputation is not enough, Can anyone with >50 reputation please provide a hyperlink to here in comments at [link](https://stackoverflow.com/questions/40553697/how-can-i-accept-negative-values-in-postfix-and-infix-notation) please? –  Apr 19 '19 at 17:02
  • 1
    You shouldn't fall through to `sum =` after `if (flag)`. The value of `'-' - '0'` isn't meaningful. – o11c Apr 19 '19 at 17:12
  • that's what. I got that after the debugging suggestion I received. Now its working fine –  Apr 19 '19 at 17:13

0 Answers0