Skip to content

Instantly share code, notes, and snippets.

@amjd
Created September 17, 2014 13:06
Show Gist options
  • Save amjd/f3bc45d41096fece65ee to your computer and use it in GitHub Desktop.
Save amjd/f3bc45d41096fece65ee to your computer and use it in GitHub Desktop.
Program to evaluate postfix expression
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define SIZE 40
int pop();
void push(int);
char postfix[SIZE];
int stack[SIZE], top = -1;
int main()
{
int i, a, b, result, pEval;
char ch;
for(i=0; i<SIZE; i++)
{
stack[i] = -1;
}
printf("\nEnter a postfix expression: ");
scanf("%s",postfix);
for(i=0; postfix[i] != '\0'; i++)
{
ch = postfix[i];
if(isdigit(ch))
{
push(ch-'0');
}
else if(ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
b = pop();
a = pop();
switch(ch)
{
case '+': result = a+b;
break;
case '-': result = a-b;
break;
case '*': result = a*b;
break;
case '/': result = a/b;
break;
}
push(result);
}
}
pEval = pop();
printf("\nThe postfix evaluation is: %d\n",pEval);
return 0;
}
void push(int n)
{
if (top < SIZE -1)
{
stack[++top] = n;
}
else
{
printf("Stack is full!\n");
exit(-1);
}
}
int pop()
{
int n;
if (top > -1)
{
n = stack[top];
stack[top--] = -1;
return n;
}
else
{
printf("Stack is empty!\n");
exit(-1);
}
}
@amulyanampally
Copy link

    if(isdigit(ch))
    {
        push(ch-'0');
    }

i cant understand push(ch-'0');

if we just write push(ch) then we will be able to insert the ASCII value of that character 'ch' but not the actual value of 'ch'. So to get the actual value of 'ch' we must subtract ASCII value of 0 from the ASCII value of character.
Example: we need to push 1. ASCII value of 1 is 49 and the ASCII value of 0 is 48. So 49-48=1.

@Deep425335
Copy link

stack[i] = -1; what does it mean?

@basawaraj
Copy link

    if(isdigit(ch))
    {
        push(ch-'0');
    }

i cant understand push(ch-'0');

if we just write push(ch) then we will be able to insert the ASCII value of that character 'ch' but not the actual value of 'ch'. So to get the actual value of 'ch' we must subtract ASCII value of 0 from the ASCII value of character. Example: we need to push 1. ASCII value of 1 is 49 and the ASCII value of 0 is 48. So 49-48=1.

thank you

@basawaraj
Copy link

stack[i] = -1; what does it mean?

stack is empty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment