#include <stdio.h>
#include<ctype.h>
double atof(char s[])
{
int i,j;//j is for multiplying all ten of decimals
short signed int sign=+1;
short int point=0,pow=0,exp=0;//exp is 0 if exponent is positive and 1 if the exponent is negative
int power=1;
double number=0.0;
while(s[i]=='\t'||s[i]==' '||s[i]=='-'||s[i]=='+')//skipping all unwanted part
{
if(s[i]=='-')
sign=-1;
i++;
}
for(;isdigit(s[i])||s[i]=='.';i++)
{
if(point=1)
pow++;//counting the number of digits after decimal
if(s[i]=='.')
{
point='1';
i++;
}
number=number*10.0+(s[i]-'0');//calculating the entire number ignoring the decimal
}
number*=sign;
for(j=1;j<=pow;j++)
number=number/10.0;//putting proper decimal point for the number
if(s[i]=='e'||s[i]=='E')
{
i++;
if(s[i]=='-')
{
exp=1;
i++;
}
else if(s[i]=='+')
{
exp=0;
i++;
}
for(;isdigit(s[i]);i++)//for collecting the power which is in exponential form
power=power*10+(s[i]-'0');
if(exp=0)
{
for(j=1;j<=power;j++)
number=number*10.0;/*multiplyinging the number with the additional power of ten present in form of exponext*/
}
else if(exp=1)
{
for(j=1;j<=power;j++)
number=number/10.0;//dividing the number with the additional power of ten present in form of exponext
}
}
return number;
}
.to test the function I have written another program
#define N 25
#include<stdio.h>
#include "newkr4-2.c"//file name of above code
double atof(char s[]);
int main()
{
char str[N]="333256E-2";
double y;
y=atof(str);
printf("%f\n",y);
getchar();
return 0;
}
.but when I run the test program I always get the value 0.00000.i have tried searching for the error but couldnt get any.i am presently doing the code in dev cpp compiler.any other improvements needed in the code are also welcome.Also in general I spent quite a lot of time(1/2 hour to 45 mins) in error detection.is it usual?what could I do to avoid it?that is run the programs correctly at the first time?
Thank you in advance,
Jink

New Topic/Question
Reply



MultiQuote



|