Created
December 10, 2020 12:59
-
-
Save Burakkepuc/849ef55cf9d67383b9d64c4dbb855d62 to your computer and use it in GitHub Desktop.
Computing Postfix Expression
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 <stdlib.h> | |
| #define SIZE 30 | |
| #include <string.h> | |
| #include <ctype.h> | |
| int stack[SIZE],top = -1; | |
| void push(int x){ | |
| top++; | |
| stack[top] = x; | |
| } | |
| int pop(){ | |
| int opt; //Values coming as integer from evaluatePostfix | |
| opt = stack[top]; | |
| printf("OPT: %d\n",opt); | |
| top--; | |
| return opt; | |
| } | |
| void evaluatePostfix(char arr[]){ | |
| char ch; | |
| int i = 0,op1,op2,res; | |
| printf("%s\n",arr); | |
| while(arr[i] != '\0'){ | |
| ch = arr[i]; | |
| if(isdigit(ch)){ | |
| push(ch - '0'); //This is push real value of ch not ascii value.(push value as an int) | |
| printf("stack[%d]:%d\n",i,stack[i]); | |
| } | |
| else{ | |
| op2=pop(); | |
| op1=pop(); | |
| printf("%d\t%d\n",op2 ,op1); | |
| switch(ch){ | |
| case '+': | |
| res = op1 + op2; | |
| push(res); | |
| break; | |
| case '-': | |
| res = op1 - op2; | |
| push(res); | |
| break; | |
| case '/': | |
| res = op1 / op2; | |
| push(res); | |
| break; | |
| case '*': | |
| res = op1 * op2; | |
| push(res); | |
| break; | |
| } | |
| } | |
| i++; | |
| } | |
| res = pop(); | |
| printf("int last value %d",res); | |
| } | |
| int main(){ | |
| char array[SIZE]; | |
| printf("Enter a postfix expression for computation(as a number): "); | |
| scanf("%s",array); | |
| evaluatePostfix(array); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment