0% found this document useful (0 votes)
18 views

Code 4

This document contains a C program that takes an infix expression as input from the user and converts it to postfix expression using a stack. It uses a stack to store operators and operands. It pops operators with higher priority and prints them before pushing the current operator onto the stack. It also pops all remaining operators from the stack and prints them after the entire infix expression has been traversed.

Uploaded by

sohamrajam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Code 4

This document contains a C program that takes an infix expression as input from the user and converts it to postfix expression using a stack. It uses a stack to store operators and operands. It pops operators with higher priority and prints them before pushing the current operator onto the stack. It also pops all remaining operators from the stack and prints them after the entire infix expression has been traversed.

Uploaded by

sohamrajam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Prac cal No.

2
Aim :- Convert an Infix expression to Pos ix expression using stack ADT
Program : Sarang R. Shinde
#include<stdio.h> ID no.:VU1F2223060
#include<ctype.h> Class : SE/A Batch : C
#define max 100
char stack [max];
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;
else if (x=='+'||x=='-')
return 1;
else if (x=='*'||x=='/'||x=='%')
return 2;
else if(x=='^')
return 3;
return 0;
}

int main ()
{
char exp [max];
char *e ,x;
prin ("Enter the expression");
scanf ("%s",&exp);
prin ("\n");
e=exp;

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

OUTPUT :
Enter the expression A+(B-C)*D%E^(F/G)+H

ABC-D*EFG/^%+H+

You might also like