2017年11月19日
文字列→実数変換関数atof()の自作
<PR>



今回はC言語のstdlib.hライブラリのatoi関数の自作ソースコードを
載せたいと思います。
<PR>





atoi()関数の自作
今回はC言語のstdlib.hライブラリのatoi関数の自作ソースコードを
載せたいと思います。
atoi()関数の自作
double my_atof(char array[])
{
int i, j, temp1, temp2, sa;
int figure1, figure2, number1 = 0;
double number, number2 = 0;
sa = '0' - 0;
figure1 = 0;
figure2 = 0;
for(i = 0;array[i] != '\0';i++)
{
if(array[i] >= '0' && array[i] <= '9')
figure1++;
else
break;
}
temp1 = figure1;
if(array[i] == '.')
{
for(i += 1;array[i] != '\0';i++)
{
if(array[i] >= '0' && array[i] <= '9')
figure2++;
else
break;
}
}
temp2 = figure2;
for(i = 0;i < figure1;i++, temp1--)
{
array[i] -= sa;
number1 += array[i] * pow_10(temp1 - 1);
}
for(i = figure1 + 1 , j = 1;i < figure1 + figure2 + 1;i++, j++)
{
array[i] -= sa;
number2 += array[i] * pow_10_inverse(j);
}
number = number1 + number2;
return number;
}
double pow_10_inverse(int n)
{
int i;
double result = 1;
if(n == 0)
return 1;
else
{
for(i = 0; i < n;i++)
{
result *= 0.1;
}
return result;
}
}
int pow_10(int n)
{
int i, result = 1;
if(n == 0)
return 1;
else
{
for(i = 0; i < n;i++)
{
result *= 10;
}
return result;
}
}
<PR>


【このカテゴリーの最新記事】
-
no image
-
no image
-
no image
-
no image
-
no image
この記事へのコメント
コメントを書く
この記事へのトラックバックURL
https://fanblogs.jp/tb/6986115
※ブログオーナーが承認したトラックバックのみ表示されます。
この記事へのトラックバック