Exercise 5.2. Write getfloat, the floating-point analog of getint. What type does getfloat return as its function value?
/* getint: get next float from input into *pn */
int getfloat(float *pn)
{
int c, sign, mark;
float fract;
while(isspace(c = getch())) //skip white space
;
if(!isdigit(c) && c != EOF && c != '+' && c != '-' && c != '.')
{
ungetch(c); //it's not a number
return 0;
}
sign = (c == '-') ? -1 : 1;
if(c == '+' || c == '-')
{
mark = c; //remember a sign
c = getch();
if(!isdigit(c))
{
if(c != EOF)
ungetch(c); //not a number - back on the input
ungetch(mark); //back sign of the symvol on the input
return 0;
}
}
for(*pn = 0.0; isdigit(c); c = getch())
*pn = 10.0 * *pn + (c - '0'); // collect integer part
if(c == '.')
c = getch();
for(fract = 1.0; isdigit(c); c = getch())
{
*pn = 10.0 * *pn +(c - '0');
fract *=10.0;
}
*pn *= sign/fract;
if(c != EOF)
ungetch(c);
return c;
}
Type function getfloat is INT.
Type function getfloat is INT.
Комментариев нет:
Отправить комментарий