Embed presentation
Download to read offline
![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 '*':](https://image.slidesharecdn.com/writeaprogramtoconvertagiveninfixintopostfix-230709065355-7fc3ec49/75/Write-a-program-to-convert-a-given-INFIX-into-POSTFIX-Make-sure-pdf-1-2048.jpg)
![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);
}](https://image.slidesharecdn.com/writeaprogramtoconvertagiveninfixintopostfix-230709065355-7fc3ec49/85/Write-a-program-to-convert-a-given-INFIX-into-POSTFIX-Make-sure-pdf-2-320.jpg)
The document outlines a program that converts infix expressions to postfix notation while ensuring proper handling of parentheses. It includes a C++ code example demonstrating stack operations to manage operators and operands, as well as checking for balanced parentheses. The program takes an infix expression as input and outputs the corresponding postfix expression.
![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 '*':](https://image.slidesharecdn.com/writeaprogramtoconvertagiveninfixintopostfix-230709065355-7fc3ec49/75/Write-a-program-to-convert-a-given-INFIX-into-POSTFIX-Make-sure-pdf-1-2048.jpg)
![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);
}](https://image.slidesharecdn.com/writeaprogramtoconvertagiveninfixintopostfix-230709065355-7fc3ec49/85/Write-a-program-to-convert-a-given-INFIX-into-POSTFIX-Make-sure-pdf-2-320.jpg)