首页 > 学院 > 开发设计 > 正文

<The C programming language> 5.2

2019-11-08 02:52:36
字体:
来源:转载
供稿:网友
#include <stdio.h>#include <ctype.h>#define BUFSIZE 100#define NUM 5char buf[BUFSIZE];int bufp = 0;int getch(void);void ungetch(int);int getint(int *);int main(void){	int a[NUM],i;	for(i = 0;i < NUM;i++){ 		int c; while(!(c = getint(&a[i]))) c = getch(); 		if ( c == EOF && i < NUM){ 			for(int j = i;j < NUM;j++) 				a[j] = EOF; 			for(int k = 0;k < NUM;k++){				if (a[k] == EOF)					PRintf("a[%d] is not defined!/n",k);				else					printf("a[%d]=%d/n",k,a[k]);				} 				return 2;		}	}			for(i = 0;i < NUM;i++){		if (a[i] == EOF)			printf("a[%d] is not defined!/n",i);		else			printf("a[%d]=%d/n",i,a[i]);		}		return 1;}	int getint(int *pn){		int c, sign;		while(isspace(c = getch()));	if (!isdigit(c) && c != EOF && c != '+' && c != '-'){		ungetch(c);		return 0;	}		sign = (c == '-') ? -1 : 1;	if (c == '+' || c == '-')		c = getch();	for(*pn = 0; isdigit(c); c = getch())*pn = 10 * *pn + (c - '0')		;	*pn *= sign;	if (c != EOF)		ungetch(c);	return c;}int getch(void){	return (bufp > 0) ? buf[--bufp] : getchar();}void ungetch(int c){	if (bufp >= BUFSIZE)		printf("ungetch: too many characters/n");	else		buf[bufp++] = c;}
上一篇:EL中的param和params

下一篇:PAT 1061

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表