Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
31 views

Write A Program For Converting A Given Infix Expression To Postfix Form Using Stack

The document describes an algorithm for converting an infix expression to postfix notation using a stack. It provides steps for scanning the infix expression from left to right, handling operands and operators of different precedence, and outputting the postfix expression. It also includes a C program implementing this algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Write A Program For Converting A Given Infix Expression To Postfix Form Using Stack

The document describes an algorithm for converting an infix expression to postfix notation using a stack. It provides steps for scanning the infix expression from left to right, handling operands and operators of different precedence, and outputting the postfix expression. It also includes a C program implementing this algorithm.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program for converting a given infix expression to postfix

form using Stack

Algorithm:

1. Start scanning the given expression from left to right.


2. If the scanned character is an operand, just print it.
3. Else
 If the precedence of the operand is higher than the precedence of the
operator the stack(or stack is empty or has'(‘), then push the operator
in the stack.
 Else, Pop all the operators, that have greater or equal precedence than
the scanned operator. Once you pop them push this scanned operator.
(If we see a parenthesis while popping then stop and push the scanned
operator in the stack)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Now, we should repeat steps 2 – 6 until the whole infix i.e. whole characters
are scanned.
7. Print output
8. Do the pop and output (print) until the stack is not empty

Program:
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

int main()
{
char exp[100];
char *e, x;
printf("Enter the Infix expression : ");
scanf("%s",exp);
printf("\n");
printf("The Postfix Expression is As Follows:\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}return 0;
}

Output:

1:
Enter the Infix expression : a+b*c

The Postfix Expression is As Follows:


abc*+

2:

Enter the Infix expression : (a+b)*c+(d-e)

The Postfix Expression is As Follows:


ab+c*de-+

You might also like