问题描述
他们在C ++中可用的任何功能可以确定字符串
包含数值。
值cabn为十六进制,整数,浮点数。即1256,即1256。 ,123.566, ,
" 0xffff"
Thnx
尝试strtol()。如果这给出了错误,那么尝试strtod()。如果这也是
给出错误,那么该字符串不包含数值。
blockquote>
如果你想要一个C ++解决方案,那么发布新闻:comp.lang.c ++
你问题的一个答案是为数字写一个正式的语法
,然后用语法写一个解析器。
你可以用C函数手工完成它:
isdigit()
isxdigit()
ispunct()
isalpha()
和一点逻辑。
PS
它比看起来更难以实现。
只是因为某个数字并不意味着你可以在
中读到它。
例如:
1.0e99999
尝试strtol()。如果这给出了错误,那么尝试strtod()。如果这也是
给出错误,那么该字符串不包含数值。
Is their any fuction available in C++ that can determine that a string
contains a numeric value.
The value cabn be in hex, int, float. i.e. "1256" , "123.566" ,
"0xffff"
Thnx
Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.
If you want a C++ solution, then post in news:comp.lang.c++
One answer to your question is to write a formal grammar for numbers
and then use the grammar to write a parser.
You could do it by hand in C by using fuctions:
isdigit()
isxdigit()
ispunct()
isalpha()
and a bit of logic.
P.S.
It''s harder than it looks to get it right.
Just because something is a number doesn''t mean you can read it in
either.
For example:
1.0e99999
Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.
这篇关于如何查找字符串包含数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!