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

postfix evaluation

This C program converts an infix expression to postfix notation using a stack. It defines functions for stack operations, operator precedence, and the conversion process. The main function prompts the user for an infix expression and displays the corresponding postfix expression.

Uploaded by

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

postfix evaluation

This C program converts an infix expression to postfix notation using a stack. It defines functions for stack operations, operator precedence, and the conversion process. The main function prompts the user for an infix expression and displays the corresponding postfix expression.

Uploaded by

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

#include <stdio.

h>
#include <string.h>
char infix[50],postfix[50],s[50];
int top=-1;
void push(char symb);
char pop();
int precedence(char symb);
int isempty ();
void convertexp(char infix[20]);

void push(char symb)


{
s[++top]=symb;
}
char pop()
{
return(s[top]--);
}
int precedence(char symb)
{
switch(symb)
{
case '+' :
case '-' :
return 1;

case '*' :
case '/' :
return 2;

case '^' :
case '$' :
return 3;
// break;
case '(' :
//case ')' :
return 0;
// break;
default:
return 0;
}

void convertexp(char infix[])


{ unsigned int i,j=0;

char scannedsymb,symb;
for (i=0;i<strlen(infix);i++)
{
symb=infix[i];
switch(symb)
{
case '(': push(symb);
break;
case ')':
while((scannedsymb=pop())!='(')
{
postfix[j++]=scannedsymb;
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
while(!isempty() && precedence(s[top])>=precedence(symb))
postfix[j++]=pop();
push(symb);
break;
default:
postfix[j++]=symb;

}
}
while(!isempty())
{
postfix[j++]=pop();
}
puts (postfix);
//printf("postfix expression is %s",postfix);
}

int isempty ()
{
if (top==-1)
return 1;
else
return 0;
}

void main()
{
printf("enter infix expression:=");
scanf("%s",infix);
convertexp(infix) ;
getch();
}

You might also like