0% found this document useful (0 votes)
110 views

Converting and Evaluating Infix

The document discusses different notations for representing mathematical expressions: infix, postfix, and prefix. It explains that while infix notation is easiest for humans to read and write, postfix and prefix notations are better suited for computer evaluation. Therefore, a program must first convert an infix expression entered by a user into postfix or prefix notation before calculating the result. The document provides examples of converting simple infix expressions like A + B to postfix and prefix. It also demonstrates handling expressions with parentheses by first converting the inner-parenthesized expression before the full conversion.

Uploaded by

Evan Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Converting and Evaluating Infix

The document discusses different notations for representing mathematical expressions: infix, postfix, and prefix. It explains that while infix notation is easiest for humans to read and write, postfix and prefix notations are better suited for computer evaluation. Therefore, a program must first convert an infix expression entered by a user into postfix or prefix notation before calculating the result. The document provides examples of converting simple infix expressions like A + B to postfix and prefix. It also demonstrates handling expressions with parentheses by first converting the inner-parenthesized expression before the full conversion.

Uploaded by

Evan Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Converting And Evaluating Infix, Postfix And Prefix Expressions

Consider a situation where you are writing a programmable calculator. If the user types in 10 + 10, how would you compute a result of the expression? You might have a solution for this simple expression. But what if he types in

3 * 2 / (5 + 45) % 10?

What would you do then?


The expression that you see above is known as Infix Notation. It is a convention which humans are familiar with and is easier to read. But for a computer, calculating the result from an expression in this form is difficult. Hence the need arises to find another suitable system for representing arithmetic expressions which can be easily processed by computing systems. The Prefix and Postfix Notations make the evaluation procedure really simple. But since we can't expect the user to type an expression in either prefix or postfix, we must convert the infix expression (specified by the user) into prefix or postfix before processing it. This means that your program would have to have two separate functions, one to convert the infix expression to post or prefix and the second to compute the result from the converted expression. Consider a simple expression: A + B This notation is called Infix Notation. A + B in Postfix notation is A B + as you might have noticed, in this form the operator is placed after the operands (Hence the name 'post'). Postfix is also known as Reverse Polish Notation. Likewise the equivalent expression in prefix would be +A B, Where the operator precedes the operands.
We use infix notation for representing mathematical operations or for manually performing calculations, since it is easier to read and comprehend. But for computers to perform quick calculations, they must work on prefix or postfix expressions.

Now let's take a slightly complicated expression : A + B / C How do we convert this infix expression into postfix? To convert A + B / C into postfix, we convert one operation at a time. The operators with maximum priority are converted first followed by those which are placed lower in the order. Hence, A + B / C can be converted into postfix in just X steps. :: A + B / C (First Convert B / C -> B C /) 1: A + B C / (Next Operation is A + (BC/) -> A BC/ + 2: A B C / + (Resultant Postfix Form) The same procedure is to be applied to convert infix into prefix except that during conversion of a single operation, the operator is placed before the two operands. Let's take the same example and convert it to Prefix Notation. :: A + B / C (First Convert B / C -> / B C) 1: A + / B C (Next Operation is A + (/BC) -> + A /BC + 2: + A / B C

Sometimes, an expression contains parenthesis like this: A + B * ( C + D ) Parenthesis are used to force a given priority to the expression that it encloses. In this case, C+D is calculated first, then multiplied to B and then added to A. Without the parenthesis, B * C would have been evaluated first. To convert an expression with paranthesis, we first convert all expressions that are enclosed within the simple brackets like this: [INFIX TO POSTFIX] :: A + B * ( C + D ) 1: A + B * C D + 2: A + B C D + * 3: A B C D + * +

You might also like