C语言字符转换之atof()
来源:互联网 发布:现代软件学院毕业证 编辑:程序博客网 时间:2024/05/21 00:19
现在看下强大的atof()函数,哇哈哈:
/* Convert a string to a double. */ double atof (const char *nptr) { return strtod (nptr, (char **) NULL); }
#if HAVE_CONFIG_H # include <config.h> #endif #include <errno.h> #ifndef errno extern int errno; #endif #include <ctype.h> #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 <math.h> #include <float.h> #include <stdlib.h> #include <string.h> /* 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()
- atof c语言实现
- C语言50题之模拟实现atof、atoi
- c语言字符转换
- atof ( )【C语言库函数源代码】
- C语言 atof()的实现
- C语言atof()函数:将字符串转换为double(双精度浮点数)
- C语言atof()函数:将字符串转换为double(双精度浮点数)
- C语言atof()函数:将字符串转换为double(双精度浮点数)
- c字符串转换函数atoi、atof、atol
- strtod ( ) 和 atof ( )【C语言库函数源代码】
- C语言实现atoi,itoa和atof
- C语言实现atoi和atof函数
- 【C语言】编写函数实现库函数atof
- C语言atof函数的探索
- [C语言]模拟实现C语言库函数atof
- C语言之格式字符
- C语言之转义字符
- 计算机科学中最重要的32个算法
- HDU-3791二叉搜索树
- C# 中如何获得屏幕宽度和高度
- Mysql 之MySQL用户变量的用法
- 第十一次课后作业
- C语言字符转换之atof()
- ios和android安全对比
- UML图之五——时序图
- 把数组排成最小的数
- 第十次上机
- Ubuntu12.04修改环境变量
- UML之六——部署图(Deployment Diagram)
- 【读书笔记】《未来闪影》罗伯特·J·索耶
- 数据类型对应字节数(32位,64位 int 占字节数)