0

hi every one
i need help please
i want a program to evaluate a postfix expression
i have made a code for it .
but it works just with numbers between 0 &9......but i want to use the program with larger numbers
e.g:
20 30 +

= 50
i will put my code.........and please help me...i really need it as soon as possible

EVALUATING EXPRESSION STRINGS

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

#define MAX 50
#define EMPTY -1

struct stack
{
        int data[MAX];
        int top;
};

void emptystack(struct stack* s)
{
        s->top = EMPTY;
}

void push(struct stack* s,int item)
{
        if(s->top == (MAX-1))
        {
                printf("\nSTACK FULL");
        }
        else
        {
                ++s->top;
                s->data[s->top]=item;
        }
}

int pop(struct stack* s)
{
        int ret=EMPTY;
        if(s->top == EMPTY)
                printf("\nSTACK EMPTY");
        else
        {
                ret= s->data[s->top];
                --s->top;
        }
        return ret;
}

void display(struct stack s)
{
        while(s.top != EMPTY)
        {
                printf("\n%d",s.data[s.top]);
                s.top--;
        }
}

int evaluate(char *postfix)
{
        char *p;
        struct stack stk;
        int op1,op2,result;

        emptystack(&stk);
        p = &postfix[0];

        while(*p != '\0')
        {
           /* removes tabs and spaces */
                while(*p == ' ' || *p == '\t')
                {
                        p++;
                }
          /* if is digit */
                if(isdigit(*p))
                {
                        push(&stk,*p - 48);
                }
                else
                {
                   /* it is an operator */
                        op1 = pop(&stk);
                        op2 = pop(&stk);

                        switch(*p)
                        {
                                case '+':
                                        result = op2 + op1;
                                        break;

                                case '-':
                                        result = op2 - op1;
                                        break;

                                case '/':
                                        result = op2 / op1;
                                        break;

                                case '*':
                                        result = op2 * op1;
                                        break;

                                case '%':
                                        result = op2 % op1;
                                        break;

                                default:
                                        printf("\nInvalid Operator");
                                        return 0;
                        }
                        push(&stk,result);
                }
                p++;
        }
        result = pop(&stk);
        return result;
}
int main()
{
        char exp[MAX];
        printf("Enter Postfix Expression : ");
        gets(exp);
        printf("%s EQUALS %d\n",exp,evaluate(&exp[0]));
        return 0;
}
1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by #www#
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.