The way to write arithmetic expression is known as a notation. An arithmetic expression can be written in three different but equivalent notations, i.e., without changing the essence or output of an expression. These notations are -
- Infix notation
- Prefix (Polish) notation
- Postfix (Reverse Polish) notation
Infix notation:
Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands – "infixed operators" – such as the plus sign in "2 + 2". Infix notation is more difficult to parse by computers than prefix notation ( e.g. + 2 2 ) or postfix notation ( e.g. 2 2 + ).
Postfix notation:
Postfix notation is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity, some popular hand-held calculators use postfix notation to avoid the complications of the multiple parentheses required in nontrivial infix expressions. You are to write a computer program that simulates how these postfix calculators evaluate expressions.
Here
is source code of the C Program to convert Infix to postfix using Stack data structure. The C program is successfully compiled and
run on a Linux system. The program output is also shown below.
Procedure for Postfix Conversion:
- Scan the Infix equation form left to right.
- If the scanned character is operand, add it to postfix string.
- If scanned character is operator and stack is empty, push it to stack.
- If scanned character is operator and stack is not empty, then compare the precedence of character with the element on the top of stack.
- If the scanned operator has greater precedence than the top element of stack, then push the scanned operator to the stack.
- Else, pop the operator from the stack until the precedence of the top element of stack is greater than or equal to the scanned operator. Push scanned operator to the stack.
- If the scanned character is an ‘(‘, push it to the stack.
- If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered.
- Repeat steps 2-6 until infix expression is scanned.
- Pop and output from the stack until it is not empty.
#include<stdio.h>
#include<ctype.h>
#define MAX 50
int top = -1;
char stack[MAX];
void push(char elem);
char pop();
int pre(char elem);
void main()
{
int i=0, j=0, prec;
char infx[50], pofx[50], ch;
printf("Enter the Infix equation: ");
scanf(" %s", &infx);
while (infx[i] != '\0')
{
if (infx[i] == '(')
{
push('(');
}
else if (isalnum(infx[i]))
{
pofx[j++] = infx[i];
}
else if (infx[i] == ')')
{
while (stack[top] != '(')
{
pofx[j++] = pop();
}
pop();
}
else
{
while (pre(stack[top]) >= pre(infx[i]))
{
pofx[j++] = pop();
}
push(infx[i]);
}
i++;
}
while (top != -1)
{
pofx[j++] = pop();
}
pofx[j] = '\0';
printf("\nPostfix equation: %s", pofx);
}
void push(char elem)
{
stack[++top] = elem;
}
char pop()
{
return (stack[top--]);
}
int pre(char elem)
{
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
Output:#include<ctype.h>
#define MAX 50
int top = -1;
char stack[MAX];
void push(char elem);
char pop();
int pre(char elem);
void main()
{
int i=0, j=0, prec;
char infx[50], pofx[50], ch;
printf("Enter the Infix equation: ");
scanf(" %s", &infx);
while (infx[i] != '\0')
{
if (infx[i] == '(')
{
push('(');
}
else if (isalnum(infx[i]))
{
pofx[j++] = infx[i];
}
else if (infx[i] == ')')
{
while (stack[top] != '(')
{
pofx[j++] = pop();
}
pop();
}
else
{
while (pre(stack[top]) >= pre(infx[i]))
{
pofx[j++] = pop();
}
push(infx[i]);
}
i++;
}
while (top != -1)
{
pofx[j++] = pop();
}
pofx[j] = '\0';
printf("\nPostfix equation: %s", pofx);
}
void push(char elem)
{
stack[++top] = elem;
}
char pop()
{
return (stack[top--]);
}
int pre(char elem)
{
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
Enter the Infix equation: (A+B)*C-(D-E)*(F+G)
Postfix equation: AB+C*DE-FG+*-
Postfix equation: AB+C*DE-FG+*-
0 comments:
Post a Comment