C程序设计语言(第二版) 4-8 最多只压回 一个字符

#include <stdio.h> 
 
int buf = EOF; /* buffer for ungetch */ 
 
int getch(void) /* get a (possibly pushed back) character */ 
{ 
	int temp; 
    if (buf != EOF) { 
		temp = buf; 
		buf = EOF; 
	} else { 
		temp = getchar(); 
	} 
	return temp;                           
} 
  
void ungetch(int c) /* push character back on input */ 
{ 
	if(buf != EOF) 
		printf("ungetch: too many characters\n"); 
	else
        buf = c; 
} 
  
int main(void) 
{ 
	int c; 
 
	while ((c = getch()) != EOF) { 
		if (c == '/') { 
			putchar(c); 
			if ((c = getch()) == '*') {  
				ungetch('#'); 
			}          
		}  
		putchar(c);                
	} 
	return 0; 
} 

 

你可能感兴趣的:(C++,c,C#)