Страницы

Translate

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

Exercise 4-9. Our getch and ungetch do not handle a pushed-back EOF correctly.

Exercise 4-9. Our getch and ungetch do not handle a pushed-back EOF correctly. Decide
what their properties ought to be if an EOF is pushed back, then implement your design.


We must change char buf into int buf/

#define BUFSIZE 100

int buf[BUFSIZE];  //buffer for ungetch;
int bufp = 0; //next free position in buf

int getch(void) // get a (possibly pushed-back) character  
{
   return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c) // push character back on input
{
    if(bufp >= BUFSIZE)
        printf("ungetch: too many characnters\n");
    else
        buf[bufp++] = c;
}

Result:


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

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