Exercise 5-2 (getfloat)
Chapter_5 Exercise_5-1 | pointers Exercise_5-3 |
Exercise 5-2 K&R, p. 97
Exercise 5-2. Write getfloat(), the floating-point analog of getint(). What type does getfloat() return as its function value?
CONTENTS: integers.txt floats.txt getfloat.c
integers.txt download
-123 254 120356 145879 +56 +963 -89 78 3254 -852
0123 142587 -8526 45678 231054 52368 +0236 789 2 0
floats.txt download
-.2 +.3 -1 +02.3 12365 0 -0.2 +0 -9927 -0
-123456789 123456789
getfloat.c download
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#define SIZE 100 // array size
int getfloat(float *); // get the next float from input
int main()
{
int i, n;
float array[SIZE];
for (n = 0; n < SIZE; n++)
{
i = getfloat(&array[n]);
if (i == EOF || i == 0) // end of input or not a number
{break;} // out of for()
}
printf("Array size: %d\n", n);
for (i = 0; i < n; i++) // print array
{printf("%g%c", array[i], (i % 10) == 9 ? '\n' : ' ');}
if (n % 10 != 0) {putchar('\n');}
return 0;
}
#include <ctype.h> // for isspace(), isdigit()
int getch(void);
void ungetch(int);
int getfloat(float *pn) // get the next float from input
{
int c, sign;
int digits = 0; // no of digits > 0 for a valid number
float power;
while (isspace(c = getch())) // skip whitespace
{}
if (c == EOF)
{return c;} // return EOF;
if (!isdigit(c) && c != '+' && c != '-' && c != '.')
{
ungetch(c);
return 0; // not a number
}
sign = (c == '-') ? -1 : 1;
if (c == '+' || c == '-')
{c = getch();}
if (!isdigit(c) && c != '.')
{
ungetch(c);
return 0; // not a number
}
for (*pn = 0; isdigit(c); c = getch()) // get integer part
{
digits++;
*pn = 10 * *pn + (c - '0');
}
if (c == '.')
{c = getch();}
for (power = 1.0; isdigit(c); c = getch()) // get fractional part
{
digits++;
*pn = 10 * *pn + (c - '0');
power *= 10.0;
}
*pn *= sign;
*pn /= power;
ungetch(c); // if (c == EOF), it will be returned by the next call to getfloat()
if (digits == 0) // not a valid number, like `+', `-', `.', `-.', `+.'
{return 0;}
return 1; // valid float found
}
// buffer for ungetch():
int buf = EOF-1; // not a real character, not even EOF
int getch(void) // get a (possibly pushed-back) character
{
if (buf < EOF)
{
return getchar();
}
int temp = buf; // buf >= EOF
buf = EOF-1; // reset buf
return temp;
}
// push character back on input (make it available for the next getch()):
void ungetch(int c)
{
buf = c;
}
/*
gcc getfloat.c -o getfloat
./getfloat
a
Array size: 0
./getfloat
a 1
Array size: 0
./getfloat
+.
Array size: 0
./getfloat
1 a
Array size: 1
1
./getfloat
-.2 // Type Enter, then Ctrl^D in Linux or Ctrl^Z + Enter in Windows (EOF)
Array size: 1
-0.2
./getfloat
-.0
Array size: 1
-0
./getfloat
1.2
Array size: 1
1.2
./getfloat
0
Array size: 1
0
./getfloat
-1 +02
Array size: 2
-1 2
./getfloat
0 1 2 3 4 5 6 7 8 9
Array size: 10
0 1 2 3 4 5 6 7 8 9
./getfloat
0 1 2 3 4 5 6 7 8 9 10
Array size: 11
0 1 2 3 4 5 6 7 8 9
10
./getfloat < integers.txt
Array size: 20
-123 254 120356 145879 56 963 -89 78 3254 -852
123 142587 -8526 45678 231054 52368 236 789 2 0
./getfloat < floats.txt
Array size: 12
-0.2 0.3 -1 2.3 12365 0 -0.2 0 -9927 -0
-1.23457e+08 1.23457e+08
*/
Chapter_5 Exercise_5-1 | BACK_TO_TOP | pointers Exercise_5-3 |
Comments
Post a Comment