Evaluation of postfix expression

This is a discussion on Evaluation of postfix expression within the C Programming forums, part of the General Programming Boards category; hey everyone, i have written a C program to evaluate the value of a postfix expression, but im not getting ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Smile Evaluation of postfix expression

    hey everyone, i have written a C program to evaluate the value of a postfix expression, but im not getting the desired output. The output always comes out to be ZERO at runtime, there is no compilation error,

    please help me out quickly to identify the error, the program is as follows:->


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

    #define MAX 50
    float stack[MAX];
    int top=-1;


    void push(float a)
    {
    if(top==MAX-1)
    return;
    else
    stack[++top]=a;
    }

    float pop()
    {
    if(top==-1)
    return -999;
    else
    return stack[top--];
    }

    void main()
    {
    char pos[50];
    float a, b, c,p;
    int i=0,j=0;
    clrscr();
    printf("\n Enter the postfix expression");
    gets(pos);
    while(pos!='\0')
    {
    if(isdigit(pos))
    {
    p = (float)pos - 48;
    push(p);
    }
    else if( (pos=='*') || (pos=='/') || (pos=='-') || (pos=='+'))
    {
    a = pop();
    b = pop();
    if(pos=='+')
    c = b+a;
    if(pos=='-')
    c = b-a;
    if(pos=='*')
    c = b*a;
    if(pos=='/')
    c = b/a;
    push(c);
    }
    else if(pos==32)
    continue;
    else
    {
    printf("\n Enter only digits and 4 operators");
    j=1;
    break;
    }
    i++;
    }
    if(j==1)
    exit();
    else
    printf("\n The value of the postfix expression is = %d",stack[0]);
    getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't assign a value to pos. pos can't equal a digit or a char. pos is a constant address to the char array named (tada!) pos.

    I believe you meant to use pos[i], everywhere you assign or check the value of pos. .

    I can't see why your compiler isn't giving you warnings and errors. Do you have your alerts shut off or turned way down?

    Anyway, welcome to the forum, and please highlight your code in the future, and click on the # icon near the smilies selector, at the top of the post reply (full) window. That will put your code into the forum as a program, rather than just plain text, and make it look much better.




    Quote Originally Posted by Prateekr10 View Post
    hey everyone, i have written a C program to evaluate the value of a postfix expression, but im not getting the desired output. The output always comes out to be ZERO at runtime, there is no compilation error,

    please help me out quickly to identify the error, the program is as follows:->
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    
    #define MAX 50
    float stack[MAX];
    int top=-1;
    
    
    void push(float a)
    {
      if(top==MAX-1)
        return;
      else
        stack[++top]=a;
    }
    
    float pop()
    {
      if(top==-1)
        return -999;
      else
        return stack[top--];
    }
    
    int main()   //not void main()
    {
      char pos[50];
      float a, b, c,p;
      int i=0, j=0;
      clrscr();
      printf("\n Enter the postfix expression");
      gets(pos);
      while(pos!='\0')   //should be pos[i]
      {
        if(isdigit(pos))   //again pos[i]    
        {
          p = (float)pos - 48;  //(pos[i] - 48)
          push(p);
        }
        else if( (pos=='*') || (pos=='/') || (pos=='-') || (pos=='+'))  //all pos[i]
        {
          a = pop();
          b = pop();
          if(pos=='+')  //pos[i]
            c = b+a;
          if(pos=='-')  //ditto
            c = b-a;
          if(pos=='*')  //etc.
            c = b*a;
          if(pos=='/')
            c = b/a;
          push(c);
        }
        else if(pos==32) 
        {
          ++i;
          continue;
        }
        else
        {
          printf("\n Enter only digits and 4 operators");
          j=1;
          break;
        }
        ++i;
      }
      if(j==1)
        exit();
      else
        printf("\n The value of the postfix expression is = %f",stack[0]);  //thank JacobN for this correction
      getch();
      return 0;
    }
    I have a large program running atm and can't run your program, yet. Have no idea if it works or not.
    Last edited by Adak; 12-29-2009 at 07:55 AM.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    The code you provide doesn't compile, there are several errors:

    1: Main should probably return int unless you're running on some older embedded platform.
    2: The way you're "iterating" over the char array isn't doing what you think you're doing.(I'm too lazy to point out everything here, but there are MANY problems)
    3: Your method is incorrect, look at the example of 2+2, if you code worked as intended:

    Push 2.0f to the stack. So far so good.
    Encounter operator '+'. OOPS!
    Pop 2.0f from the stack. OOPS!
    Pop value from empty stack. OOPS!
    Calculate result. OOPS!
    Push result to stack. OOPS!
    Push 2.0f to the stack. OOPS!
    Print value at the top of the stack. Incorrect result.

    The method should probably be a 2-step one including:

    Shunting-yard algorithm - Wikipedia, the free encyclopedia
    Reverse Polish notation - Wikipedia, the free encyclopedia

    4: When you print a float using printf, don't use the %d format flat, that's for a signed integer, that'll not work with a float, use %f instead.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    2

    Smile Error found!!!!

    Hey with the help of one of my friend, im able to identify the errors, they are:

    1. as JacoBn has suggested, since i want to print a float value i need to use %f instead of %d as formatting string

    2. while checking for gape if any as in the following line

    else if(pos[i]==32)
    continue;

    it is not incrementing the value of i, so the program is going in an infinite loop, thus to avoid it instead of "while", "for" should be used.


    3. if the expression contains sumthing other than operators, digits and spaces it should display the message and exit,

    however the exit function doesnt allow the displaying of the message so the following thing shud be done:



    instead of

    if(j==1)
    exit();
    else
    printf("\n The value of the postfix expression is = %f",stack[0]);

    use

    if(j==0)
    printf("\n The value of the postfix expression is = %f", stack[0]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  4. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21