P177. Implement user defined ( atof ) function.
#include<stdio.h>
#include<stdlib.h>
double
my_atof(const char *);
void
main(int argc,char **argv)
{
int flag,i;
if(argc<2)
printf("Usage: a.out
String \n");
double num = my_atof(argv[1]);
printf("%f\n",num);
}
double
my_atof(const char *p)
{
int i;
float c,num,num1;
if(p[0]=='-' || p[0]=='+')
i=1;
else
i=0;
for(num=0;p[i];i++)
{
if(p[i]>'0' && p[i]<'9')
{
num=num*10+(p[i]-48);
}
else if(p[i]=='.')
break;
else
goto L1;
}
i++;
for(num1=0,c=1;p[i];i++)
{
if(p[i]>'0' && p[i]<'9')
{
num1=num1*10+(p[i]-48);
c=c*10;
}
else
break;
}
num1=num1/c;
num=num + num1;
if(p[0]=='-')
num=-num;
L1:
return num;
}
Comments
Post a Comment