4.3.1.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define BUFSIZE 100
  4. char buf[BUFSIZE]; /* buffer for ungetch */
  5. int bufp = 0; /* next free position in buf */
  6. int push_back_char = 'd';
  7. int char_is_pushed_back = 0;
  8. // @deprecated
  9. int getch(void) /* get a (possibly pushed-back) character */
  10. {
  11. if(char_is_pushed_back)
  12. {
  13. if (char_is_pushed_back!=-1)
  14. {
  15. char_is_pushed_back = 0;
  16. return push_back_char;
  17. }
  18. else
  19. {
  20. char_is_pushed_back = 0;
  21. return '\0';
  22. }
  23. }
  24. return (bufp > 0) ? buf[--bufp] : getchar();
  25. }
  26. // @deprecated
  27. void ungetch(int c) /* push character back on input */
  28. {
  29. /* if (bufp >= BUFSIZE)
  30. printf("ungetch: too many characters\n");
  31. else
  32. buf[bufp++] = c;*/
  33. push_back_char = c;
  34. char_is_pushed_back = 1;
  35. }
  36. // @deprecated
  37. void ungets(char s[]) /* push string back on input */
  38. {
  39. if (s == 0) return; //we need a valid string. null will not do.
  40. for (int i =0 ; ; i++)
  41. {
  42. if (s[i] == 0) break; // we've reached the end of the string
  43. else
  44. {
  45. ungetch(s[i]);
  46. }
  47. }
  48. }