日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言atof字母,C语言字符转换之atof()

發布時間:2025/3/11 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言atof字母,C语言字符转换之atof() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在看下強大的atof()函數,哇哈哈:

/* Convert a string to a double. */

double

atof (const char *nptr)

{

return strtod (nptr, (char **) NULL);

}

#if HAVE_CONFIG_H

# include

#endif

#include

#ifndef errno

extern int errno;

#endif

#include

#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))

# define IN_CTYPE_DOMAIN(c) 1

#else

# define IN_CTYPE_DOMAIN(c) isascii(c)

#endif

#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))

#define ISDIGIT(c) (IN_CTYPE_DOMAIN (c) && isdigit (c))

#define TOLOWER(c) (IN_CTYPE_DOMAIN (c) ? tolower(c) : (c))

#include

#include

#include

#include

/* Convert NPTR to a double. If ENDPTR is not NULL, a pointer to the

character after the last one used in the number is put in *ENDPTR. */

double

strtod (const char *nptr, char **endptr)

{

register const char *s;

short int sign;

/* The number so far. */

double num;

int got_dot; /* Found a decimal point. */

int got_digit; /* Seen any digits. */

/* The exponent of the number. */

long int exponent;

if (nptr == NULL) /*如果為空串,則結束轉換*/

{

errno = EINVAL;

goto noconv; /*轉向處理無法轉換的代碼*/

}

s = nptr;

/* Eat whitespace. */

while (ISSPACE (*s))

++s;

/* Get the sign. */

sign = *s == '-' ? -1 : 1;

if (*s == '-' || *s == '+')

++s;

num = 0.0;

got_dot = 0;

got_digit = 0;

exponent = 0;

for (;; ++s)

{

if (ISDIGIT (*s))

{

got_digit = 1;

/* Make sure that multiplication by 10 will not overflow. */

if (num > DBL_MAX * 0.1)

/* The value of the digit doesn't matter, since we have already

gotten as many digits as can be represented in a `double'.

This doesn't necessarily mean the result will overflow.

The exponent may reduce it to within range.

We just need to record that there was another

digit so that we can multiply by 10 later. */

++exponent;

else

num = (num * 10.0) + (*s - '0');

/* Keep track of the number of digits after the decimal point.

If we just divided by 10 here, we would lose precision. */

if (got_dot)

--exponent;

}

else if (!got_dot && *s == '.')

/* Record that we have found the decimal point. */

got_dot = 1;

else

/* Any other character terminates the number. */

break;

}

if (!got_digit)

goto noconv;

if (TOLOWER (*s) == 'e')

{

/* Get the exponent specified after the `e' or `E'. */

int save = errno;

char *end;

long int exp;

errno = 0;

++s;

exp = strtol (s, &end, 10);

if (errno == ERANGE)

{

/* The exponent overflowed a `long int'. It is probably a safe

assumption that an exponent that cannot be represented by

a `long int' exceeds the limits of a `double'. */

if (endptr != NULL)

*endptr = end;

if (exp < 0)

goto underflow;

else

goto overflow;

}

else if (end == s)

/* There was no exponent. Reset END to point to

the 'e' or 'E', so *ENDPTR will be set there. */

end = (char *) s - 1;

errno = save;

s = end;

exponent += exp;

}

if (endptr != NULL)

*endptr = (char *) s;

if (num == 0.0)

return 0.0;

/* Multiply NUM by 10 to the EXPONENT power,

checking for overflow and underflow. */

if (exponent < 0)

{

if (num < DBL_MIN * pow (10.0, (double) -exponent))

goto underflow;

}

else if (exponent > 0)

{

if (num > DBL_MAX * pow (10.0, (double) -exponent))

goto overflow;

}

num *= pow (10.0, (double) exponent);

return num * sign;

overflow:

/* Return an overflow error. */

errno = ERANGE;

return HUGE_VAL * sign;

underflow:

/* Return an underflow error. */

if (endptr != NULL)

*endptr = (char *) nptr;

errno = ERANGE;

return 0.0;

noconv:

/* There was no number. */

if (endptr != NULL)

*endptr = (char *) nptr;

return 0.0;

}

總結

以上是生活随笔為你收集整理的c语言atof字母,C语言字符转换之atof()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。