-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetint.c
45 lines (34 loc) · 902 Bytes
/
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
#include "getch.c"
#include <ctype.h>
#include <stdio.h>
int getch(void);
void ungetch(int);
/**
* @brief Get next integer from input into *pn.
*
* @param pn Pointer to integer to store the input.
* @return int EOF if end of input, 0 if the next input is not a number and +ve value if the input contains a valid number.
*/
int getint(int *pn) {
int c, sign;
while (isspace(c = getch())) // skip white space
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); // not a number
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
c = getch();
if (!isdigit(c)) {
ungetch(c);
ungetch((sign == -1)? '-' : '+');
return 0;
}
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
*pn *= sign;
if (c != EOF)
ungetch(c);
return c;
}