-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path5-2.c
76 lines (64 loc) · 1.63 KB
/
5-2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Exercise 5-2. Write getfloat, the floating-point anolog of getint. What type
* does getfloat return as its function value?
*
* By Faisal Saadatmand
*/
/*
* Answer: It should return an int, which then could be used to check if the
* operation was successful or not or if the EOF was reached. See main
* function.
*/
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 100
/* functions */
int getch(void);
void unget(int);
int getfloat(double *);
/* global */
int buf[BUFSIZE]; /* buffer from ungetch (can handle EOF push back) */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
/* getfloat: get next float from input into *pn */
int getfloat(double *pn)
{
int c, sign;
double i;
while (isspace((c = getch()))) /* skip whitespace */
;
if (!isdigit(c) && c != '.' && c != '+' && c != '-') {
ungetch(c); /* it is not a number */
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
if (!isdigit((c = getch()))) /* is the sign is follwed by a digit? */
return 0; /* it is not a number */
for (*pn = 0; isdigit(c); c = getch())
*pn = 10 * *pn + (c - '0');
if (c == '.')
for (i = 10.0F; isdigit(c = getch()); i = i * 10.0F)
*pn += (c - '0') / i;
*pn *= sign;
ungetch(c);
return c;
}
int main(void)
{
int input;
double num;
while ((input = getfloat(&num)) && input != EOF)
printf("%g\n", num);
return 0;
}