-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.2.c
56 lines (52 loc) · 992 Bytes
/
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
#include <stdio.h>
#include <ctype.h>
#include "lib/libkr.h"
#define SIZE 10
int getfloat(double *);
/* getfloat: get next double from input into *pn */
int getfloat(double *pn)
{
int c, sign;
double power;
while (isspace(c = getch())) /* skip white space */
;
if (!isdigit(c) && c != EOF && c != '+' && c != '-') {
ungetch(c); /* it is not a number */
return 0;
}
sign = (c == '-') ? -1 : 1;
if (c == '-' || c == '+') {
int buf = c;
if (!isdigit(c = getch())) {
ungetch(c);
ungetch(buf);
return 0;
}
}
for (*pn = 0.0; isdigit(c); c = getch())
*pn = 10.0 * *pn + (c - '0');
if (c == '.')
c = getch();
if (!isdigit(c)) {
ungetch(c);
ungetch('.');
goto out;
}
for (power = 1.0; isdigit(c); c = getch()) {
*pn = 10.0 * *pn + (c - '0');
power *= 10;
}
*pn = (*pn * sign) / power;
if (c != EOF)
ungetch(c);
out:
return c;
}
int
main(void)
{
double array[SIZE];
if ((getfloat(&array[0]) > 0))
printf("%g\n", array[0]);
return 0;
}