[go: up one dir, main page]

0% found this document useful (0 votes)
7 views3 pages

Infixtopostfix

The document is a C program that converts infix expressions to postfix notation using a stack data structure. It defines functions for handling the conversion process, including managing operator precedence and stack operations. The program reads an infix expression from user input and displays the corresponding postfix expression after conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Infixtopostfix

The document is a C program that converts infix expressions to postfix notation using a stack data structure. It defines functions for handling the conversion process, including managing operator precedence and stack operations. The program reads an infix expression from user input and displays the corresponding postfix expression after conversion.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Created by: CPP Viewer

#include<stdio.h>
#include<string.h>
#define SIZE 10
char infix[SIZE],postfix[SIZE];
char stack[SIZE];
int top=-1;
void infixtopostfix();
void dsplay();
int precedence(char);
void push(char);
char pop();
int main()
{
printf("enter infix expression");
fgets(infix,SIZE,stdin);
infix[strcspn(infix, "\n")] = '\0';
infixtopostfix();
dsplay();
return 0;
}
void infixtopostfix()
{
int i,j=0;
char ch,x;
for(i=0;i<strlen(infix);i++)
{
ch=infix[i];
switch(ch)
{
case'(' : push(ch);
break;
case')' : while((x=pop())!='(')
{
postfix[j++]=x;
}
break;
case '*':
case '/':
case '+':
case '-':while(top!=-1&&precedence(stack[top])>=precedence(ch))
{
postfix[j++]=pop();
}
push(ch);
break;
default:postfix[j++]=ch;
break;
}
}
while(top!=-1)
{
postfix[j++]=pop();
}
postfix[j]='\0';
}
int precedence(char ch)
{
switch(ch)
{
case '^' :return 3;
break;
case '/' :
case '*' :return 2;
break;
case '+' :
case '-' :return 1;
break;
default :return 0;
break;
}
}
void push(char ch)
{
if(top==SIZE-1)
{
printf("stack is full");
}
else
{
top++;
stack[top]=ch;
}
}
char pop()
{
if(top==-1)
{
printf("stack is empty");
}
else
{
return stack[top--];
}
}
void dsplay()
{
printf("\n after conversion of infix to postfix is\n");
puts(postfix);
}

You might also like