Infix into postfix
#include<stdio.h>
#include<conio.h>
#define max 100
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int isoper(char ch);
int precedence(char);
int main()
{
int i,j=0,len;
char infix[max],postfix[max], stack[max];
int top=-1;
printf(" Enter infix");
gets(infix);
len=strlen(infix);
for (i=0; i<len; i++)
{ //opening of for loop
if(isalpha(infix[i]))
postfix[j++]=infix[i];
else if(isoper(infix[i]))
{ //opening of else if
while(top>-1 && stack[top]!='(')
{ //opening of while loop
if(precedence(infix[i])<=precedence(stack[top]))
{ //opening of if
postfix[j++]=stack[top--];
}// closing of if
else break;
}//closing of while
stack[++top]=infix[i];
} //closing of else if
else if(infix[i]=='(')
stack[++top]=infix[i];
else if(infix[i]==')')
{ while (stack[top]!='(')
{ postfix[j++]=stack[top--];
}
top--;
}
}//closing for loop
while (top>-1)
postfix[j++]=stack[top--];
postfix[j++]='\0';
printf("Infix expression %s", infix);
printf("\n postfix expression %s", postfix);
getch();
}// closing main
int precedence(char ch)
{
int prcd;
if(ch=='+'|| ch == '-')
prcd=0;
else if(ch=='*'|| ch=='/')
prcd=1;
else if(ch=='$')
prcd=2;
else
{
printf("Invalid operator");
exit(0);
}
return prcd;
}
int isoper(char ch)
{
if(ch=='+' || ch=='-'|| ch=='*' || ch== '/'|| ch=='$')
return 1;
else
return 0;
}
OUTPUT
#include<conio.h>
#define max 100
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
int isoper(char ch);
int precedence(char);
int main()
{
int i,j=0,len;
char infix[max],postfix[max], stack[max];
int top=-1;
printf(" Enter infix");
gets(infix);
len=strlen(infix);
for (i=0; i<len; i++)
{ //opening of for loop
if(isalpha(infix[i]))
postfix[j++]=infix[i];
else if(isoper(infix[i]))
{ //opening of else if
while(top>-1 && stack[top]!='(')
{ //opening of while loop
if(precedence(infix[i])<=precedence(stack[top]))
{ //opening of if
postfix[j++]=stack[top--];
}// closing of if
else break;
}//closing of while
stack[++top]=infix[i];
} //closing of else if
else if(infix[i]=='(')
stack[++top]=infix[i];
else if(infix[i]==')')
{ while (stack[top]!='(')
{ postfix[j++]=stack[top--];
}
top--;
}
}//closing for loop
while (top>-1)
postfix[j++]=stack[top--];
postfix[j++]='\0';
printf("Infix expression %s", infix);
printf("\n postfix expression %s", postfix);
getch();
}// closing main
int precedence(char ch)
{
int prcd;
if(ch=='+'|| ch == '-')
prcd=0;
else if(ch=='*'|| ch=='/')
prcd=1;
else if(ch=='$')
prcd=2;
else
{
printf("Invalid operator");
exit(0);
}
return prcd;
}
int isoper(char ch)
{
if(ch=='+' || ch=='-'|| ch=='*' || ch== '/'|| ch=='$')
return 1;
else
return 0;
}
OUTPUT
Other C Programming:
C programming beginner from Hello World
Keywords in C programming
Program for Stack Using switch( PUSH POP DISPLAY and empty
Queue Implementation
Matrix Row sum , column sum and transpose
Program that uses pointers to copy an array of double
Break and continue statement
C program for tower of hanoi
Infix into postfix
Keywords in C programming
Program for Stack Using switch( PUSH POP DISPLAY and empty
Queue Implementation
Matrix Row sum , column sum and transpose
Program that uses pointers to copy an array of double
Break and continue statement
C program for tower of hanoi
Infix into postfix
Post a Comment