Saturday, 22 February 2014

Using Stacks to evaluate prefix notation expressions (Polish notation)

Prefix notation (for those that do not know), is a way of writing a mathematical expression without the use of parenthesis or brackets. Also known as "Polish notation" (which was created by Jan Ɓukasiewicz to simplify sentential logic), it provides an easy way for computers to evaluate order of operations expressions without the use of brackets.

To start, a prefix notation example is “+34″, which would evaluate to 7 because the expression is 3+4, just in polish notation. Accordingly, there are a lot more examples of polish notation, and for the sample code posted, the algorithm will evaluate the prefix notation from a string array. In pseudo-code, the algorithm uses a stack to push and pop values in the expression and then evaluate according to the operator in the expression:

Code in C#:

class Program
    {
        static void Main(string[] args)
        {
            Stack<int> polishNotationStack = new Stack<int>();//a stack of type int
            //the expression will be a string array
            string[] expression = { "/", "*", "5", "+", "4", "3", "2" };//first, a prefix expression
            for (int i = 0; i < expression.Length; i++)//we write the prefix expression
            {
                Console.Write(expression[i]);
            }
            int result;//a result to push onto the stack after an operation was done
           
            Console.WriteLine();//newline space
            Array.Reverse(expression);
           //we reverse the expression to read in the characters from right to left
           
            int n;
            foreach (string c in expression)//for each string characcter in the array
            {
                if (int.TryParse(c, out n))//if the character can be converted to a number (operand)
                {
                    polishNotationStack.Push(n);//push the number onto the stack
                }
                if (c == "+")//handling of operators
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x + y;//evaluate the values popped from the stack
                    polishNotationStack.Push(result);//push current result onto the stack
                }
                if (c == "-")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x - y;
                    polishNotationStack.Push(result);
                }
                if (c == "*")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x * y;
                    polishNotationStack.Push(result);
                }
                if (c == "/")
                {
                    int x = polishNotationStack.Pop();
                    int y = polishNotationStack.Pop();
                    result = x / y;
                    polishNotationStack.Push(result);
                }
               
            }
            /*write the final result of the expression,
             * which is at the top of the stack, so we use Peek()*/
            Console.WriteLine("result of expression: {0}", polishNotationStack.Peek());

            //Function();
            Console.ReadLine();
        }

No comments:

Post a Comment