IIT Bombay Data Structures and Algorithms Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Infix to Postfix (Program) 1 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
20
Embed
Data Structures and Algorithms · characters in expression are valid validate() Infix expression (character array) Returns true or false to the main() function To check whether the
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
IIT Bombay
Data Structures and AlgorithmsProf. Ajit A. Diwan
Prof. Ganesh RamakrishnanProf. Deepak B. Phatak
Department of Computer Science and EngineeringIIT Bombay
Session: Infix to Postfix (Program)
1Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay
IIT Bombay
Algorithm
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 2
Input: Infix ExpressionOutput: Postfix Expression
1. Validate infix expression1.1 If not valid, display appropriate error message, else proceed for conversion
2. Traverse the infix expression (character by character)2.1 If character is operand, then append to the string ‘postfix’2.2 else, do the following:
2.2.1 If operator is ‘(‘, push it to the operator stack and continue with point 2.
IIT Bombay
Algorithm
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 3
2.2.2 If stack is empty and operator is not ‘)’, then push the operator on the stack
2.2.3 If stack is not empty, then compare the current operator with the one which is present on top of the stack.(a) If it has higher precedence: push the operator on the stack(b) If it has lower precedence then,
i. Append the top element of the stack to string ‘postfix’ii. Pop the top operator from the stack iii. Continue i and ii till ‘(‘ is encountered or the stack
becomes empty.iv. Finally, push the current operator, but not ‘)’ on the
stack3. Append the remaining operators from the stack to the string ‘postfix’ by using
‘top’ followed by ‘pop’, till stack becomes empty.
IIT Bombay
Program: Functions
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 4
Name Parameter Returns Purpose
checkParen-theses()
Infix expression (character array)
Called from validate(). Returns ‘true’ or ‘false’
To check whether openand close braces match
isValid() Current character Called from validate(). Returns ‘true’ or ‘false’
To check whether characters in expression are valid
validate() Infix expression(character array)
Returns ‘true’ or ‘false’ to the main() function
To check whether the expression is valid
isOperand() Current character Called from ‘infix2Postfix()’. Returns ‘true’ or ‘false’
To check whether the current character is an operand i.e. a to z or A to Z
IIT Bombay
Program: Functions
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 5
Name Parameter Returns Purpose
priorityNumber() characteri.e. Operator
Called from‘checkPriority()’. Returns an integer.
To determine the priority of the operator
checkPriority() Current operator, and top operator on stack
Called from ‘infix2Postfix()’. Returns ‘true’ or ‘false’
To determine the priority of the operator
infix2Postfix() Infix expression,Postfix expression. (Both are character arrays)
Returns the postfix string to the main() function
Converts a valid infix expression to postfix expression
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6
Check whether the character is either of the followingA to Z ora to z or(, ), *, + or- or/ or^Return true if the above condition is satisfied, else return false
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8
Note: 2.2, 2.2.1, 2.2.2, and 2.2.3 are point number given in the ‘Algorithm’
2.2.2 If stack is empty and operator is not ‘)’, then push the operator on the stack
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 15
2.2.3 If stack is not empty, then compare the current operator with the one which is present on top of the stack.(a) If it has higher precedence: push the operator on the stack(b) If it has less precedence then,
i. Append the top element of the stack to string ‘postfix’
ii. Pop the top operator from the stack iii. Continue i and ii till ‘(‘ is encountered or the stack
becomes empty.iv. Finally, push the current operator, but not ‘)’ on
the stack
IIT Bombay
Program
Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 16