0
int getop(char *s) {
    int i, c;

    while((s[0] = c = getch()) == ' ' || c == '\t')
           ;

    s[1] = '\0';

    if(!isdigit(c) && c != '.') {
        return c;
    }

    i = 0;
    if(isdigit(c))
        while(isdigit(s[++i] = c = getch()))
                ;

    if(c == '.')
        while(isdigit(s[++i] = c = getch()))
                ;

    s[i] = '\0';

    if(!isdigit(c))
        ungetch(c);

    return NUMBER;

}

I came across this fucntion while working on an example named "Reverse polish calculator". we can input numbers for calculator operations via this function, but I'm not getting the working of this function. Like.,

if we enter some input like ---->

12.34 11.34 +

From

while((s[0] = c = getch()) == ' ' || c == '\t')
    ;
s[1] = '\0';

s will contain 1. But from where does the remaining input goes inside s ?

I've gone through this function well and I came to know it's working but then I want to know the deep working, like the complete flow. Any help is highly appriciated.

edit:- After testing various inputs I came to the conclusion that what is the need for getch() and ungetch(), I mean yeah they are there to unread character that is not needed but than look at the test cases.,

  int getop(char *s) {
    int i, c;

    while((s[0] = c = getchar()) == ' ' || c == '\t')
        ;

    s[1] = '\0';

    if(!isdigit(c) && c != '.') {
        return c;
    }

    i = 0;
    if(isdigit(c))
        while(isdigit(s[++i] = c = getchar()))
                ;

    if(c == '.')
        while(isdigit(s[++i] = c = getchar()))
                ;

    s[i] = '\0';

   /* if(!isdigit(c))
        ungetch(c);*/

    return NUMBER;


}

Here I replaced getch() with getchar() and it still accepted the input

12a 12 -

and the output was absolutely correct and it unread 'a' character as well

144

and so was the case when I was using getch() ?

nikumbh tyagi
  • 39
  • 1
  • 6
  • 1
    It looks like he statement `while((s[0] = c = getch()) == ' ' || c == '\t');` is used to remove any leading white space before a token. Given you input, yes the first character read is `1`, which is assigned to c _and_ s[0]. also 1 != ' ' and 1 != '\t' so the check will fail, and we fall through to the rest of the function. The next if is used to catch operators. Finally, the lines after `i=0` is used to extract the numbers. – thurizas Jul 11 '17 at 16:45
  • 1
    I don't understand what you're asking but https://stackoverflow.com/questions/31630268/k-and-r-reverse-polish-notation?rq=1 and https://stackoverflow.com/questions/33299349/getop-function-kr-book-p-78?rq=1 are related. – melpomene Jul 11 '17 at 16:45
  • It's not clear that the function handles EOF properly. – Jonathan Leffler Jul 11 '17 at 22:11

1 Answers1

0

Lets break it down piece by piece:

  • while((s[0] = c = getch()) == ' ' || c == '\t') skips all spaces and tabulation from the beginning fo the string. At the end of this loop s[0] contains the first char which is not either of the two mentioned before.

Now if c is something other than . or a digit we simply return that.

   if(!isdigit(c) && c != '.') {
        return c;
    }

If c is not a digit we are done and it's quite right to set s[1] to null!

Otherwise, if c is a digit we read as much digit chars as we can overwriting s from position 1. s[1] was '\0' but it does not matter because s[++i] would be s[1] at the very first iteration of

 i = 0;
    if(isdigit(c))
        while(isdigit(s[++i] = c = getch()))

so s is kept in a consistent status.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
  • Now where does s[++i] = c gets it's remaining input. Like if we input 123, s[0] will have '1' and s[1] will be '\0'. what about remaining '23'. where does it get saved ? – nikumbh tyagi Jul 11 '17 at 16:57
  • S[1] gets null but then what happen? The program does not return and infact it will overwrite S[1] with 2. Try to execute the code with a debugger line by line and you will get it :) – Davide Spataro Jul 11 '17 at 17:17