PatientJK
PatientJK

Reputation: 43

Evaluate expression(in postfix notation) using concrete stack in C

If the user enter 235+*, the result will be 16.

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

#define MAX 100

struct Stacks
{
    int top;
    unsigned cap;
    int* array;
};

struct Stacks* createStacks(unsigned cap)
{
    struct Stacks* stack = malloc(sizeof(struct Stacks));
    stack->cap = cap;
    stack->top = -1;
    stack->array = malloc(stack->cap * sizeof(int));
    return stack;
}

int isEmpty(struct Stacks* stack)
{
    return stack->top == -1;
}

int isFull(struct Stacks* stack)
{
    return stack->top == stack->cap - 1;
}

void push(struct Stacks* stack, int item)
{
    if (isFull(stack))
        return;
    stack->array[++stack->top] = item;
    printf("%d has been pushed to stack\n", item);
}

int pop(struct Stacks* stack)
{
    if (isEmpty(stack))
        return 0;
    return stack->array[stack->top--];
}

int showTop(struct Stacks* stack)
{
    if (isEmpty(stack))
        return 0;
    return stack->array[stack->top];
}

int isOperand(char ch)
{
    if ((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z'))
        return 1;
    else
        return 0;
}

int main()
{
    struct Stacks* stack = createStacks(MAX);
    char postfix[MAX], ch;
    int i = 0, op1, op2, result, m;

    printf("Please enter a postfix expression: \n");
    fgets(postfix, MAX, stdin);
    while (postfix[i] != '\0')
    {
        ch = postfix[i];
        if (isOperand(ch) == 1)
        {
            printf("Enter the value of %c:", ch);
            scanf("%d", &m);
            push(stack, m);
        }
        else
        {
            op2 = pop(stack);
            op1 = pop(stack);
            switch (ch)
            {
                case '+': result = op1 + op2;
                          push(stack, result);
                          break;
                case '-': result = op1 - op2;
                          push(stack, result);
                          break;
                case '*': result = op1 * op2;
                          push(stack, result);
                          break;
                case '/': result = op1 / op2;
                          push(stack, result);
                          break;
            }
        }
        i++;
   }
    printf("\nThe result is %d", showTop(stack));
    return EXIT_SUCCESS;
}

And below is the output:

Please enter a postfix expression: 
abc+*
Enter the value of a:2
2 has been pushed to stack
Enter the value of b:3
3 has been pushed to stack
Enter the value of c:5
5 has been pushed to stack 
8 has been pushed to stack
16 has been pushed to stack

The result is 0

Can someone help me out why result not correct, thx.

Upvotes: 0

Views: 37

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

When you call fgets, there will be a newline included in your result (if space is available). This results in six characters being in postfix, not five. When that last newline character is processed, you pop two values from the stack (the second one with the stack empty) and don't push any new values. When you print the result at the end you have an empty stack.

You add some validation to your switch (ch) statement to report unexpected characters, and add some code to ignore spaces (space, tab, newline) in the expression you are parsing.

Upvotes: 2

Related Questions