5 Replies - 4056 Views - Last Post: 27 October 2011 - 07:50 AM Rate Topic: -----

#1 jink  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

function to convert char array to double.

Posted 26 October 2011 - 10:56 PM

I have written a funtion to convert a number like 233.44e-2(in char array) to double.the code for the function is
#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

Is This A Good Question/Topic? 0
  • +

Replies To: function to convert char array to double.

#2 imnoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 26-October 11

Re: function to convert char array to double.

Posted 26 October 2011 - 11:04 PM

double toString(char *myStr,int p)
{
double myDouble = atof(myStr);
myDouble = myDouble/pow10(p);
return(myDouble);
}
Was This Post Helpful? 0
  • +
  • -

#3 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: function to convert char array to double.

Posted 26 October 2011 - 11:55 PM

What is the value of i when the program starts?
Also,
if(point=1)


maybe you want to use '==' here instead of '='.
That should solve some of the problem, if it stil doesn't work post back :)
Was This Post Helpful? 1
  • +
  • -

#4 jink  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: function to convert char array to double.

Posted 27 October 2011 - 12:24 AM

yeah i should have initialised i and used the == operator.mistake.but still its giving value 0.00000.you can try running both the codes yourself yourself if you want to verify.modified code
#include <stdio.h>
#include<ctype.h>
double atof(char s[])
{
       int i=0,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]=='+')
       {
       if(s[i]=='-')
       sign=-1;
       i++;
       }
       for(;isdigit(s[i])||s[i]=='.';i++)
       {
                        if(point==1)
                        pow++;
                        if(s[i]=='.')
                        {
                        point='1';
                        i++;
                        }
                        number=number*10.0+(s[i]-'0');
                              }
       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++)
                               power=power*10+(s[i]-'0');
                               for(j=1;j<=pow;j++)
                               number=number/10.0;
                               if(exp=0)
                               {
                               for(j=1;j<=power;j++)
                               number=number*10.0;
                               }
                               else if(exp=1)
                               {
                               for(j=1;j<=power;j++)
                               number=number/10.0;
                               }
                               }
                               return number;
}

.and for testing
#define N 25
#include<stdio.h>
#include "newkr4-2.c"

double atof(char s[]);
int main()
{
char str[N]="333256E-2";
double y;
y=atof(str);
printf("%6.4f\n",y);
getchar();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: function to convert char array to double.

Posted 27 October 2011 - 02:06 AM

Sorry about the long wait, I wasn't here for a while.
Some more broken ifs
if(exp=0)
else if(exp=1)


Give me a mo while I skim the rest of the code.
And power should start off at 0, not 1. (Think about power = power*10 + (s[i] - '0');

This post has been edited by sk1v3r: 27 October 2011 - 02:08 AM

Was This Post Helpful? 2
  • +
  • -

#6 jink  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 10-July 11

Re: function to convert char array to double.

Posted 27 October 2011 - 07:50 AM

I finally got the code working right after a few more corrections.Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1