0% found this document useful (0 votes)
13 views33 pages

Module2 02 0.1

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views33 pages

Module2 02 0.1

Uploaded by

ABHISHEK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Infix to postfix conversion

1.Read the characters one by one from left to right from the i/p
infix notation.
2.If the character is an operand append it on to output postfix
notation.
3.If the character is an operator then
(i) if(priority(top of stack) >= priority of operator read) then pop
out the top character and append it on to postfix notation.
(ii) if(priority(top of stack) < priority of operator read) then push
the operator on to stack.
4.If the character is an open bracket then push it on to stack
5.If the character is a closed bracket then repeatedly perform pop
until a corresponding closed bracket is found and then delete
the close bracket.
6.Finally, if we read the end of input, we pop the stack until it is
empty, writing symbols onto the output.
void reverse(char *str)
{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}

/* Driver program to test above function */


int main()
{
char a[] = "Geeks for Geeks";
reverse(a);
getch();
return 0;
}
Infix to postfix conversion

infixVect
(a+b-c)*d–(e+f)

postfixVect
Infix to postfix conversion
stackVect

infixVect
a+b-c)*d–(e+f)

postfixVect

(
Infix to postfix conversion
stackVect

infixVect
+b-c)*d–(e+f)

postfixVect
a

(
Infix to postfix conversion
stackVect

infixVect
b-c)*d–(e+f)

postfixVect
a

+
(
Infix to postfix conversion
stackVect

infixVect
-c)*d–(e+f)

postfixVect
ab

+
(
Infix to postfix conversion
stackVect

infixVect
-c)*d–(e+f)

postfixVect
ab+

(
Infix to postfix conversion
stackVect

infixVect
c)*d–(e+f)

postfixVect
ab+

-
(
Infix to postfix conversion
stackVect

infixVect
)*d–(e+f)

postfixVect
ab+c

-
(
Infix to postfix conversion
stackVect

infixVect
*d–(e+f)

postfixVect
ab+c-
Infix to postfix conversion
stackVect

infixVect
d–(e+f)

postfixVect
ab+c-

*
Infix to postfix conversion
stackVect

infixVect
–(e+f)

postfixVect
ab+c-d

*
Infix to postfix conversion
stackVect

infixVect
–(e+f)

postfixVect
ab+c–d*
Infix to postfix conversion
stackVect

infixVect
(e+f)

postfixVect
ab+c–d*

-
Infix to postfix conversion
stackVect

infixVect
e+f)

postfixVect
ab+c–d*

(
-
Infix to postfix conversion
stackVect

infixVect
+f)

postfixVect
ab+c–d*e

(
-
Infix to postfix conversion
stackVect

infixVect
f)

postfixVect

+ ab+c–d*e

(
-
Infix to postfix conversion
stackVect

infixVect
)

postfixVect

+ ab+c–d*ef

(
-
Infix to postfix conversion
stackVect

infixVect

postfixVect
ab+c–d*ef+

-
Infix to postfix conversion
stackVect

infixVect

postfixVect
ab+c–d*ef+-
Example-1

Convert the infix notation to its equivalent


postfix and Evaluate:

3 – 9 / 3 + 77 * 3 -1

Postfix  3 9 3 / - 77 3 * + 1 -

Evaluated Solution  230

23
Example-2

Convert the infix notation to its equivalent


postfix and Evaluate:

(8/2) – 6/3 + 7-2 * (4%3)+ 3 -1

Postfix  ????

Evaluated Solution  ????

24
Example-2

Convert the infix notation to its equivalent


postfix and Evaluate:

(8/2) – 6/3 + 7-2 * (4%3)+ 3 -1

Postfix  8 2 / 6 3 / - 7 + 2 4 3 % * - 3 + 1 -

Evaluated Solution  9
25
Infix to prefix conversion

Expression = (A+B^C)*D+E^5

Step 1. Reverse the infix expression.

5^E+D*)C^B+A(

Step 2. Make Every '(' as ')' and every ')' as '('

5^E+D*(C^B+A)

Step 3: Apply infix to postfix algorithm - 5E^DCB^A+*+

Step 4: Reverse the output of Step3 - +*+A^BCD^E5


Stack Appln–Balancing Symbols
The common mistake made frequently by
programmers is incorrect closing or missing symbols
such as parenthesis. This causes the compiler to
report lines of error. A program that checks whether
the symbols are balanced would be much useful. For
example the sequence [ ( ) ] is legal, but [ ( ] ) is
incorrect. The correct representation of arithmetic
expressions includes
1.There must be equal number of opening and closing
symbols
2.The left opening symbol must be balanced with the
right closing symbol
Stack Appln–Balancing Symbols
A stack can be used to find whether the expression
contains balanced symbols as follows:
1.Make an empty stack
2.Read characters until end of string
3.If the character is an opening symbol, push it onto
the stack
4.If the stack is empty then report an error
5.Otherwise pop the stack
6.If the symbol popped is not the corresponding
opening symbol, then report an error.
7.At end of file, if the stack is not an empty report an
error.
Stack–Balancing Symbols Example
Consider the correct expression: {p + (q – [m + n] ) }#
Push ( ‘{’ ) Push ( ‘(’ ) Push ( ‘[’ ) Pop ( ‘]’ ) Pop ( ‘)’ ) Pop ( ‘}’ )

[
( ( (
{ { { { {

At the end of the expression, the stack becomes empty, therefore balanced.
Consider the following incorrect expression: ( (x + y) #
Push ( ‘(’ ) Push ( ‘(’ ) Pop ( ‘)’ )

(
( ( (

At the end of the expression, the stack is not empty, therefore unbalanced.
Recursive Function or Recursion
A Function which calls the same function
itself again and again until some condition is
satisfied is said to be Recursive function
Eg:
main( ) f1( )
{ {
f1( ); f1( );
} }

31
//Factorial using recursion

#include<stdio.h> Example
#include<conio.h>

int fact(int);
int fact(int n)
void main()

{
{
int n,z; int x;
clrscr( );
if(n==1)
printf("Enter the value of n:");

scanf("%d",&n);
return(1);
z=fact(n); else
printf("\nFactorial of %d is %d",n,z);
{
getch();
x=n*fact(n-1
}
return(x);
}
}

32
int main() Example
{

char a[] = "Geeks for Geeks";


reverse(a);
getchar();

return 0;
} /* Function to print reverse of
the passed string */

void reverse(char *str)


{
if(*str)
{
reverse(str+1);
printf("%c", *str);
}
}

You might also like