#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

int data[MAX_SIZE];
int top = -1;

int is_empty(){
    if(top == -1){
        return 1;
    }
    else{
        return 0;
    }
}


void push(int e){
    data[++top] = e;
}

int pop(){
    if(is_empty()){
        printf("Empty\n");
        exit(0);
    }
    else{
        return data[top--];
    }
}

int evaluate_postfix(char expr[]){

    int i = 0;
    int num = 0;
    int building = 0;

    while(expr[i] != '\0'){
        char c = expr[i];

        if(c >= '0' && c <= '9'){
            num = num * 10 + (c - '0');
            building = 1;
        }
        else if(c == ' '){
            if(building == 1){
                push(num);
                num = 0;
                building = 0;
            }
        }
        else if(c == '+' || c == '-' || c == '*' || c == '/'){
            if(building == 1){
                push(num);
                num = 0;
                building = 0;
            }
            else{

                int a = pop();
                int b = pop();

                if(c == '+'){
                    push(b+a);
                }
                else if(c == '-'){
                    push(b-a);
                }
                else if(c == '*'){
                    push(b*a);
                }
                else if(c == '/'){
                    push(b/a);
                }
            }
        }
        i++;
    }
    if(building == 1){
        push(num);
    }
    return pop();
}

int main(void){

    char expr[201];

    scanf("%[^\n]", expr);

    printf("%d\n", evaluate_postfix(expr));

    return 0;
}






Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: