Thread: Postfix Notation Calculator

  1. #1
    C Student
    Guest

    Postfix Notation Calculator

    Hi,
    I'm looking for a postfix notation calculator program in C that uses a stack with push and pop functions to compute results. My main problem is with the stack and push and pop -- I do not understand how they work. Please help.

  2. #2
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    Thats all a little above my head!

    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    picture a stack as a stack of dinner plates

    when you push one you put one on top;
    when you pop one you take one off the top.


    so if you input 2 3 +
    you
    push 2
    push 3
    and whe you reach an operator you
    pop 2 and 3 and push the result

    If you have linux or unix, you may have a built in calculator that works this way called dc ( desktop calculator) so you can try it out.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My main problem is with the stack and push and pop -- I do not understand how they work.
    A stack is actually very simple, you have a list of items where only the top item can be accessed. You can push a new item onto the stack, thus creating a new top item, and you can pop the current top off of the stack:
    Code:
    STACK:
    1
    2
    3
    
    POP:
    2
    3
    
    PUSH 4:
    4
    2
    3
    Here is some simple code implementing a fairly general stack structure, feel free to play around with it to get a feel for the concept:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NULLITEM -1
    typedef int item;
    
    struct stack
    {
      item *st;
      size_t size;
      size_t n;
    };
    
    typedef struct stack STACK;
    
    void st_init ( STACK *s, size_t size )
    {
      s->n = 0;
      s->size = size;
      s->st = malloc ( sizeof ( item ) * s->size );
    
      if ( s->st == NULL ) {
        fprintf ( stderr, "Error allocating memory\n" );
        exit ( EXIT_FAILURE );
      }
    }
    
    void st_kill ( STACK *s )
    {
      if ( s->st != NULL )
        free ( s->st );
    }
    
    int st_empty ( STACK *s )
    {
      return s->n == 0;
    }
    
    void st_push ( STACK *s, item new )
    {
      if ( s->n == s->size ) {
        printf ( "Stack is full\n" );
        return;
      }
    
      s->st[s->n++] = new;
      printf ( "Pushed %d\n", new );
    }
    
    item st_pop ( STACK *s )
    {
      if ( s->n == 0 ) {
        printf ( "Stack is empty\n" );
        return NULLITEM;
      }
    
      return s->st[--s->n];
    }
    
    int main ( void )
    {
      int i;
      STACK my_stack = {0};
    
      st_init ( &my_stack, 3 );
    
      for ( i = 0; i < 5; i++ )
        st_push ( &my_stack, i );
    
      for ( i = 0; i < 5; i++ ) {
        int ret = st_pop ( &my_stack );
    
        if ( ret != NULLITEM )
          printf ( "Popped %d\n", ret );
      }
    
      st_kill ( &my_stack );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

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. Postfix (reverse polish notation) calculator
    By ottomated in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 05:32 PM
  3. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  4. stack (array impl) problem: postfix notation
    By kocika73 in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 01:35 PM
  5. Help with Postfix Calculator Project
    By hpy_gilmore8 in forum C Programming
    Replies: 5
    Last Post: 03-12-2003, 11:51 PM