C Program for Infix To Postfix in Conversion.
C Programming Code :)
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char a[100];
int top = -1;
char ans;
void push(char c)
{
top++;
a[top] = c;
}
char pop()
{
if (top == -1)
{
return -1;
}
else
{
ans = a[top];
top = top - 1;
return(ans);
}
}
int proirity_check(char c)
{
if (c == '(')
{
return 0;
}
else if (c == '+' || c == '-' )
{
return 1;
}
else if(c == '*' || c == '/')
{
return 2;
}
}
int main()
{
char s[100];
char n;
int i;
printf("Enter an Infix Expression : =>");
scanf("%s",s);
printf("Infix TO Postfix Conversion is : ");
for(i=0 ; s[i]!='\0'; i++)
{
if (isalnum(s[i]))
{
printf("%c",s[i]);
}
else if (s[i] == '(')
{
push(s[i]);
}
else if (s[i] == ')')
{
n = pop();
while(n != '(')
{
printf("%c",n);
n = pop();
}
}
else
{
while(proirity_check(a[top]) >= proirity_check(s[i]))
{
printf("%c",pop());
}
push(s[i]);
}
}
while(top != -1)
{
n = pop();
printf("%c",n);
}
return 0;
}
0 Comments