Created
August 10, 2020 12:30
-
-
Save AakashCode12/4e040a8de65bef6443269a268af934b8 to your computer and use it in GitHub Desktop.
Practical-4 || Data Structures || Postfix Evaluation Using Stacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
#include<string.h> | |
#include<stdlib.h> | |
#include<math.h> | |
#define max 100 | |
int st[max],top = -1; | |
//todo Function prototypes | |
void push(int ); | |
int pop (); | |
//todo push Function | |
void push(int c) | |
{ | |
if (top == max-1){ | |
printf("\nStack is Full (type a short equation)"); | |
} | |
else{ | |
top++; | |
st[top]= c; | |
} | |
} | |
//todo pop Function | |
int pop() { | |
char val; | |
if (top == -1) { | |
//!printf("\nStack is empty"); | |
} | |
else{ | |
val=st[top]; | |
top--; | |
} | |
return val; | |
} | |
//todo main function | |
void main() | |
{ | |
char exp[100]; | |
int i=0; | |
int op1,op2,val,num; | |
printf("\nEnter the postfix expression :\n "); | |
scanf("%s",exp); | |
do | |
{ | |
if (exp[i]!='+'&& exp[i]!='-'&& exp[i]!='*'&&exp[i]!='/'&&exp[i]!='^'){ | |
num = exp[i] - '0'; | |
push(num); | |
} | |
else | |
{ | |
op2=pop(); | |
op1=pop(); | |
switch(exp[i]){ | |
case '+': val = op1 + op2; | |
break; | |
case '-': val = op1 - op2; | |
break; | |
case '*': val = op1 * op2; | |
break; | |
case '/': val = op1 / op2; | |
break; | |
case '^': val = pow(op1, op2); | |
break; | |
} | |
push(val); | |
} | |
i++; | |
} while (i<strlen(exp)); | |
printf("The solution of the given expression is %d.\n",val); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment