Skip to content

Commit 26b614f

Browse files
committed
revise answer
1 parent 9d05903 commit 26b614f

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

chapter05/5-2.c

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
/*
22
* Exercise 5-2. Write getfloat, the floating-point anolog of getint. What type
3-
* does getfloat return as its function value? int
3+
* does getfloat return as its function value?
44
*
55
* By Faisal Saadatmand
66
*/
77

8+
/*
9+
* Answer: It should return an int, which then could be used to check if the
10+
* operation was successful or not or if the EOF was reached. See main
11+
* function.
12+
*/
13+
814
#include <stdio.h>
915
#include <ctype.h>
1016

1117
#define BUFSIZE 100
12-
#define SIZE 100
1318

1419
/* functions */
1520
int getch(void);
1621
void unget(int);
1722
int getfloat(double *);
1823

1924
/* global */
20-
int buf[BUFSIZE]; /* buffer from ungetch */
21-
int bufp = 0; /* next free position in buf */
25+
int buf[BUFSIZE]; /* buffer from ungetch (can handle EOF push back) */
26+
int bufp = 0; /* next free position in buf */
2227

23-
int getch(void) /* get a (possibly pushed back) character */
28+
int getch(void) /* get a (possibly pushed back) character */
2429
{
2530
return (bufp > 0) ? buf[--bufp] : getchar();
2631
}
2732

28-
void ungetch(int c) /* push character back on input */
33+
void ungetch(int c) /* push character back on input */
2934
{
3035
if (bufp >= BUFSIZE)
3136
printf("ungetch: too many characters\n");
@@ -36,45 +41,36 @@ void ungetch(int c) /* push character back on input */
3641
/* getfloat: get next float from input into *pn */
3742
int getfloat(double *pn)
3843
{
39-
int c, sign, signChar, decimalPoint = 1;
44+
int c, sign;
45+
double i;
4046

41-
while (isspace(c = getch())) /* skip white space */
47+
while (isspace((c = getch()))) /* skip whitespace */
4248
;
43-
if (!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.')
44-
return 0; /* it is not a number */
45-
sign = (c == '-') ? -1 : 1;
46-
if (c == '+' || c == '-') {
47-
signChar = c;
48-
if (!isdigit(c = getch())) {
49-
if (c != EOF)
50-
ungetch(c);
51-
ungetch(signChar);
52-
return signChar;
53-
}
49+
if (!isdigit(c) && c != '.' && c != '+' && c != '-') {
50+
ungetch(c); /* it is not a number */
51+
return 0;
5452
}
53+
sign = (c == '-') ? -1 : 1;
54+
if (c == '+' || c == '-')
55+
if (!isdigit((c = getch()))) /* is the sign is follwed by a digit? */
56+
return 0; /* it is not a number */
5557
for (*pn = 0; isdigit(c); c = getch())
5658
*pn = 10 * *pn + (c - '0');
5759
if (c == '.')
58-
while (isdigit(c = getch())) {
59-
decimalPoint *= 10;
60-
*pn += (double) (c - '0') / decimalPoint;
61-
}
60+
for (i = 10.0F; isdigit(c = getch()); i = i * 10.0F)
61+
*pn += (c - '0') / i;
6262
*pn *= sign;
63-
if (c != EOF)
64-
ungetch(c);
63+
ungetch(c);
6564
return c;
6665
}
6766

6867
int main(void)
6968
{
70-
int n, input;
71-
double array[SIZE];
69+
int input;
70+
double num;
71+
72+
while ((input = getfloat(&num)) && input != EOF)
73+
printf("%g\n", num);
7274

73-
for (n = 0; n < SIZE && (input = getfloat(&array[n])) != EOF; ++n) {
74-
if (input == 0 || input == '-' || input == '+')
75-
break;
76-
printf(" %g", array[n]);
77-
}
78-
printf("\n");
7975
return 0;
8076
}

0 commit comments

Comments
 (0)