#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h> double atof_num(char s[])
{
double val,power = 1.0;
int i,sign,j,val_lat = 0; for(i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1; if(s[i] == '+' || s[i] == '-')
i++;
for(val = 0.0; isdigit(s[i]);i++)
val = 10.0 * val + (s[i] - '0');
if(s[i] == '.')
i++;
for(power = 1.0; isdigit(s[i]);i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0;
}
if(s[i] == 'e' || s[i] == 'E')
{
i++;
if(s[i] == '-')
{
i++;
while(isdigit(s[i]))
{
val_lat = 10 * val_lat + (s[i] - '0');
i++;
}
for(j = 0;j < val_lat;j++ )
power *= 10.0;
return val * sign / power;
} else if(s[i] == '+')
{
i++;
while(isdigit(s[i]))
{
val_lat = 10 * val_lat + (s[i] - '0');
i++;
}
for(j = 0;j < val_lat;j++ )
power /= 10.0;
return val * sign / power;
}
else
{ while(isdigit(s[i]))
{
val_lat = 10 * val_lat + (s[i] - '0');
i++;
}
for(j = 0;j < val_lat;j++ )
power /= 10.0;
return val * sign / power;
}
}
return val * sign / power;
} int main()
{
char s[100],c;
int i = 0;;
double result; while((c = getchar()) != '\n')
s[i++] = c;
s[i] = '\0'; result = atof_num(s); printf("%f\n",result); return 0;
}
1、isspace、isdigit函数在ctype.h中;
2、else后面不能加判断语句;
3、s[i++]此类要小心用!