C Program to Evaluate an Expression using Stacks. C Server Side Programming Programming. Here also we have to use the stack data structure to solve the postfix expressions. From the postfix expression, when some operands are found, pushed them in the stack. When some operator is found, two items are popped from stack and then operation.
Learn how to evaluate Postfix expression using Stack in C Programming Language. This C code for postfix evaluation takes in a postfix expression from the user and then evaluates it.
What is Postfix Expression?
In a postfix operation, he operators are placed after the operands. On scanning the expression from left to right, first the operands are recieved and then the operators.
Steps To Evaluate a Postfix Expression
- Scan the characters of the postfix string from left to right one by one.
- If the character is an operand then push it on the stack.
- If the character is an operator then pop two elements from the stack and apply the operator to these two characters. Now, push the result on the stack.
- After all the characters have been scanned, pop the remaining element of the stack and that is the value of the arithmetic postfix expression.
Example
Infix Expression: (3 + 4) * (2 / 2)
Postfix Expression: 34+22/*
Evaluation: 7
Note: This C program for evaluation of postfix expression using stacks has been compiled with GNU GCC Compiler and developed using gEdit Editor in Linux Ubuntu Operating System.C Program To Evaluate Postfix Expression using Stack
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 | #include<stdlib.h> #include<string.h> voidpush(longintcharacter); intpop(); longintstack[50]; { top=-1; printf('nEnter an Expression in Postfix format:t'); printf('nExpression in Postfix Format: t%sn',postfix_expression); printf('nEvaluation of Postfix Expression: t%ldn',evaluated_value); } longintpostfix_evaluation() longintx,y,temp,value; for(count=0;count<strlen(postfix_expression);count++) if(postfix_expression[count]<='9'&&postfix_expression[count]>='0') push(postfix_expression[count]-'0'); else x=pop(); switch(postfix_expression[count]) case'+':temp=y+x; case'-':temp=y-x; case'*':temp=y *x; case'/':temp=y/x; case'%':temp=y%x; case'^':temp=pow(y,x); default:printf('Invalid'); push(temp); } returnvalue; { { exit(1); top=top+1; } intpop() if(isEmpty()) printf('Stack is Emptyn'); } } intisEmpty() if(top-1) return1; else return0; } |
Ouput
If you have any doubts or compilation errors in this c program for evaluation of postfix expression using stack in data structure, let us know about it in the comment section below.
Recommended Programs |
---|
C Program For Insertion Sort Algorithm |
C Program To Convert Infix String into Postfix String |
C Program For Evaluating a Prefix Expression |
C Program To Convert Prefix String into Infix String |
C Program To Convert Postfix String into Prefix String |
C Program For Tower of Hanoi Algorithm |
C Program To Convert Infix String To Prefix String |
C Program To Convert Postfix String into Infix String |
C Program To Generate Arithmetic Progression |
C Program To Check Matching Parantheses in an Expression |