Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithms"— Presentation transcript:

1 Data Structures and Algorithms
Prof. Ajit A. Diwan Prof. Ganesh Ramakrishnan Prof. Deepak B. Phatak Department of Computer Science and Engineering IIT Bombay Session: Postfix Evaluation (Program) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

2 Algorithm Input: Postfix Expression
Output: Numerical value of the evaluated postfix expression Get values of all operands present in the postfix expression Traverse the postfix expression If it is an operand, push the value of that operand on the stack Else, it is an operator, so do the following: Assign the value from the top of stack to a variable ‘operand2’ Pop the element from the stack Assign the value from the top of stack to a variable ‘operand1’ Evaluate the result of operand1 and operand2 and push the result back on the stack Return the top value of the stack (answer) Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

3 Program: Functions Name Parameter Returns Purpose getValues()
Postfix expression, values (array) Called from evaluatePostfix(). Does not return any value To store the values of operands in the array ‘value’ which is passed from evaluatePostfix() isOperator() Current character Called from evaluatePostfix(). Returns ‘true’ or ‘false’ To check whether character is an operator or not calculateValues() operand1, operand2, operator Called from evaluatePostfix(). Returns numerical value Evaluate operand1 and operand2 evaluatePostfix() Postfix expression Called from ‘main()’. Returns numerical value To evaluate postfix expression Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

4 Program Examine the ‘currentChar’ If it is an operator, return true
Else return false bool isOperator(char currentChar) { if (currentChar=='+' || currentChar=='-' || currentChar=='*' || currentChar=='/' || currentChar=='^') return true; else return false; } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

5 Program Traverse the postfix expression
If the character is an operand: Accept the value of operand from the user and store it in the array ‘value’ at the index ‘operand’ Using Associative arrays for storing values For a few operands, we need a small array to store their values. In our examples, we are using operands represented by single character (a-z and A –Z). Their ASCII codes are within 0 to 255 We will use array ‘value’ of size 256 to store operand values, in locations corresponding to their ASCII codes, which can be used directly as an index to the array ‘value’ E.g. Postfix expression: ac+ Here, ‘a’ and ‘c’ are operands Assume that user has entered: number 5 for operand ‘a’ and number 7 for operand ‘c’. value[97] = 5 //ASCII value of ‘a’ is 97 value[99] = 7 //ASCII value of ‘c’ is 99 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

6 Program Traverse the postfix expression
If the character is an operand: Accept the value of operand from the user and store it in the array ‘value’ at the index ‘operand’ void getValues(char postfix[], float value[]) { int i=0; char currentChar; while(postfix[i]!='\0') { currentChar=postfix[i]; if (!isOperator(currentChar)) { //Operand cout << "Enter value of " << currentChar << ": "; cin >> value[currentChar]; } i++; } //End of function E.g. Postfix expression: ac+ Here, ‘a’ and ‘c’ are operands Assume that user has entered: number 5 for operand ‘a’ and number 7 for operand ‘c’. value[97] = 5 //ASCII value of ‘a’ is 97 value[99] = 7 //ASCII value of ‘c’ is 99 Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

7 Program Perform the operation on ‘operand1’ and ‘operand2’
Return the result to the function ‘evaluatePostfix’. float calculateValues(float operand1, float operand2, char Operator) { if (Operator == '+') return operand1 + operand2; if (Operator == '-') return operand1 - operand2; if (Operator == '*') return operand1 * operand2; if (Operator == '/') return operand1 / operand2; if (Operator == '^') return pow(operand1,operand2); } //End of function Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

8 Program float evaluatePostfix(char postfix[]) {
stack<float> Stack; //stack char currentChar; int i=0; float result, operand1, operand2, value[256]; getValues(postfix, value); while(postfix[i]!='\0') { currentChar = postfix[i]; i++; } //End of while (expression traversed) return Stack.top(); //Final answer } //End of function Code for evaluation Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

9 Program if (isOperator(currentChar)) { operand2 = Stack.top();
Stack.pop(); operand1 = Stack.top(); result = calculateValues (operand1, operand2, currentChar); Stack.push(result); } else { //It is an operand Stack.push(value[currentChar]); float evaluatePostfix(char postfix[]) { stack<float> Stack; //stack char currentChar; int i=0; float result, operand1, operand2, value[200]; getValues(postfix, value); while(postfix[i]!='\0') { currentChar = postfix[i]; i++; } //End of while (expression traversed) return Stack.top(); //Final answer } //End of function Code for evaluation Ajit A. Diwan, Ganesh Ramakrishnan, and Deepak B. Phatak, IIT Bombay

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


Download ppt "Data Structures and Algorithms"

Similar presentations


Ads by Google