EVALUATION - SUFFIX EXPRESSION

 

Develop a Program in C for the following Stack Applications

  1. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^

Features:

  • Single-digit operands (0 to 9)

  • Operators: +, -, *, /, %, ^

  • Assumes a valid postfix expression

  • Uses stack-based evaluation

 

C Program – Evaluate Postfix Expression

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

#define MAX 100

int stack[MAX];
int top = -1;

// Stack operations
void push(int value) {
    if (top >= MAX - 1) {
        printf("Stack Overflow\n");
        exit(1);
    }
    stack[++top] = value;
}

int pop() {
    if (top == -1) {
        printf("Stack Underflow\n");
        exit(1);

  • This version handles single-digit operands only.

  • You can extend it to support multi-digit operands or variables.

  • Division and modulo by zero are checked to prevent crashes.

Let me know if you want an extended version supporting:

  • Multi-digit numbers

  • Expression from a file

  • Step-by-step output tracing the stack

    }
    return stack[top--];
}

// Integer power function (replaces pow)
int int_power(int base, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= base;
    }
    return result;
}

// Evaluate postfix expression
int evaluatePostfix(const char* expr) {
    int i = 0;
    char ch;

    while ((ch = expr[i++]) != '\0') {
        if (isspace(ch)) continue;

        if (isdigit(ch)) {
            push(ch - '0');  // Convert char to int
        } else {
            int right = pop();
            int left = pop();
            int result;

            switch (ch) {
                case '+': result = left + right; break;
                case '-': result = left - right; break;
                case '*': result = left * right; break;
                case '/':
                    if (right == 0) {
                        printf("Division by zero error\n");
                        exit(1);
                    }
                    result = left / right;
                    break;
                case '%':
                    if (right == 0) {
                        printf("Modulo by zero error\n");
                        exit(1);
                    }
                    result = left % right;
                    break;
                case '^':
                    result = int_power(left, right);
                    break;
                default:
                    printf("Invalid operator: %c\n", ch);
                    exit(1);
            }
            push(result);
        }
    }

    return pop();
}

// Driver code
int main() {
    char expr[MAX];

    printf("Enter a postfix expression (e.g. 23*54*+9-):\n");
    fgets(expr, MAX, stdin);

    int result = evaluatePostfix(expr);
    printf("Result: %d\n", result);

    return 0;
}


Sample Runs

Input:

ceciot@cec-IOT:~$ cd Desktop/
ceciot@cec-IOT:~/Desktop$ gcc suffix.c -o abcd
ceciot@cec-IOT:~/Desktop$ ./abcd

Enter a postfix expression (e.g. 23*54*+9-):
11*16*+6-
Result: 1
ceciot@cec-IOT:~/Desktop$

 

Step-by-step Evaluation:

  • 1 1 * →1

  • 1 6 * →6

  • 1 6 + →7

  • 7 6 - →1

Output:

Result: 1

 notes:

  • This version handles single-digit operands only.

  • You can extend it to support multi-digit operands or variables.

  • Division and modulo by zero are checked to prevent crashes.

Let me know if you want an extended version supporting:

  • Multi-digit numbers

  • Expression from a file

  • Step-by-step output tracing the stack

No comments:

Post a Comment