-1

I would like to evaluate (not convert) infix expression in C++. If you posses algorithm or even implementation of such algorithm (in any language... I will try to rewrite it to C++) share please.

Evaluation means give the value of expression. (2+2)*3 is 12

Update

I am sorry, I forgot that I am talking about stack solution, because I know the tree solution and It is not suitable this time.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 3
    It's not clear what you want to do. Could you explain more what you mean by "evaluate infox expression"? – sth Aug 25 '12 at 13:00

2 Answers2

3

How do you got the expression? If you got it like (3 + 4) - (1 * 2) + 1 =

                  +  
            /          \
           -            1
     /          \   
    +            *          
 /     \      /    \
3      4    1       2

http://cboard.cprogramming.com/cplusplus-programming/32682-inserting-infix-into-binary-tree.html

do a tree transversal of the tree like Left Root Right so it will be something like this: 3 + 4 the result - the result of 1 * 2 the result + 1.

If you got the expression like 34+12*-1+ you can simulate assembly like do a stack and if you get to an operator pop the last 2 elements in the stack and apply the operator: put 3 in stack, put 4 in stack, get op. + so pop the last 2 elements and use the operator. Now you got only 7 in stack. Now read until get an operator so in the stack you will have 7 1 2 after op. * in stack you got 7 2 after op. - you get only 5 in stack add 1 in stack: Stack 5 1, use the last operator + and get the final result 6.

Ok, here is the code:

#include <STACK>

int GetResult( char * rpn ) 
{
    std::stack<int> myStack;

    int nr1, nr2; int length = strlen(rpn);

    for (int i = 0; i < length; i++)
    {
        if (isdigit(rpn[i]))
        {
            myStack.push(rpn[i] - '0');
        }
        else
        {
            switch(rpn[i])
            {
                case '+':
                    nr1 = myStack.top();
                    myStack.pop();
                    nr2 = myStack.top();
                    myStack.pop();
                    myStack.push(nr2 + nr1);
                    break;
                    
                case '-':
                    nr1 = myStack.top();
                    myStack.pop();
                    nr2 = myStack.top();
                    myStack.pop();
                    myStack.push(nr2 - nr1);
                    break;
                    
                case '*':
                    nr1 = myStack.top();
                    myStack.pop();
                    nr2 = myStack.top();
                    myStack.pop();
                    myStack.push(nr2 * nr1);
                    break;
                    
                case '/':
                    nr1 = myStack.top();
                    myStack.pop();
                    nr2 = myStack.top();
                    myStack.pop();
                    myStack.push(nr2 / nr1);
                    break;
                default:
                    break;
            }
        }
    }

    return myStack.top();
}

int main(int argc, char* argv[])
{
    char *rpn = "34+12*-1+";

    int rez = GetResult(rpn);

    printf("%i", rez);

    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thanatos
  • 1,176
  • 8
  • 18
  • I am sorry I forgat that I am talking with stack solution. I know the tree solution. – Yoda Aug 25 '12 at 13:11
  • @RobertKilar I don't understand. Rephrase please! – Thanatos Aug 25 '12 at 13:13
  • I ment that I know the Tree traversal solution(I have already done it), but I need the stack one(kind of which we use for RPN- reverse polish noation). – Yoda Aug 25 '12 at 13:15
  • So do you want me to post the stack solution? Because I already explained it? – Thanatos Aug 25 '12 at 13:17
  • If it possible please post it : ). Sorry, I am reading all the advices and links i got from all of you. It would help a lot. Now i found java solution but it is with some issues, so I am trying to understand it and correct it. – Yoda Aug 25 '12 at 13:24
1

By far the easiest way is to convert the infix expression to postfix notation using Dijkstra's Shunting-yard algorithm, and evaulate the result, which is, like, trivial. There are code samples of this all over the internet (take a look at Rosetta code or LiteratePrograms).


Alternatively, if you'd like to learn, and you'd like to enter the magical world of parsing and a bit of compiler theory, write a recursive descent parser. It's great fun.


Oh and, if you'd like something more robust, you could take a look at Pratt parsing, which is awesome. Here's a great article about it.

SáT
  • 3,633
  • 2
  • 33
  • 51