Страницы

Translate

понедельник, 26 августа 2013 г.

Exercise 4.10. An alternate organization uses getline to read an entire input line

Exercise 4.10. An alternate organization uses getline to read an entire input line; this makes getch and ungetch unnecessary. Revise the calculator to use this approach.


/* getop: get next character of numeric operand */
int getop(char s[])
{
    int c, i;
    if(line[m] == '\0')
    {
        if(getlin(line, MAXLINE) == 0)
            return EOF;
        else
            m = 0;
    }
    while((s[0] = c = line[m++]) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    i = 0;
    if(!isdigit(c) && c != '.') // not a number
        return c;
    i = 0;
    if(isdigit(c)) // collect integer part
        while(isdigit(s[++i] = c = line[m++]))
            ;
    if(c == '.') // collect fraction part
        while(isdigit(s[++i] = c = line[m++]))
            ;
    s[i] = '\0';
    m--;
    return NUMBER;
}

Комментариев нет:

Отправить комментарий