Sea130 DS Exp 2
Sea130 DS Exp 2
Aim:
To convert Infix expression to Postfix form
Name: 21/09/21
Tiwari
NAIDU Ayush Manoj
ANURADHA 21/09/21
SHRIKANT A
SEA130
Roll no: SEA 124
TITLE:
Write a program in C to convert Infix expression to Postfix form.
THEORY:
Infix expression:
In infix notation the operator is placed in between the
operands.
Syntax:
<operand1> <operator> <operand2>
Example:
a+b
Algorithm:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SIZE 100
char stack[SIZE];
int top = -1;
void push(char item)
{
if(top >= SIZE-1)
{
printf("\nStack Overflow.");
}
else
{
top = top+1;
//top=-1 top=-1+1=0
stack[top] = item;
//stack[0]='('
}
}
char pop()
{
char item ;
if(top <0)
{
printf("stack under flow: invalid infix expression");
getchar();
/* underflow may occur for invalid expression */
/* where ( and ) are not matched */
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
}
/* define function that is used to determine whether any symbol is
operator or not
(that is symbol is operand)
* this fucntion returns 1 if symbol is opreator else return 0 */
i=0;
j=0;
item=infix_exp[i]; /* initialize before loop*/
//item=+
int main()
{
char infix[SIZE], postfix[SIZE];
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
OUTPUT :