Страницы

Translate

суббота, 24 августа 2013 г.

Exercise 4-8. Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.

Exercise 4-8. Suppose that there will never be more than one character of pushback. Modify getch and ungetch accordingly.


/* getch */
char buf = 0;

int getch(void) // get a (possibly pushed-back) character  
{
    int c;
    if(buf == 0)
        c = getchar();
    else
        c = buf;
    buf = 0;
    return c;    
}

/* ungetch */
void ungetch(int c) // push character back on input
{
    if(buf != 0)
        printf("ungetch: too many characnters\n");
    else
        buf = c;
}

Result:


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

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