user-0 提问: atol函数的返回值问题

回复:
user: user-0
content: 不过也无所谓了，反正任何输入前的检查肯定得要自已做，如果是转double的话，把转int的结果稍做一下变换就可以满足了，收贴吧

user: user-0
content: 引用 13 楼 SXJIAKE 的回复:[quote=引用 2 楼 bug1190 的回复:] [quote=引用 1 楼 zxhuser-4 的回复:] 不返回0那返回啥呢？ 我就草了，，我规定，返回值为int，-1表示转换失败，0表示成功，在参数列表中加入一个int型的指针，将返回结果写入！这样还有上面的类似问题吗? [/quote] API：StrToIntEx[/quote] 能不能再找一个关于Str转Double的好用的函数，那样就完美了，strtod不算哟，，

user: user-0
content: 引用 13 楼 SXJIAKE 的回复:[quote=引用 2 楼 bug1190 的回复:] [quote=引用 1 楼 zxhuser-4 的回复:] 不返回0那返回啥呢？ 我就草了，，我规定，返回值为int，-1表示转换失败，0表示成功，在参数列表中加入一个int型的指针，将返回结果写入！这样还有上面的类似问题吗? [/quote] API：StrToIntEx[/quote] 谢谢，哥们帮我解决了大问题了，

user: user-1
content: 引用 2 楼 bug1190 的回复:[quote=引用 1 楼 zxhuser-4 的回复:] 不返回0那返回啥呢？ 我就草了，，我规定，返回值为int，-1表示转换失败，0表示成功，在参数列表中加入一个int型的指针，将返回结果写入！这样还有上面的类似问题吗? [/quote] API：StrToIntEx

user: user-2
content: 实在想在一定范围内精确表示十进制小数，请搜索“定点小数”

user: user-2
content: 引用 10 楼 bug1190 的回复:[quote=引用 9 楼 zhao4zhong1 的回复:] http://bbs.csdn.net/topics/360055953 fscanf我在项目中用过，存在同样的问题，对格式要求太严，“%lf”转换成double时不能保证准确的纯度，本来是555.666,转换后有时就成了555.66600000000003，而且同样有遇到空格的问题，哎，说来说去就是效果差，不太适合在项目当中用，容易被坑，，或者 坑别人，，[/quote] 这只能说明你对计算机表示浮点数理解不透彻。 http://bbs.csdn.net/topics/390676437

user: user-0
content: 引用 9 楼 zhao4zhong1 的回复:http://bbs.csdn.net/topics/360055953 fscanf我在项目中用过，存在同样的问题，对格式要求太严，“%lf”转换成double时不能保证准确的纯度，本来是555.666,转换后有时就成了555.66600000000003，而且同样有遇到空格的问题，哎，说来说去就是效果差，不太适合在项目当中用，容易被坑，，或者 坑别人，，

user: user-2
content: http://bbs.csdn.net/topics/360055953

user: user-0
content: 引用 7 楼 zhao4zhong1 的回复:正则表达式不是万能的，有限状态自动机是万能的。 谁能用正则表达式实现以下文法的解析？ include\float.h ... #define DBL_DIG         15                      /* # of decimal digits of precision */ #define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */ #define DBL_MANT_DIG    53                      /* # of bits in mantissa */ #define DBL_MAX         1.7976931348623158e+308 /* max value */ #define DBL_MAX_10_EXP  308                     /* max decimal exponent */ #define DBL_MAX_EXP     1024                    /* max binary exponent */ #define DBL_MIN         2.2250738585072014e-308 /* min positive value */ #define DBL_MIN_10_EXP  (-307)                  /* min decimal exponent */ #define DBL_MIN_EXP     (-1021)                 /* min binary exponent */ #define _DBL_RADIX      2                       /* exponent radix */ #define _DBL_ROUNDS     1                       /* addition rounding: near */  #define FLT_DIG         6                       /* # of decimal digits of precision */ #define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */ #define FLT_GUARD       0 #define FLT_MANT_DIG    24                      /* # of bits in mantissa */ #define FLT_MAX         3.402823466e+38F        /* max value */ #define FLT_MAX_10_EXP  38                      /* max decimal exponent */ #define FLT_MAX_EXP     128                     /* max binary exponent */ #define FLT_MIN         1.175494351e-38F        /* min positive value */ #define FLT_MIN_10_EXP  (-37)                   /* min decimal exponent */ #define FLT_MIN_EXP     (-125)                  /* min binary exponent */ #define FLT_NORMALIZE   0 #define FLT_RADIX       2                       /* exponent radix */ #define FLT_ROUNDS      1                       /* addition rounding: near */ ... C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.  Syntax  floating-constant :  fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt  fractional-constant :  digit-sequenceopt . digit-sequence digit-sequence .  exponent-part :  e signopt digit-sequence E signopt digit-sequence  sign : one of  + –  digit-sequence :  digit digit-sequence digit  floating-suffix :one of  f l F L  Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:  18.46 38.  The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:  18.46e0      // 18.46 18.46e1      // 184.6  If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.  Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.  Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like  void func( double );  and   void func( long double ); 想来想去，我还是单独写个验证能否转换的函数算了，sscanf_s也不可靠，对格式要求太严，也没必要去为了解决这个问题而使用正则，

user: user-2
content: 正则表达式不是万能的，有限状态自动机是万能的。 谁能用正则表达式实现以下文法的解析？ include\float.h ... #define DBL_DIG         15                      /* # of decimal digits of precision */ #define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */ #define DBL_MANT_DIG    53                      /* # of bits in mantissa */ #define DBL_MAX         1.7976931348623158e+308 /* max value */ #define DBL_MAX_10_EXP  308                     /* max decimal exponent */ #define DBL_MAX_EXP     1024                    /* max binary exponent */ #define DBL_MIN         2.2250738585072014e-308 /* min positive value */ #define DBL_MIN_10_EXP  (-307)                  /* min decimal exponent */ #define DBL_MIN_EXP     (-1021)                 /* min binary exponent */ #define _DBL_RADIX      2                       /* exponent radix */ #define _DBL_ROUNDS     1                       /* addition rounding: near */  #define FLT_DIG         6                       /* # of decimal digits of precision */ #define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */ #define FLT_GUARD       0 #define FLT_MANT_DIG    24                      /* # of bits in mantissa */ #define FLT_MAX         3.402823466e+38F        /* max value */ #define FLT_MAX_10_EXP  38                      /* max decimal exponent */ #define FLT_MAX_EXP     128                     /* max binary exponent */ #define FLT_MIN         1.175494351e-38F        /* min positive value */ #define FLT_MIN_10_EXP  (-37)                   /* min decimal exponent */ #define FLT_MIN_EXP     (-125)                  /* min binary exponent */ #define FLT_NORMALIZE   0 #define FLT_RADIX       2                       /* exponent radix */ #define FLT_ROUNDS      1                       /* addition rounding: near */ ... C++ Floating-Point Constants Floating-point constants specify values that must have a fractional part. These values contain decimal points (.) and can contain exponents.  Syntax  floating-constant :  fractional-constant exponent-partopt floating-suffixopt digit-sequence exponent-part floating-suffixopt  fractional-constant :  digit-sequenceopt . digit-sequence digit-sequence .  exponent-part :  e signopt digit-sequence E signopt digit-sequence  sign : one of  + –  digit-sequence :  digit digit-sequence digit  floating-suffix :one of  f l F L  Floating-point constants have a “mantissa,” which specifies the value of the number, an “exponent,” which specifies the magnitude of the number, and an optional suffix that specifies the constant’s type. The mantissa is specified as a sequence of digits followed by a period, followed by an optional sequence of digits representing the fractional part of the number. For example:  18.46 38.  The exponent, if present, specifies the magnitude of the number as a power of 10, as shown in the following example:  18.46e0      // 18.46 18.46e1      // 184.6  If an exponent is present, the trailing decimal point is unnecessary in whole numbers such as 18E0.  Floating-point constants default to type double. By using the suffixes f or l (or F or L — the suffix is not case sensitive), the constant can be specified as float or long double, respectively.  Although long double and double have the same representation, they are not the same type. For example, you can have overloaded functions like  void func( double );  and   void func( long double );

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:参考下面：#include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() {     p=s;     while (1) {         k=sscanf(p,"%d%n",&v,&n);         printf("k,v,n=%d,%d,%d\n",k,v,n);         if (1==k) {             p+=n;         } else if (0==k) {             printf("skip char[%c]\n",p[0]);             p++;         } else {//EOF==k             break;         }     }     printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End.  看来还是你比较上道，，，明白俺的意思，，我也在考虑用这个函数，应该能解决，不过有一点还不清楚的地方就是：这个函数的格式指定时，能不能有一块的格式写成可有可无的形式，就是如果跟实串匹配成功了，就提取，如果没匹配成功，则也跳过，而不是直接返回个0，就此函数返回，，这种情况是可以用多次调用这个函数来解决，但我觉得那样麻烦，有没有一种高级点的格式写法，只调用一次把这个问题解决掉，，格式比如像： int nRetValue = _stscanf_s(pszBuf,TEXT("%*[ \t+]%[0-9]"),szTextBuf,sizeof(szTextBuf)/sizeof(TCHAR));  	if (nRetValue==0)  	{  		_tprintf_s(TEXT("Sscanf Return Empty\n"));  	}

user: user-3
content: 改用strtol函数就行了，通过第二参数判断是否转换成功  #include <stdio.h> #include <stdlib.h>  int main(void) { 	char str[][10] = { "0", "0a", " 0 " }; 	long l; 	char *end; 	int i;  	for (i = 0; i < sizeof(str) / sizeof(str[0]); ++i) 	{ 		l = strtol(str[i], &end, 10); 		printf("%ld %c(%d)\n", l, *end, *end); 	}  	getchar(); 	return 0; }

user: user-2
content: 参考下面：#include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() {     p=s;     while (1) {         k=sscanf(p,"%d%n",&v,&n);         printf("k,v,n=%d,%d,%d\n",k,v,n);         if (1==k) {             p+=n;         } else if (0==k) {             printf("skip char[%c]\n",p[0]);             p++;         } else {//EOF==k             break;         }     }     printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End.

user: user-2
content: 用sscanf

user: user-0
content: 引用 1 楼 zxhuser-4 的回复:不返回0那返回啥呢？ 我就草了，，我规定，返回值为int，-1表示转换失败，0表示成功，在参数列表中加入一个int型的指针，将返回结果写入！这样还有上面的类似问题吗?

user: user-4
content: 不返回0那返回啥呢？