1
1
/*
2
2
* 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?
4
4
*
5
5
* By Faisal Saadatmand
6
6
*/
7
7
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
+
8
14
#include <stdio.h>
9
15
#include <ctype.h>
10
16
11
17
#define BUFSIZE 100
12
- #define SIZE 100
13
18
14
19
/* functions */
15
20
int getch (void );
16
21
void unget (int );
17
22
int getfloat (double * );
18
23
19
24
/* 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 */
22
27
23
- int getch (void ) /* get a (possibly pushed back) character */
28
+ int getch (void ) /* get a (possibly pushed back) character */
24
29
{
25
30
return (bufp > 0 ) ? buf [-- bufp ] : getchar ();
26
31
}
27
32
28
- void ungetch (int c ) /* push character back on input */
33
+ void ungetch (int c ) /* push character back on input */
29
34
{
30
35
if (bufp >= BUFSIZE )
31
36
printf ("ungetch: too many characters\n" );
@@ -36,45 +41,36 @@ void ungetch(int c) /* push character back on input */
36
41
/* getfloat: get next float from input into *pn */
37
42
int getfloat (double * pn )
38
43
{
39
- int c , sign , signChar , decimalPoint = 1 ;
44
+ int c , sign ;
45
+ double i ;
40
46
41
- while (isspace (c = getch ())) /* skip white space */
47
+ while (isspace (( c = getch ()))) /* skip whitespace */
42
48
;
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 ;
54
52
}
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 */
55
57
for (* pn = 0 ; isdigit (c ); c = getch ())
56
58
* pn = 10 * * pn + (c - '0' );
57
59
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 ;
62
62
* pn *= sign ;
63
- if (c != EOF )
64
- ungetch (c );
63
+ ungetch (c );
65
64
return c ;
66
65
}
67
66
68
67
int main (void )
69
68
{
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 );
72
74
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" );
79
75
return 0 ;
80
76
}
0 commit comments