data structures and algorithms · characters in expression are valid validate() infix expression...

20
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

Upload: others

Post on 01-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

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

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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

Page 5: 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

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

Page 6: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 6

bool checkParentheses(char infix[]) {int open=0, close=0, i=0;while (infix[i] != '\0') {

if(infix[i] == '(')open++;

if(infix[i] == ')') {close++;if (close>open)

return false;}i++;

}if(open==close) return true;else return false;

} // End of function

1. Traverse the entire infix expression

i. Count the number of ‘(‘

i. Count the number of ‘)’a. If count of ‘)’ is more than ‘(‘,

then return false

2. If the total count of ‘(‘ and ‘)’ matches, return true3. Else, return false

Page 7: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 7

bool isValid(char currentChar) {return ((currentChar>=65 && currentChar<=90) ||

(currentChar>=97 && currentChar<=122) ||(currentChar>=40 && currentChar<=43) || currentChar==45 ||currentChar==47 || currentChar==94);

} // end of fucntion

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

Page 8: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 8

bool validate(char infix[]) {int i=0;while (infix[i] != '\0') {

if (! isValid(infix[i]) )return false;

elsei++;

}if (checkParentheses(infix) == false)

return false;

return true;} // end of fucntion

1. Traverse the string i. If character is not an operand or

operator, then return falseii. Else, point to the next character

2. Examine whether ‘checkParentheses()’i. If ‘false’ was returned, return ‘false

3. Return ‘true’. This means that the expression passed all tests and it is valid.

Page 9: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 9

bool checkPriority(char currentOperator, char topOperator) {if ( priorityNumber(currentOperator) < priorityNumber(topOperator))

return true;else

return false;} // End of function

int priorityNumber(char Operator) {if (Operator=='^') return 0;if (Operator=='*' || Operator=='/') return 1;if (Operator=='+' || Operator=='-') return 2;if (Operator=='(') return 3;

} // End of function

Return value 0, 1, 2, or 3 depending on the operator

Page 10: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 10

bool isOperand(char currentChar) {return ( (currentChar>=65 && currentChar<=90) ||

(currentChar>=97 && currentChar<=122) );} // End of function

Check whether the character is either of the followingA to Z ora to z orReturn true if the above condition is satisfied, else return false

Page 11: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 11

void infix2Postfix(char infix[], char answer[]) {int i=0, j=0; char currentChar; char postfix[50]="";stack<char> operatorStack; //Operator stackwhile(infix[i]!='\0') { //Traverse the infix expression

currentChar = infix[i];

i++; //Point to next character of infix expression} //End of while

strcpy(answer,postfix); } //End of function

Code for 2.1

Code for 2.2

Code for 3

Note: 2.1, 2.2, and 3 are point number given in the ‘Algorithm’

Page 12: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 12

// Code for 2.1if ( isOperand(currentChar) ) {

postfix[j]=currentChar;j++;

}

Note: 2.1, is point number given in the ‘Algorithm’

2.1 If character is operand, then append to the string ‘postfix’

Page 13: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 13

// Code for 2.2else { //It is operator

}

Code for 2.2.1

Code for 2.2.2

Code for 2.2.3

if (currentChar=='(') {operatorStack.push(currentChar);i++;continue;

}

Note: 2.2, 2.2.1, 2.2.2, and 2.2.3 are point number given in the ‘Algorithm’

2.2 else, do the following (It is an operator): 2.2.1 If operator is ‘(‘, push it to the operator stack and continue with point 2.

Page 14: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 14

// Code for 2.2else { //It is operator

}

Code for 2.2.1

Code for 2.2.2

Code for 2.2.3

if(operatorStack.empty() && currentChar!=')') {operatorStack.push(currentChar);

}

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

Page 15: 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

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

Page 16: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 16

// Code for 2.2else { //It is operator

}

Code for 2.2.1

Code for 2.2.2

Code for 2.2.3

else {if (checkPriority(currentChar,operatorStack.top()))

operatorStack.push(currentChar);else {

while (!operatorStack.empty()) {if (operatorStack.top()=='(') {

operatorStack.pop(); break;

}postfix[j]=operatorStack.top();j++;operatorStack.pop();

} //End of whileif(currentChar!=')')

operatorStack.push(currentChar);} //End of else

} //End of else

2.2.3.a

2.2.3.b

Page 17: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 17

// Code for 3//Transfer remaining items in stackwhile(!operatorStack.empty()) {

postfix[j]=operatorStack.top();operatorStack.pop();j++;

}

Note: 3, is point number given in the ‘Algorithm’

3. Append the remaining operators from the stack to the string ‘postfix’ by using ‘top’ followed by ‘pop’, till stack becomes empty.

Page 18: 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

IIT Bombay

Program

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 18

int main() {char infix[50], postfix[50];cout << "Enter an infix expression without space.

Use parentheses to indicate priority. Only +, -, *, /, ^ symbols are allowed\n";

cin >> infix;

if (validate(infix) == false) {cout << "The expression contains a number, or an unwanted symbol,

or the round braces do not match\n";return -1;

}cout << "Infix expression : " << infix << endl;infix2Postfix(infix,postfix);cout << "Postfix expression : " << postfix << endl;

return 0; }

Page 19: 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

IIT Bombay

References

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 19

• https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

• http://csis.pace.edu/~wolf/CS122/infix-postfix.htm

Page 20: 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

IIT Bombay

Thank you

Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay 20