Write a program to convert a given INFIX into POSTFIX. Make sure the program checks for "(
)", and check the balances of parentheses.
Write a program to convert a given INFIX into POSTFIX. Make sure the program checks for "(
)", and check the balances of parentheses.
Solution
#include
#include
#include
#define size 100
using namespace std;
char s[size];
int top=-1;
push(char z)
{ /*PUSH Function*/
s[++top]=z;
}
char pop()
{ /*POP Function*/
return(s[top--]);
}
int pr(char z)
{
switch(z)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
int main()
{
char in[100],post[100],c,z;
int i=0,k=0;
printf("  Enter your Infix Expression : ");
scanf("%s",in);
push('#');
while( (c=in[i++]) != '0')
{
if( c == '(') push(c);
else
if(isalnum(c)) post[k++]=c;
else
if( c == ')')
{
while( s[top] != '(')
post[k++]=pop();
z=pop(); /* Remove ( */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(c) )
post[k++]=pop();
push(c);
}
}
while( s[top] != '#') /* Popping from stack */
post[k++]=pop();
post[k]='0';
printf("  Infix Expression: %s   Postfix Expression: %s ",in,post);
}

Write a program to convert a given INFIX into POSTFIX. Make sure .pdf

  • 1.
    Write a programto convert a given INFIX into POSTFIX. Make sure the program checks for "( )", and check the balances of parentheses. Write a program to convert a given INFIX into POSTFIX. Make sure the program checks for "( )", and check the balances of parentheses. Solution #include #include #include #define size 100 using namespace std; char s[size]; int top=-1; push(char z) { /*PUSH Function*/ s[++top]=z; } char pop() { /*POP Function*/ return(s[top--]); } int pr(char z) { switch(z) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*':
  • 2.
    case '/': return3; } } int main() { char in[100],post[100],c,z; int i=0,k=0; printf(" Enter your Infix Expression : "); scanf("%s",in); push('#'); while( (c=in[i++]) != '0') { if( c == '(') push(c); else if(isalnum(c)) post[k++]=c; else if( c == ')') { while( s[top] != '(') post[k++]=pop(); z=pop(); /* Remove ( */ } else { /* Operator */ while( pr(s[top]) >= pr(c) ) post[k++]=pop(); push(c); } } while( s[top] != '#') /* Popping from stack */ post[k++]=pop(); post[k]='0'; printf(" Infix Expression: %s Postfix Expression: %s ",in,post); }