如果string是一个数字(初学者) [英] if string is a number (beginner)

查看:74
本文介绍了如果string是一个数字(初学者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的小组,

我想检查给定的字符串是否为数字。如果任何值是

输入除了数字而不是错误。以下不是正确的

解决方案。有人可以告诉我上述任务的任何链接吗?请不要给b
$ b不要给出代码。谢谢。


#include< stdio.h>

#include< stdlib.h>

int main(void)< br $>
{

char * got;

got = malloc(sizeof(char));

if(got = = NULL)

{

got = malloc(sizeof(char));

if(got == NULL)

退出(EXIT_FAILURE);

}


scanf("%s",got);

while( (*得到)++!=''\''')

{

if(* got!=(''0'' - ''9' '))

{

printf(" error \ n");

休息;

}

else

printf("%s\ n",got);

}

返回0 ;

}

Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the correct
solution. Can some one tell me any link for the above task? Pls just
don''t give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);
while( (*got)++ != ''\0'')
{
if(*got != (''0'' - ''9''))
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}

推荐答案

fool写道:
fool wrote:

亲爱的小组,

我想检查给定的字符串是否为数字。如果任何值是

输入除了数字而不是错误。以下不是

正确的解决方案。有人可以告诉我上述任务的任何链接吗?

请不要给出代码。谢谢。


#include< stdio.h>

#include< stdlib.h>

int main(void)

{

char * got;

got = malloc(sizeof(char));
Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the
correct solution. Can some one tell me any link for the above task?
Pls just don''t give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));



这只分配一个字符的空间。那个很漂亮

没用。为什么你甚至动态分配内存?

This allocates exactly one character''s worth of space. That''s pretty
useless. Why are you even dynamically allocating memory?


if(got == NULL)

{

got = malloc(sizeof(char));
if(got == NULL)
{
got = malloc(sizeof (char));



如果之前失败了,请再试一次?为什么?

If it failed before, try again? Why?


if(got == NULL)

退出(EXIT_FAILURE);

}


scanf("%s",got);
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);



你分配的内存没有足够的空间存储任何字符串

而不是空字符串。上面使用的scanf()函数无法限制输入的字符数
。如果它比空的
字符串长,则表示未定义的行为结果。

Your allocated memory does not enough room to store any string longer
than the empty string. The scanf() function as used above has no way to
limit the number of characters entered. If it''s longer than the empty
string, then Undefined Behavior results.


while((* got)++!= ''\''')
while( (*got)++ != ''\0'')



您取消引用指针,然后递增结果。我怀疑这是你想要的。

You dereferenced the pointer, then incremented the result. I doubt
that''s what you wanted.


{

if(* got!=( ''0'' - ''9''))
{
if(*got != (''0'' - ''9''))



你觉得减号怎么办?无论如何,它实际上做了什么

是减去两个字符的数值,这将导致-9中的



没关系,因为如果循环执行完全(不是空的
字符串)那么你之前有UB,现在更多。

What do you think that minus sign does? Whatever, what it actually does
is subract the numerical value of the two characters, which will result
in -9.

It doesn''t matter, because if the loop executed at all (not the empty
string) then you had UB before, and more now.


{

printf(" error \ n");

休息;

}

else

printf("%s \ n",got);

}

返回0;

}
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}



您使用的是什么书?

这里是一个简短的片段(评论供您考虑) :


char buffer [80]; / *魔术数字不好,你会怎么解决? * /

int i;


fgets(buffer,sizeof buffer,stdin);

/ *如果不止于此进入? * /


for(i = 0; buffer [i]; i ++)/ *为什么这个而不是while? * /

{

if(buffer [i]<''0''|| buffer [i]''9'')/ *你可以解码这个? * /

{

printf(" error\ nn);;

休息;

}

}

Brian


What book are you using?
Here''s a brief snippet (the comments are for you to think about):

char buffer[80]; /* magic numbers are bad, how would you fix that? */
int i;

fgets(buffer, sizeof buffer, stdin);
/* what if more than that is entered? */

for(i = 0; buffer[i]; i++) /* why this instead of the while? */
{
if (buffer[i] < ''0'' || buffer[i] ''9'') /* can you decode this? */
{
printf("error\n");
break;
}
}
Brian


fool写道:
fool wrote:

char * got;

got = malloc(sizeof(char));

if(got == NULL)

{

got = malloc(sizeof(char));

if(got == NULL)

退出(EXIT_FAILURE);

}
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}



这个分配看起来很奇怪。你没有任何改变就重试了。

期望得到一个结果 - 它不会发生。如果malloc失败

无法恢复,我建议使用xalloc。这不是一个像malloc这样的函数库,而是一个包围它的包装器,试图分配

内存,并在发生故障时向stderr写一条错误消息br />
exit()s。使用它可以帮助你删除那6行错误处理代码

分散程序的真实逻辑。

此外,你只为单个字符分配空间该大小为1,由

定义(即标准表示sizeof(char)== 1)。

This allocation looks strange. You retry without anything changing and
expect that to then yield a result - it won''t happen. If a malloc failure
can''t be recovered from, I''d suggest using xalloc. This is not a library
function like malloc but a wrapper around it that tries to allocate the
memory and, in case of failure, writes an error message to stderr and
exit()s. Using this helps you remove those 6 lines of error-handling code
that distracts from the real logic of the program.
Also, you only allocate space for a single character and that size is 1, by
definition (i.e. the standard says that sizeof (char)==1).


scanf(" %S",GOT);
scanf("%s",got);



坏主意,你正在读一个大小为1的数组中的无界字符串。

嗯,事实上无论如何这都是一个坏主意数组的长度,你不应该使用无界函数参数。你可以告诉scanf目标字符串的长度是多少。另外,还有fgets()(请务必阅读获取()的

警告,一开始看起来更方便!)只有

输入为字符串。

Bad idea, you are reading an unbounded string into an array of size 1.
Well, in fact this is a bad idea regardless of how long the array is, you
should never use unbounded function parameters. You can tell scanf how
long the target string is. Also, there is fgets() (be sure to read the
warnings for gets(), which looks more convenient at first!) which only
does input as a string.


while((* got)++!=''\''')
while( (*got)++ != ''\0'')



这不是你想要的。在具有普通字符的循环上尝试使用

literal并使用调试器逐步执行此操作。

This is not what you want. Try this on a loop with a normal character
literal and step through it with a debugger.


if(* got!=(' '0'' - ''9''))
if(*got != (''0'' - ''9''))



对不起,虽然这看起来很直观,但C不会让你。 0和9

被视为整数,而之间的减号只会产生它们之间的

差异。你需要的是检查角色是否为
至少为零且最多为9。还有一个库函数isdigit(),

但首先很难正确使用,其次它还包括减号和加号IIRC。


最后,在while()表达式中,好像你已经转移到了

下一个字符。我建议在你的循环中将当前字符打印为整数。这样你就能更好地理解发生了什么。


Uli

Sorry, but while this looks intuitive, C doesn''t let you. The ''0'' and ''9''
are treated as integers, and the minus sign in between just yields the
difference between them. What you need is a check that the character is at
least zero and at most nine. There is also a library function isdigit(),
but firstly that is difficult to use correctly and secondly it also
includes the minus and plus signs, IIRC.

Lastly, in the while() expression, it seems like you already moved to the
next character. I suggest printing the current character as integer inside
your loop. That way you will better understand what''s going on.

Uli




傻瓜 < fo ** @ fool.comwrote in message

news:MP ************************ @ news.sunsite .dk ...

"fool" <fo**@fool.comwrote in message
news:MP************************@news.sunsite.dk...

亲爱的小组,

我想检查给定的字符串是否为数字。如果任何值是

输入除了数字而不是错误。以下不是正确的

解决方案。有人可以告诉我上述任务的任何链接吗?请不要给b
$ b不要给出代码。谢谢。


#include< stdio.h>

#include< stdlib.h>

int main(void)< br $>
{

char * got;

got = malloc(sizeof(char));

if(got = = NULL)

{

got = malloc(sizeof(char));

if(got == NULL)

退出(EXIT_FAILURE);

}


scanf("%s",got);

while( (*得到)++!=''\''')

{

if(* got!=(''0'' - ''9' '))

{

printf(" error \ n");

休息;

}

else

printf("%s\ n",got);

}

返回0 ;

}
Dear group,
I want to check if the given string is a number. If any value is
inputed other than a number then error. The following is not the correct
solution. Can some one tell me any link for the above task? Pls just
don''t give the code. Thanks.

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char *got;
got = malloc(sizeof (char));
if(got == NULL)
{
got = malloc(sizeof (char));
if(got == NULL)
exit(EXIT_FAILURE);
}

scanf("%s",got);
while( (*got)++ != ''\0'')
{
if(*got != (''0'' - ''9''))
{
printf("error\n");
break;
}
else
printf("%s\n",got);
}
return 0;
}



strtol()用于整数,或者strtod用于浮点数,是你的

朋友。

不幸的是,对于新手来说,他们会使用一个指针来指示数字的

结尾可以解释为nu mbers。

然而这个想法并不太复杂。如果结束指针指向0,那么

结束字符串字符,你就有了一个有效的数字。如果它是非数字,你可能想要抛出这个数字。如果结束指针位于字符串的开头

,那么字符串的开头就没有数字,所以你必须输出


顺便注意观察空白。

-
www.personal.leeds.ac.uk/~bgy1mm

免费软件游戏下载。

strtol() for integers, or strtod, for floating-point numbers, are your
friend.
Unfortunately for newbies they take a pointer to a pointer to indicate the
end of the digits that can be interpreted as numbers.
However the idea is not too complicated. if the end pointer points to 0, the
end of string character, you have a valid number. If it is a non-digit, you
might want to throw the number out. If the end pointer is at the beginning
of the string, there were no digits at the beginning of the string so you
must throw the input out.
Watch for whitespace, by the way.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


这篇关于如果string是一个数字(初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆