急!在线等,c语言如何实现:将字符串转换为float型,或double
我查看c函数参考,有函数atof,但运行结果不对,输出-1.00000,程序如下,请问哪边有问题。
做过此方面程序的,能否给出一个简单完整的例子,谢谢。解决后马上给分
main()
{
char *s="1.1";
float f;
f=atof(s);
printf("%f",f);
getch();
}
问题点数:100、回复次数:19Top
1 楼mccxj(老鼠不逛街)回复于 2005-06-03 09:40:04 得分 50
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char *s="1.1";
float f;
f=atof(s);
printf("%f",f);
}
测试通过。。。没问题。Top
2 楼fogg(fogg)回复于 2005-06-03 09:44:13 得分 0
那我的程序有什么问题,如果没有包含相应头文件,会提示的。既然没有错误提示,那说明atof是可用的Top
3 楼ppiy3670()回复于 2005-06-03 09:45:49 得分 0
有两中方法可以实现这个转换,
一就是用atof函数,二就是用sprintf函数。
两个函数都要注意一个问题,就是你所开的字符串的长度必须要足够,否则就可能出现一些不可预知的问题。
比如你例中的s长度为3,所用空间为4字节。那么当你的f = 3.1415926的时候,问题就要来了!Top
4 楼mccxj(老鼠不逛街)回复于 2005-06-03 09:45:57 得分 0
double atof( const char *string );
int atoi( const char *string );
__int64 _atoi64( const char *string );
long atol( const char *string );
Return Value
Each function returns the double, int, __int64 or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.
或许改一下f的类型试试把。。在我的机子上测试是没问题的。。用的是vc6。0Top
5 楼mccxj(老鼠不逛街)回复于 2005-06-03 09:46:45 得分 0
那我的程序有什么问题,如果没有包含相应头文件,会提示的。既然没有错误提示,那说明atof是可用的
你不用头文件也可以用??Top
6 楼fogg(fogg)回复于 2005-06-03 09:46:56 得分 0
补充:
给定一个char *s
如何判断其指向的是一个数值呢还是字符串Top
7 楼mccxj(老鼠不逛街)回复于 2005-06-03 09:49:26 得分 0
给定一个char *s
如何判断其指向的是一个数值呢还是字符串
。。。呵呵。。这就是一个指向字符的拉。。哪来的数字。。。char *s;是什么意思,看不懂?Top
8 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-06-03 09:51:54 得分 10
#include <ctype.h>
/* atof: convert string s to double */
double atof(char s[])
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++) /* skip white space */
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (val = 0.0; isdigit(s[i]); i++)
val = 10.0 * val + (s[i] - '0');
if (s[i] == '.')
i++;
for (power = 1.0; isdigit(s[i]); i++) {
val = 10.0 * val + (s[i] - '0');
power *= 10;
}
return sign * val / power;
}
Top
9 楼fogg(fogg)回复于 2005-06-03 09:52:09 得分 0
是这样
char *s
s="0.12",或者s="abc","2ba","0.0"
我要把s指向的字符串如果是数值("0.12","0.0")则做数值处理,如果是字符串("abc","2ba")则另做处理Top
10 楼foochow(无聊,灌水......)回复于 2005-06-03 09:56:33 得分 10
atof()将字符串转换成浮点数的函数
原形:double atof(const char *s)
功能:把s所指向的字符串转换成double类型。
s格式为:符号 数字.数字 E符号 数字
返回值:字符串的转换值。
头文件:math.h、stdlib.h
Top
11 楼pengcyu(cpp)回复于 2005-06-03 10:55:52 得分 0
可以啊Top
12 楼jingyueid(干宁)回复于 2005-06-03 11:36:57 得分 0
char *s
s="0.12",或者s="abc","2ba","0.0"
用atof可能无法做到,对于abc这样无法识别的符号,atof会将字母位去掉,直接读数字位。
要达到功能,可以先从字符串里进行查找,如果发现存在字母信息,那么就另行处理,否则进行atof操作Top
13 楼barbara2008(亦农)回复于 2005-06-03 12:05:42 得分 0
判断ASCII码值在0-9之间的值得为数字,其余为字符Top
14 楼heskyII(赫斯基)回复于 2005-06-03 12:17:31 得分 10
以下是MSDN给出的参考,这些英文简单,很容易看懂。
double atof( const char *string );
int atoi( const char *string );
long atol( const char *string );
atof <math.h> and <stdlib.h> ANSI, Win 95, Win NT
atoi <stdlib.h> ANSI, Win 95, Win NT
atol <stdlib.h> ANSI, Win 95, Win NT
#include <stdlib.h>
#include <stdio.h>
void main( void )
{
char *s; double x; int i; long l;
s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );
s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );
s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}
Output
atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854
Top
15 楼maxcai(cailin)回复于 2005-06-03 12:43:18 得分 0
楼主:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char *s="1.1";
float f;
f=atof(s);
printf("%f",f);
getch();
}
可以通过
main()
{
char *s="1.1";
float f;
f=atof(s);
printf("%f",f);
getch();
}
不可以的哟
wintc + 2003Top
16 楼qfeng_zhao(鱼儿鱼儿满天飞)回复于 2005-06-03 12:51:28 得分 0
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char *s="1.1";
float f;
f=atof(s);
printf("%f",f);
}
输出1.100000,没问题啊,vc6.0+xp
Top
17 楼useresu(俗人)(灌水是我无言的抗议)回复于 2005-06-03 13:03:56 得分 20
atof只认"2ba1123"其中的数字字符
对于ba不会自动转换,而是舍弃Top
18 楼fogg(fogg)回复于 2005-06-03 13:14:20 得分 0
谢谢各位
结贴Top
19 楼zloves(俺是菜鸟)回复于 2005-06-03 13:31:17 得分 0
markTop




