Thread: getop - inverse polish calculator - K&R

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    getop - inverse polish calculator - K&R

    I will be grateful if someone could help me to understand this function.

    Code:
    int getch(void);
    void ungetch(int);
    /* getop: get next character or numeric operand */
    int getop(char s[])
    {
    int i, c;
    while ((s[0] = c = getch()) == ' ' || c == '\t')
    ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.')
    return c; /* not a number */
    i = 0;
    if (isdigit(c)) /* collect integer part */
    while (isdigit(s[++i] = c = getch()))
    ;
    if (c == '.') /* collect fraction part */
    while (isdigit(s[++i] = c = getch()))
    ;
    s[i] = '\0';
    if (c != EOF)
    ungetch(c);
    return NUMBER;
    }
    Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The first while loop skips whitespace (space characters or tabs). The other while loops do what they say on the tin (or in this case, the comments attached to them). The last part is ungetch(), which puts the last read character back in the stream to be read next time (since we read until we get something that doesn't fit, we have to put that thing that doesn't fit back into the input).

    The only syntax trick involved here is that you can string multiple assignments together, so in something like
    Code:
    s[0] = c = getch()
    the getch function is called, it gets a character, and that is assigned to c, and is then also assigned to s[0]. And the further reads also add those characters to s as well, so at the end of the day s contains the read-in material.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Thank you! It is quite clear.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    s[1] = '\0';

    but why we have this afterwards? I cannot get it

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Strings must end in a \0 character.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    ok tabstop... maybe this is too much! but please try to help...

    let us say that I have input 1 2 +

    could you explain how this will look like at the getop function!

    looking forward for your answer...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Step through it and see. Remember that getch reads one character at a time, and ungetch puts one character back into the input stream.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    tabstop have you ever read K&R? I am quite near to understand the inverse polish calculator and I solved "based on other solutions" most of the exercises. But now I am at page 80 -> 4.4 Scope of rules ... doest this means that I am not a simple beginner (anymore) and I will be able to progress even more if I finish with pointers and structures successfully (based on your and other helps) here at the forum? What is your opinion...

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I have never read K&R, no. I would suppose that you should consider yourself "ready to move on" if you can work the exercises without 'based on other solutions'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse polish calculator
    By Tool in forum C Programming
    Replies: 1
    Last Post: 12-24-2009, 05:25 PM
  2. How to inverse a trig?
    By Diablo02 in forum C# Programming
    Replies: 4
    Last Post: 11-07-2007, 10:11 PM
  3. Inverse Polish Calculator
    By Sailors in forum C Programming
    Replies: 0
    Last Post: 07-30-2007, 04:51 PM
  4. Using pointers in Inverse polish calculator
    By Sailors in forum C Programming
    Replies: 5
    Last Post: 07-29-2007, 04:24 AM
  5. Polish calculator, problems with getop() function
    By EliasK in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 12:33 AM