#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Explain The Below C Program


    explain the below c code which uses push and pop.please explain the concepts in it.
    Code:
     
      #include <stdio.h>
       #include <stdlib.h>  /* for  atof() */
    
       #define MAXOP   100  /* max size of operand or operator */
       #define NUMBER  '0'  /* signal that a number was found */
    
       int getop(char []);
       void push(double);
       double pop(void);
    
       /* reverse Polish calculator */
       main()
       {
           int type;
           double op2;
           char s[MAXOP];
    
           while ((type = getop(s)) != EOF) {
               switch (type) {
               case NUMBER:
                   push(atof(s));
                   break;
               case '+':
                   push(pop() + pop());
                   break;
               case '*':
                   push(pop() * pop());
                   break;
               case '-':
                   op2 = pop();
                   push(pop() - op2);
                   break;
               case '/':
                   op2 = pop();
                   if (op2 != 0.0)
                       push(pop() / op2);
                   else
                       printf("error: zero divisor\n");
                   break;
               case '\n':
                   printf("\t%.8g\n", pop());
                   break;
               default:
                   printf("error: unknown command %s\n", s);
                   break;
               }
           }
           return 0;
       }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    What part of it don't you understand?

    BTW, you do realize that that is not a complete program. Is it that you don't understand what push() and pop(), which are missing in that code listing, do?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    I just assumed it was a bad rip-off and they're just trying to disguise the origin.

    One of many very similar copies around the web.
    http://webspace.ulbsibiu.ro/macarie....erSIC/POLISH.C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo