summaryrefslogtreecommitdiffstats
path: root/5/2.c
blob: db37319167217528b34252ffff3f74179efbc680 (plain)
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
#include <ctype.h>
#include <stdio.h>

#define BUFSIZE 100

int getfloat(float *);

int main(int argc, char *argv[]) {
  int rc;
  float f;

  while ((rc = getfloat(&f)) != EOF && rc != 0)
    printf("\t%.8g\n", f);

  if (rc == 0)
    printf("error: not a valid number\n");

  return 0;
}

char buf[BUFSIZE];
int bufp = 0;

int getch() { return (bufp > 0) ? buf[--bufp] : getchar(); }

void ungetch(int c) {
  if (bufp >= BUFSIZE)
    printf("ungetch: too many characters\n");
  else
    buf[bufp++] = c;
}

int getfloat(float *pf) {
  int c, sign, rad, div;

  rad = 0;

  while (isspace(c = getch()))
    ;

  if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.') {
    ungetch(c);
    return 0;
  }

  sign = (c == '-') ? -1 : 1;

  if ((c == '+' || c == '-') && !isdigit(c = getch()) && c != '.') {
    ungetch(c);
    return 0;
  }

  for (*pf = 0.0, div = 1.0; isdigit(c) || c == '.'; c = getch()) {
    if (c == '.') {
      if (rad > 0)
        return 0;
      rad++;
      continue;
    }

    if (rad)
      div *= 10;

    *pf = 10.0 * *pf + (c - '0');
  }

  *pf = (sign * *pf) / div;

  if (c != EOF)
    ungetch(c);

  return c;
}