检查数字 [英] checking for numbers
问题描述
检查在命令
行输入的参数是否为数字的最简单方法是什么?
我知道一种方法会将参数视为一个字符
字符串,然后手动检查每个字符以确保它是0
到9,第一个数字不是0。 br />
但是这看起来像是这样的基本功能,我想知道我是不是会错过某些东西。
What''s the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I''m wondering if I''m
missing something.
推荐答案
1月28日下午3:29,H。 < hbe ... @ gmail.comwrote:
On Jan 28, 3:29 pm, "H." <hbe...@gmail.comwrote:
检查命令输入参数的最简单方法是什么
行是一个数字?
我知道一种方法是将参数视为一个字符
字符串,然后手动检查每个字符使确定它是0
到9,第一个数字不是0.
但这看起来像是这样的基本功能,我想知道我是不是'$
缺少一些东西。
What''s the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I''m wondering if I''m
missing something.
您对数字的定义是什么?它必须是整数还是
它可以是十进制数吗?这个数字可以是负数吗?可以是
任意大小是否需要落入特定的
类型的范围内?可以从空格字符开始吗?你到底想要什么?
想要做什么?
你可能会发现strto *函数很有帮助,否则你需要
来澄清你的定义一个数字。
Robert Gamble
What is your definition of a number? Does it have to be an integer or
can it be a decimal number? Can the number be negative? Can it be of
arbitrary size of does it need to fall into the range of a specific
type? Can it start with space characters? What exactly are you
trying to do?
You might find the strto* functions helpful, otherwise you will need
to clarify your definition of a number.
Robert Gamble
" H." < hb **** @ gmail.comwrites:
"H." <hb****@gmail.comwrites:
检查在命令中输入参数的最简单方法是什么?
行是一个数字?
我知道一种方法是将参数视为一个字符
字符串,然后手动检查每个字符确保它是0
到9,第一个数字不是0.
但这看起来像是这样的基本功能,我想知道是否我是b $ b缺少一些东西。
What''s the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I''m wondering if I''m
missing something.
为什么不领先零?您可以尝试strspn:
#include< stdio.h>
#include< string.h>
int main(无效)
{
char s [] =" 3345567899005431234" ;;
char valid [] =" 0123456789" ;;
size_t res = strspn(s,有效);
if(res!= strlen(s))
printf(" ;无效参数:%s \ n",s);
返回0;
}
-
espen
Why not leading zeros? You can try strspn:
#include <stdio.h>
#include <string.h>
int main (void)
{
char s[] = "3345567899005431234";
char valid[] = "0123456789";
size_t res = strspn (s, valid);
if ( res != strlen (s) )
printf("Invalid argument: %s\n", s);
return 0;
}
--
espen
H.写道:
H. wrote:
检查在命令
line输入的参数是否为数字的最简单方法是什么?
我知道一种方法是将参数视为一个字符
字符串,然后手动检查每个字符以确保它为0
到9,第一个数字不是0.
但这看起来像是这样的基本功能,我想知道我是不是
缺少一些东西。
What''s the easiest way to check if an argument entered at the command
line is a number?
I know that one way would be to treat the argument as a character
string, and then manually check each character to make sure it is 0
through 9, with the first digit not being 0.
But this seems like such basic functionality, I''m wondering if I''m
missing something.
最佳投注:strtol(),strtoul()或strtod(),具体取决于
的数字类型。你想接受。请注意,这些将是
接受某些形式的数字。您可能不想要的数字:数字
带有前导空格的字符串,或带有前导
减号的字符串。但除了检查数字(等等),
他们还会检查转换值的范围:一个字符串
连续九个九是 ;数字,"但是你可能不会想要将它视为数字。
另一种可能性是sscanf(),但我觉得strtoxx()
功能更直接。
-
Eric Sosman
es ***** @ acm-dot-org.inva 盖子
这篇关于检查数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!