Thread: Postfix expression C++

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Karachi, Pakistan, Pakistan
    Posts
    1

    Post Postfix expression C++

    I made a program and I have a problem here.

    Problems ::
    1. This code accepts 1 digit only if you enter 5 6 + this program shows 11 but it you enter 11 22 + it shows 4. please help me out to change it upto 3 digits.

    2. If a user enters 2 operators like 30 40 + * it should display an error "Two operators are not allowed"

    3. if user enters 6 2 3 + - 3 8 2 / + * 2 ^ 3 + result 52 should appear.

    here is the code.....

    Code:
     
    
    
    #include <stdio.h>
    #include <ctype.h>
    
    
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cstdio>
    #include <iomanip>
    #include <cstdlib>
    
    
    #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;
                        
                    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 Exp<b></b>ression : ");
    
    
        gets(exp);
        
    
    
        printf("%s EQUALS %d\n",exp,evaluate(&exp[0]));
        
        gets(exp); 
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    37
    Can you give some general idea what this program is for?
    Some examples? (there are some but the line is messed up so i didn't understand what you meant).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Headers: you don't need <stdlib.h> or <stdio.h> since you've already got the more suitable equivalents <cstdlib> and <cstdio>. As for <ctype.h>, the equivalent is <cctype>.



    2. Avoid gets as it is an unsafe function. Prefer instead fgets as it allows you to specify the maximum size of the string you wish to accept from the user.



    3. A bit of a nit but... you've posted this in the C++ forum but your I/O is decidedly straight vanilla C. There is nothing in the program, other than the headers you are including, that says "this is a C++ program".



    4.
    1. This code accepts 1 digit only if you enter 5 6 + this program shows 11 but it you enter 11 22 + it shows 4. please help me out to change it upto 3 digits.
    Well, this can be done several ways. I'd probably suggest parsing the input using strtok. Each token you get you can attempt to convert to a number (strtol for example) to see if it is a value or not. If it is a value that you can convert, then push it onto your stack. If not, then you check if is an operator and proceed accordingly.



    5.
    2. If a user enters 2 operators like 30 40 + * it should display an error "Two operators are not allowed"
    I'd suggest making sure there are at least two values on your stack before you attempt to resolve an operator. That would detect these conditions and you could then display an error message to the user. In this case the 30 40 + would evaluate to 70 which would be on the stack... once you parse the asterisk from the user you'd look at the stack and see that there was only one value present which would be a big clue that something was wrong.


    6.
    3. if user enters 6 2 3 + - 3 8 2 / + * 2 ^ 3 + result 52 should appear.
    I haven't looked too deeply at your code however if you at least take care of the other issues from your post (#1 and #2) then you'd be a lot closer to having this issue resolved. Fixing those problems might even take care of this one for you all on its own.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluation of postfix expression
    By Prateekr10 in forum C Programming
    Replies: 3
    Last Post: 12-29-2009, 09:50 AM
  2. How to evaluate a postfix|prefix expression using stack?
    By Marrah_janine in forum C Programming
    Replies: 5
    Last Post: 08-04-2007, 04:12 AM
  3. help in evaluating postfix expression using stacks
    By gau in forum C++ Programming
    Replies: 11
    Last Post: 06-12-2007, 12:21 PM
  4. Stack-Postfix Expression
    By kewell in forum C Programming
    Replies: 1
    Last Post: 07-12-2002, 12:16 PM
  5. Evaluate postfix expression program
    By shad0w in forum C Programming
    Replies: 1
    Last Post: 12-23-2001, 04:40 PM