After you have the appropriate infix to postfix expression how would you do the math with the expression? I'm having difficulty programming it.

// Operator supported: +,-,*,/,%,^,(,)
// Operands supported: all single character operands

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

#define MAX 50

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

int precedence(char);
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int top(stack *);   //value of the top element
void infix_to_postfix(char infix[],char postfix[]);

void main()
{
    char infix[30],postfix[30];
    printf("Enter an infix expression(eg: 5+2*4): ");
    gets(infix);
    infix_to_postfix(infix,postfix);
    printf("\nPostfix expression: %s",postfix);
}

void infix_to_postfix(char infix[],char postfix[])
{
    stack s;
    char x,token;
    int i,j;    //i-index of infix,j-index of postfix
    init(&s);
    j=0;

    for(i=0;infix[i]!='\0';i++)
    {
        token=infix[i];
        if(isalnum(token))
            postfix[j++]=token;
        else
            if(token=='(')
               push(&s,'(');
        else
            if(token==')')
                while((x=pop(&s))!='(')
                      postfix[j++]=x;
                else
                {
                    while(precedence(token)<=precedence(top(&s))&&!empty(&s))
                    {
                        x=pop(&s);
                        postfix[j++]=x;
                    }
                    push(&s,token);
                }
    }

    while(!empty(&s))
    {
        x=pop(&s);
        postfix[j++]=x;
    }

    postfix[j]='\0';
}

int precedence(char x)
{
    if(x=='(')
        return(0);
    if(x=='+'||x=='-')
        return(1);
    if(x=='*'||x=='/'||x=='%')
        return(2);

    return(3);
}

void init(stack *s)
{
    s->top=-1;
}

int empty(stack *s)
{
    if(s->top==-1)
        return(1);

    return(0);
}

int full(stack *s)
{
    if(s->top==MAX-1)
        return(1);

    return(0);
}

void push(stack *s,int x)
{
    s->top=s->top+1;
    s->data[s->top]=x;
}

int pop(stack *s)
{
    int x;
    x=s->data[s->top];
    s->top=s->top-1;
    return(x);
}

int top(stack *p)
{
    return (p->data[p->top]);
}

how would you do the math with the expression?

Push any operands. On an operator, pop the number of operands required, then push the result. Repeat until there's only a result. For example, 1 + (2 * 4 + 3) would be converted to 1 2 4 * 3 + +. It would be evaluated like so:

  • Push 1 - Stack: {1}
  • Push 2 - Stack: {2,1}
  • Push 4 - Stack: {4,2,1}
  • Pop 4 and 2, multiply them, and push 8 - Stack: {8,1}
  • Push 3 - Stack: {3,8,1}
  • Pop 3 and 8, add them, and push 11 - Stack: {11,1}
  • Pop 11 and 1, add them, and push 12 - Stack: {12}
  • You're done, pop the final result of 12 - Stack: {}

My question is why shall we go for maths after converting. If you simply want result go for normal procedure while for showing the infix or prefix you do as you have done.

@deceptikon

Your procedure is right but why we need so much ?

This article has been dead for over six months. Start a new discussion instead.