0 evaluări0% au considerat acest document util (0 voturi)
162 vizualizări44 pagini
The document discusses converting infix notation to postfix notation. It explains that a stack can be used to perform the conversion while maintaining operator precedence and associativity. The pseudocode provided pops operators from the stack and appends them to the output when encountering parentheses or an operator with equal or higher precedence. An example conversion is shown step-by-step to generate the postfix equivalent of the expression "ab*bc–g/ad+bcf/-**+".
The document discusses converting infix notation to postfix notation. It explains that a stack can be used to perform the conversion while maintaining operator precedence and associativity. The pseudocode provided pops operators from the stack and appends them to the output when encountering parentheses or an operator with equal or higher precedence. An example conversion is shown step-by-step to generate the postfix equivalent of the expression "ab*bc–g/ad+bcf/-**+".
Drepturi de autor:
Attribution Non-Commercial (BY-NC)
Formate disponibile
Descărcați ca TXT, PDF, TXT sau citiți online pe Scribd
The document discusses converting infix notation to postfix notation. It explains that a stack can be used to perform the conversion while maintaining operator precedence and associativity. The pseudocode provided pops operators from the stack and appends them to the output when encountering parentheses or an operator with equal or higher precedence. An example conversion is shown step-by-step to generate the postfix equivalent of the expression "ab*bc–g/ad+bcf/-**+".
Drepturi de autor:
Attribution Non-Commercial (BY-NC)
Formate disponibile
Descărcați ca TXT, PDF, TXT sau citiți online pe Scribd
Infix to Postfix Conversion Need to handle operator precedence Need to handle parentheses Need to handle lef t-to-right association Input: Infix with variables, integers, operators, parenth eses (no exponentiation for now) Output: Postfix form of same expression Infix Precedence Parenthesis () Exponentiation ^ Multiplication *, Division / Addition +, Subtrac tion - We will use a stack to convert infix expression to its equivalent postfix expres sion In computer science, a stack is an abstract data type and data structure ba sed on the principle of Last In First Out (LIFO). Psuedocode Get the input of an infix expression into an array tos=0 (tos=top of the stack) for (i=0;i<=arraySize;i++) { if array[i]is an operand: Append array[i] to the postfix expression break; if ar ray[i]is an operator: if (tos==0) push array[i] into the stack tos++; if (tos>0) { for(j=tos;j>=0;j--){ compare the precedence of array[i] and stack[j]; if arra y[i] is an operator of greater or equal precedence pop stack[j] and append it to the postfix expression; tos--; else break; } then push array[i] into the stack tos++; break; } if array[i] is '(' : push array[i] into the stack tos++; if array[i] is ')' : fo r(k=tos;k>=0;k--) { if ( stack[k]!=’(’ ) pop stack[k] and append it to the postf ix expression; tos --; if ( stack==’(’ ) tos --; break; } if array[i]is NULL: fo r(l=tos;l>=0;l--) { pop stack[l] and append it to the postfix expression; } } Infix input a*b + (b-c)/g * ( (a+d) * (b-c/f) ) Lets start converting it to its equivalent postfix form using a stack. Infix to postfix conversion infix expression a*b + (b-c)/g * ( (a+d) * (b-c/f) ) postfix expression Infix to postfix conversion infix expression *b + (b-c)/g * ( (a+d) * (b-c/f) ) postfix expression a Infix to postfix conversion infix expression b + (b-c)/g * ( (a+d) * (b-c/f) ) postfix expression a * Infix to postfix conversion infix expression + (b-c)/g * ( (a+d) * (b-c/f) ) postfix expression ab * Infix to postfix conversion infix expression (b-c)/g * ( (a+d) * (b-c/f) ) postfix expression ab* + Infix to postfix conversion infix expression b-c)/g * ( (a+d) * (b-c/f) ) postfix expression ab* ( + Infix to postfix conversion infix expression -c)/g * ( (a+d) * (b-c/f) ) postfix expression ab*b ( + Infix to postfix conversion infix expression c)/g * ( (a+d) * (b-c/f) ) postfix expression ( + ab*b Infix to postfix conversion infix expression )/g * ( (a+d) * (b-c/f) ) postfix expression ( + ab*bc Infix to postfix conversion infix expression /g * ( (a+d) * (b-c/f) ) postfix expression ab*bc- + Infix to postfix conversion infix expression g * ( (a+d) * (b-c/f) ) postfix expression ab*bc/ + Infix to postfix conversion infix expression * ( (a+d) * (b-c/f) ) postfix expression ab*bc-g / + Infix to postfix conversion infix expression ( (a+d) * (b-c/f) ) postfix expression ab*bc–g/ * + Infix to postfix conversion infix expression (a+d) * (b-c/f) ) postfix expression ( * + ab*bc–g/ Infix to postfix conversion infix expression a+d) * (b-c/f) ) ( ( * + postfix expression ab*bc–g/ Infix to postfix conversion infix expression +d) * (b-c/f) ) ( ( * + postfix expression ab*bc–g/a Infix to postfix conversion infix expression d) * (b-c/f) ) + ( ( * + postfix expression ab*bc–g/a Infix to postfix conversion infix expression ) * (b-c/f) ) + ( ( * + postfix expression ab*bc–g/ad Infix to postfix conversion infix expression * (b-c/f) ) postfix expression ( * + ab*bc–g/ad+ Infix to postfix conversion infix expression (b-c/f) ) * ( * + postfix expression ab*bc–g/ad+ Infix to postfix conversion infix expression b-c/f) ) ( * ( * + postfix expression ab*bc–g/ad+ Infix to postfix conversion infix expression -c/f) ) ( * ( * + postfix expression ab*bc–g/ad+b Infix to postfix conversion infix expression ( * ( * + c/f) ) postfix expression ab*bc–g/ad+b Infix to postfix conversion infix expression ( * ( * + /f) ) postfix expression ab*bc–g/ad+bc Infix to postfix conversion / ( * ( * + infix expression f) ) postfix expression ab*bc–g/ad+bc Infix to postfix conversion / ( * ( * + infix expression )) postfix expression ab*bc–g/ad+bcf Infix to postfix conversion infix expression ) * ( * + postfix expression ab*bc–g/ad+bcf/- Infix to postfix conversion infix expression postfix expression ab*bc–g/ad+bcf/-* * + Infix to postfix conversion infix expression postfix expression ab*bc–g/ad+bcf/-** + Infix to postfix conversion infix expression postfix expression ab*bc–g/ad+bcf/-**+ Equivalent postfix expression ab*bc–g/ad+bcf/-**+ We have observed When we get any integer or character it goes to the output. The order of the ope rands in the postfix expression is the same as the order in the infix expression , and the operands that appear to the left of an operator in the infix expressio n also appear to its left in the postfix expression. When we get an operator, it is pushed onto the stack maintaining the following rule if the stack is empty, the operator is pushed onto the stack. However, if the stack is not empty, at fi rst we pop the operators of greater or equal precedence from the stack and appen d them to postfix expression. Then the new operator is pushed onto the stack. Th us, this step orders the operators by precedence and in accordance with left-to- right association. We have observed After getting a left parenthesis ‘(‘ , 1 & 2 is maintained until we get right pa renthesis ‘)’. When ")" is encountered, we pop operators off the stack and appen d them to the end of postfix expression until we encounter the matching "(". Wit hin a pair of parentheses, precedence and left-to-right association determine th e order of the operators. When we reach the end of the string, we append the rem aining contents of the stack to postfix expression. And finally we get the postfix expression. Thanks to…….. Course teacher: Abul Hasan Samee Made by…… Sabrina Hossain Tonny Roll : 200614033 CSE-7 MIST