怎样判断一个字符串是否为可以转换为数字

lidengdeng 2011-07-06 05:42:05
老问题了, 怎样判断一个字符串是否为可以转换为数字..

例如: -2003.11 +2003.11 2.444e-18 等等都是转以转化为数字的字符串...

我想调用 atof( pszTerm )看是否出现异常, 如果异常则为非数字

但这个catch exception不知道如何写..

另外, 有其他方法么???
据说正则表达式也可以实现, C++是否支持?? 还是要安装插件的

谢谢
...全文
238 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-07-06
  • 打赏
  • 举报
回复
个人认为这个问题只能由人来解决:(以下每行都能转换为数字)
一百二十三
壹佰贰拾叁
三加一
L在英文字母表中的顺序
ぉ在日文字母表中的顺序
今天是今年的第几天
这个帖子在几楼
……

(^_^)
bsr2009 2011-07-06
  • 打赏
  • 举报
回复
呵呵,正则表达式是个好办法。
至善者善之敌 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tan625747 的回复:]
正则表达式也可以实现, C++是否支持?

boost
C/C++ code

#include <boost/regex.hpp>
using namespace boost;


boost::cmatch what;
regex expression("[\d]*",regex::icase | regex::perl);
if(regex_search((char*……
[/Quote]


++1 ,就用boost库
某某9 2011-07-06
  • 打赏
  • 举报
回复

[-+]?(\d*)\.?\d*([eE][-+]?\d+)?

看这个咋样?
luciferisnotsatan 2011-07-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hnuqinhuan 的回复:]


atof看看有没有异常
[/Quote]
atof好像不抛异常吧。是设置errno

Each function returns the double value produced by interpreting the input characters as a number. The return value is 0.0 for if the input cannot be converted to a value of that type.

In Visual C++ 2005, in all out-of-range cases, errno is set to ERANGE. If the parameter passed in is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return 0.
tan625747 2011-07-06
  • 打赏
  • 举报
回复
正则表达式也可以实现, C++是否支持?

boost

#include <boost/regex.hpp>
using namespace boost;


boost::cmatch what;
regex expression("[\d]*",regex::icase | regex::perl);
if(regex_search((char*)m_sHtml.c_str(), what, expression))
{
//数字
}
else
{
//非数字cout<<"Error Input getBody"<<endl;
}
無_1024 2011-07-06
  • 打赏
  • 举报
回复

atof看看有没有异常
jixingzhong 2011-07-06
  • 打赏
  • 举报
回复
atof不会抛出异常,根据转换的结果来分支处理吧。

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧