Sunday, 29 September 2024

Write a program to Evaluation of a postfix expression


#include <stdio.h> #include <ctype.h> #include <string.h> #define MAX 100 // Maximum size of stack // Global stack and top index int stack[MAX]; int top = -1; // Function to push an integer onto the stack void push(int x) { // Push (Inserting Element in stack) operation if (top == MAX - 1) { printf("Stack overflow\n"); } else { stack[++top] = x; // Push the integer onto the stack } } // Function to pop an integer from the stack int pop() { // Pop (Removing element from stack) if (top == -1) { // Check if stack is empty printf("Stack underflow\n"); return -1; // Return -1 to indicate underflow } else { return stack[top--]; // Return the top integer and remove it } } // Function to perform the operation int performOperation(int operand1, int operand2, char operation) { switch (operation) { case '+': return operand1 + operand2; case '-': return operand1 - operand2; case '*': return operand1 * operand2; case '/': return operand1 / operand2; default: return 0; // Return 0 for unknown operation } } // Function to evaluate the postfix expression int evaluatePostfixExpression(char* expression) { int length = strlen(expression); // Iterate from left to right for (int i = 0; i < length; i++) { char c = expression[i]; if (isdigit(c)) { // Convert char digit to int and push onto the stack push(c - '0'); // Push the integer value of the digit } else { // Pop the top two elements for the operation int operand2 = pop(); // Order of popping is important for correct operation int operand1 = pop(); // Perform the operation and push the result back onto the stack int result = performOperation(operand1, operand2, c); push(result); } } return pop(); // Return the final result } int main() { char expression[MAX]; printf("Enter a postfix expression: "); scanf("%s", expression); int result = evaluatePostfixExpression(expression); printf("Result of Postfix Expression: %d\n", result); return 0; }

Method 2

#include <stdio.h>

#include <ctype.h>  // for isdigit function

#define MAX 100     // Stack size

// Stack structure

struct Stack {

    int data[MAX];

    int top;

};

// Function to initialize the stack

void initializeStack(struct Stack* stack) 

{

    stack->top = -1;

}

// Function to push an element onto the stack

void push(struct Stack* stack, int value) {

    if (stack->top < MAX - 1) {

        stack->top++;

        stack->data[stack->top] = value;

    } else {

        printf("Stack overflow\n");

    }

}

// Function to pop an element from the stack

int pop(struct Stack* stack) {

    if (stack->top >= 0) {

        int value = stack->data[stack->top];

        stack->top--;

        return value;

    } else {

        printf("Stack underflow\n");

        return -1;

    }

}

// Function to perform the operation

int performOperation(int operand1, int operand2, char operation) {

    switch (operation) {

        case '+':

            return operand1 + operand2;

        case '-':

            return operand1 - operand2;

        case '*':

            return operand1 * operand2;

        case '/':

            return operand1 / operand2;

        default:

            return 0;

    }

}

// Function to evaluate the postfix expression

int evaluatePostfixExpression(char* expression) {

    struct Stack stack;

    initializeStack(&stack);

    for (int i = 0; expression[i] != '\0'; i++) {

        char c = expression[i];

        if (isdigit(c)) {

            // Convert char digit to int and push onto the stack

            push(&stack, c - '0');

        } else {

            // Pop the top two elements for the operation

            int operand2 = pop(&stack);

            int operand1 = pop(&stack);

            // Perform the operation and push the result back onto the stack

            int result = performOperation(operand1, operand2, c);

            push(&stack, result);

        }

    }

    // The final result should be the only item left in the stack

    return pop(&stack);

}

int main() {

    char expression[MAX];

    //  user for input

    printf("Enter a postfix expression: ");

    scanf("%s", expression);

    int result = evaluatePostfixExpression(expression);

    printf("Result of Postfix Expression: %d\n", result);

    return 0;

}

Convert the following Infix expression into postfix using a table.

A*(B*C+D*E)+F 

Sr No

Expression

Stack

Postfix

0

A


A

1

*

*

A

2

(

* (

A

3

B

* (

A B

4

*

* ( *

A B

5

C

* ( *

A B C

6

+

* ( +

A B C *

7

D

* ( +

A B C * D

8

*

* ( + *

A B C * D

9

E

* ( + *

A B C * D E

10

)

*

A B C * D E * +

11

+

+

A B C * D E * + *

12

F

+

A B C * D E * + * F

13



A B C * D E * + * F +


A ^ (B – C * D /E) + F


Sr No

Expression

Stack

Postfix

0

A


A

1

^

^

A

2

(

^ (

A

3

B

^ (

A B

4

^ ( –

A B

5

C

^ ( –

A B C

6

*

^ ( – *

A B C

7

D

^ ( – *

A B C D

8

/

^ ( – /

A B C D *

9

E

^ ( – /

A B C D * E

10

)

^

A B C D * E / –

11

+

+

A B C D * E / – ^

12

F

+

A B C D * E / – ^ F

13



A B C D * E / – ^ F +