Program For Evaluating Postfix Expression Using Stacked

C++ program to evaluate an expression entered in postfix form. 6 Responses to “C++ program to evaluate an expression entered in postfix form”. C++ program to implement Stack using Formula Based Representation. C++ program to multiply two polynomials maintained as linked lists. Infix to postfix conversion and postfix expression evaluation. In this C Program, we take an infix expression as input from the user and convert it in to a postfix expression using a stack.

Infix to postfix conversion and postfix expression evaluation. In this C Program, we take an infix expression as input from the user and convert it in to a postfix expression using a stack. Then we evaluate that postfix expression to obtain the result.

Contents

Aiohow.org is not responsible for third party website content. The media files you download with aiohow.org must be for time shifting, personal, private, non commercial use only and remove the files after listening. It is illegal for you to distribute copyrighted files without permission. If one of this file is your intelectual property (copyright infringement) or child pornography / immature sounds, please or email to info[at]aiohow.org to us. Aiohow.org is Media search engine and does not host any files, No media files are indexed hosted cached or stored on our server, They are located on soundcloud and Youtube, We only help you to search the link source to the other server. Kis desh mein mera dil title songs download.

Problem Statement

Write a C Program to convert a given infix expression to postfix and evaluate it. Infix expression is the most commonly used expression and we are all familiar with this. In Infix expression, the operator is between two operands, as in 1 + 2, or “5 + ((2 + 6) × 9) − 8”. In Postfix expression, also called as Reverse Polish Notation or postorder expression, operators are written after their operands. For example the above expressions become 12+ and 526+9*+8- respectively when written in postfix notation.

Read more about Infix, Postfix and Prefix

The Solution

The problem is divided into two tasks. First, the given infix expression is converted to a postfix expression. Then we take that postfix expression and evaluate it. In C Program Examples website, we have already given programs to do these things separately. You can see them here and here.

Here we are doing it in a single program. This is in response to a request from one of our readers in a recent comment.

Aliens, mutants, cyborgs, robots, martial artists, wizards, experiments, super-geniuses, vigilantes, and more, plus the optional Mega-Hero™. 55 dsl.

The Program

/*
* C Program to convert a given infix expression to postfix expression and evaluate it.
* (c) www.c-program-example.com
*/
#defineSIZE50/* Size of Stack */
#include<ctype.h>
#include<stdio.h>
char s[SIZE];
int top = -1; /* Global declarations */
/* Function to remove spaces from given string */
voidRemoveSpaces(char* source) {
char* i = source;
char* j = source;
while(*j != 0) {
*i = *j++;
if(*i != '')
i++;
}
*i = 0;
}
/* Function for PUSH operation */
voidpush(char elem) {
s[++top] = elem;
}
/* Function for POP operation */
charpop() {
return (s[top--]);
}
/* Function for precedence */
intpr(char elem) {
switch (elem) {
case'#':
return0;
case'(':
return1;
case'+':
case'-':
return2;
case'*':
case'/':
return3;
}
}
/*
* Function to convert from infix to postfix expression
*/
voidinfix_to_postfix(char *infix, char *postfix) {
char ch, elem;
int i = 0, k = 0;
RemoveSpaces(infix);
push('#');
while ((ch = infix[i++]) != 'n') {
if (ch '(')
push(ch);
elseif (isalnum(ch))
postfix[k++] = ch;
elseif (ch ')') {
while (s[top] != '(')
postfix[k++] = pop();
elem = pop(); /* Remove ( */
} else { /* Operator */
while (pr(s[top]) >= pr(ch))
postfix[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
postfix[k++] = pop();
postfix[k] = 0; /* Make postfix as valid string */
}
/*
* Function to evaluate a postfix expression
*/
inteval_postfix(char *postfix) {
char ch;
int i = 0, op1, op2;
while((ch = postfix[i++]) != 0) {
if(isdigit(ch))
push(ch-'0'); /* Push the operand */
else { /* Operator,pop two operands */
op2 = pop();
op1 = pop();
switch(ch) {
case'+' : push(op1+op2);
break;
case'-' : push(op1-op2);
break;
case'*' : push(op1*op2);
break;
case'/' : push(op1/op2);
break;
}
}
}
return s[top];
}
voidmain() { /* Main Program */
char infx[50], pofx[50];
printf('nInput the infix expression: ');
fgets(infx, 50, stdin);
infix_to_postfix(infx, pofx);
printf('nGiven Infix Expression: %sPostfix Expression: %s', infx, pofx);
top = -1;
printf('nResult of evaluation of postfix expression : %d', eval_postfix(pofx));
}

A postfix expression is a collection of operators and operands in which the operator is placed after the operands. That means, in a postfix expression the operator follows the operands. Postfix Expression has following general structure. Operand1 Operand2 Operator Example Postfix Expression Evaluation using Stack Data Structure A postfix expression can be evaluated using the Stack data structure. To evaluate a postfix expression using Stack data structure we can use the following steps.

Read all the symbols one by one from left to right in the given Postfix Expression. If the reading symbol is operand, then push it on to the Stack. If the reading symbol is operator (+, -,., / etc.,), then perform TWO pop operations and store the two popped oparands in two different variables (operand1 and operand2). Then perform reading symbol operation using operand1 and operand2 and push result back on to the Stack. Perform a pop operation and display the popped value as final result. Example Consider the following Expression.