# Dissertation for Desk Calculator

Topics: Reverse Polish notation, Infix notation, Calculator Pages: 8 (1838 words) Published: May 14, 2013
Desk Calculator

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI WORK INTEGRATED LEARNING PROGRAMMES DIVISION
BITS-WIPRO Collaborative Programme: MS in Information Technology, YEAR-2012.

Abstract:
The project is to solve an infix expression using a desk calculator. Input is given as an infix expression and output is obtained in a text file. Method followed to solve the expression is, the input is fetched from the input text document and converted to postfix notation using expression tree data structure, the resulted RPN expression is evaluated and the result is shown in output text file. Infix is converted to postfix because, it is easy for the compiler to execute the expression in postfix format. Commonly used data structures for evaluating expressions are stacks and expression trees, both of the above data structures are analyzed and concluded that expression trees are the best suited data structure for solving infix expression.

1.Introduction:

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. The calculator you will be working with uses Reverse Polish Notation (RPN), which avoids the ambiguities that can be caused by operator precedence and parentheses, but requires the calculator to remember many operands. RPN is just another name for postfix notation

This means that our program would have to have two separate functions, 1) To convert the infix expression to postfix or RPN.
2) To evaluate the postfix expression.
We first introducing, to the conversion techniques first and later move on to writing the expression evaluator functions.
Let us first introduce ourselves to the infix and postfix notations.

1.1 Infix and Postfix

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.

1.1.1 BODMAS Rule
Now let's take a expression : A + B / C
For this we need to use the BODMAS rule. This rule states that each operator has its own priority and the operators with the highest priority are evaluated first. The operator priority in descending order is BODMAS which is:

B O D M A S
Bracket Open -> Division -> Multiplication -> Addition -> Subtraction

Hence, in the preceding example, B / C is evaluated first and the result is added to A.

1.2 Infix to Postfix Conversion:
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.

1. A + B / C (First Convert B / C -> B C /)
2. A + B C / (Next Operation is A + (BC/) -> A BC/ +
3. A B C / + (Resultant Postfix Form)

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 the added to A. Without the parenthesis, B * C would...