10
10
#include <ctype.h>
11
11
12
12
#define BUFSIZE 100
13
- #define SIZE 100
14
13
15
14
/* functions */
16
15
int getch (void );
17
16
void ungetch (int );
18
17
int getint (int * );
19
18
20
19
/* global */
21
- int buf [BUFSIZE ]; /* buffer from ungetch */
20
+ int buf [BUFSIZE ]; /* buffer from ungetch (can handle EOF push back) */
22
21
int bufp = 0 ; /* next free position in buf */
23
22
24
23
int getch (void ) /* get a (possibly pushed back) character */
@@ -37,40 +36,30 @@ void ungetch(int c) /* push character back on input */
37
36
/* getint: get next integer from input into *pn */
38
37
int getint (int * pn )
39
38
{
40
- int c , sign , signChar ;
39
+ int c , sign ;
41
40
42
- while (isspace (c = getch ())) /* skip white space */
41
+ while (isspace (c = getch ())) /* skip white space */
43
42
;
44
- if (!isdigit (c ) && c != EOF && c != '+' && c != '-' )
45
- return 0 ; /* it is not a number */
46
- sign = (c == '-' ) ? -1 : 1 ;
47
- if (c == '+' || c == '-' ) {
48
- signChar = c ;
49
- if (!isdigit (c = getch ())) {
50
- if (c != EOF )
51
- ungetch (c );
52
- ungetch (signChar );
53
- return signChar ;
54
- }
43
+ if (!isdigit (c ) && c != '+' && c != '-' ) {
44
+ ungetch (c ); /* it is not a number */
45
+ return 0 ;
55
46
}
47
+ sign = (c == '-' ) ? -1 : 1 ;
48
+ if (c == '+' || c == '-' )
49
+ if (!isdigit ((c = getch ()))) /* sign followed by a digit? */
50
+ return 0 ; /* not a number */
56
51
for (* pn = 0 ; isdigit (c ); c = getch ())
57
52
* pn = 10 * * pn + (c - '0' );
58
53
* pn *= sign ;
59
- if (c != EOF )
60
54
ungetch (c );
61
55
return c ;
62
56
}
63
57
64
58
int main (void )
65
59
{
66
- int array [SIZE ], input ;
67
- size_t n ;
60
+ int input , num ;
68
61
69
- for (n = 0 ; n < SIZE && (input = getint (& array [n ])) != EOF ; ++ n ) {
70
- if (input == 0 || input == '-' || input == '+' )
71
- break ;
72
- printf (" %i" , array [n ]);
73
- }
74
- printf ("\n" );
62
+ while ((input = getint (& num )) && input != EOF )
63
+ printf ("%i\n" , num );
75
64
return 0 ;
76
65
}
0 commit comments