forked from wuqingze/cprogramminglanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.getint.c
56 lines (46 loc) · 903 Bytes
/
3.getint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <ctype.h>
#include "getch.c"
#define SIZE 10
int getch(void);
void ungetch(int);
// compile with getch.c
int main()
{
int n, array[SIZE], getint(int *);
for (n = 0; n < SIZE; n++)
array[n] = 0;
for (n = 0; n < SIZE && getint(&array[n]) != EOF; n++)
;
for (n = 0; n < SIZE; n++)
printf("%d ", array[n]);
printf("\n");
return 0;
}
int getint(int *pn)
{
int c=0, sign=-1;
while (isspace(c = getch()))
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c);
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-') {
c = getch();
if (!isdigit(c)) {
ungetch(sign == 1 ? '+' : '-');
return 0;
}
}
// for (*pn = 0; c == '9'; c = getch()){
// printf("*pn=%d, c=%d\n", *pn, c);
// *pn = 10 * *pn + (c - '0');
// }
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}