为什么代码不起作用? [英] Why the code doesn't work ?

查看:74
本文介绍了为什么代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



bool isBlankLine(char * line){


char * tmp = line;


bool blank = true;


while(* tmp ++!=''\ 0''){


if((* tmp!= '''')||(* tmp!=''\ n'')){


blank = false;


休息;


}


}


返回空白;

-

通过 http://dbforums.com 发布/>

推荐答案

dreamcatcher写道:

[一堆格式化的代码可以引起恶心。]


请尝试以可以阅读的形式发布。这是你的代码


bool isBlankLine(char * line)

{

char * tmp = line;

bool blank = true;

while(* tmp ++!=''\ 0''){

if((* tmp!='''' ')||(* tmp!=''\ n'')){

blank = false;

break;

}

}

返回空白;


请注意,我们这些幸运的人有< stdbool.h>必须

#include它甚至有机会尝试你的代码,而那些具有

C89(C90)编译器,绝大多数,需要弥补

bool,false和true的定义。尝试发布我们有机会甚至测试的代码。


然后请注意,没有关闭右支撑。这个功能不是
结束。现在,我不知道你认为什么是空白。如果你的意思是一个只有空格的b / b $ b,这是一个更好的方法。请注意,该函数的

名称已更改。尽量避免使用标识符

以''是'',''',''str''或''E'开头。

可以学习它们的规则,但是避免使用它们会更好。我使用的名字来自

LISP成语,其中''p'代表''属性''。


#include< ctype .h>


int blanklinep(char * s)

{

如果(!s)返回0; / * null指针不能指向

空白字符串* /

for(; * s&& isspace(* s); s ++)

/ *没有* /;

/ *上面的循环在* s为0或不是空格时结束,

为''空白''行,* s总是0 * /

返回!(* s);

}


-

Martin Ambuhl

dreamcatcher wrote:
[A bunch of code formatted to induce nausea.]

Please try to post in a form that can be read. Here is your code

bool isBlankLine(char *line)
{
char *tmp = line;
bool blank = true;
while (*tmp++ != ''\0'') {
if ((*tmp != '' '') || (*tmp != ''\n'')) {
blank = false;
break;
}
}
return blank;

Notice that those of us lucky enough to have <stdbool.h> would have to
#include it before even having a chance to try your code, and those with
C89(C90) compilers, which are the vast majority, would need to make up
definitions for bool, false, and true. Try to post code that we have a
chance in hell of even testing.

Then note that there is no closing right brace. This function does not
end. Now, I don''t know what you consider a blank line. If you mean one
that has nothing but whitespace, here is a better approach. Note that the
name of the function is changed. Try very hard to avoid indentifiers
beginning with ''is'', ''to'', ''str'', or ''E''. You could learn the rules for
when they are OK, but avoiding them is better. The name I use is from the
LISP idiom, where the ''p'' stands for ''property''.

#include <ctype.h>

int blanklinep(char *s)
{
if (!s) return 0; /* null pointers can''t point to
blank strings */
for(; *s && isspace(*s); s++)
/* nothing */ ;
/* The loop above ends when either *s is 0 or not whitespace,
for a ''blank'' line, *s will always be 0 */
return !(*s);
}

--
Martin Ambuhl


On Sun,2003年9月14日00:26:01 -0400,dreamcatcher

<我********* @ dbforums.com>写道:
On Sun, 14 Sep 2003 00:26:01 -0400, dreamcatcher
<me*********@dbforums.com> wrote:

bool isBlankLine(char * line){
char * tmp = line;
bool blank = true;

while(* tmp ++!=''\'''){


这会增加tmp,所以从

访问* tmp时如果下面的语句在

之后访问你想要的字符。

if((* tmp!='''')||(* tmp!='' \ n'')){


这永远不会检查字符串的第一个字符;

更糟糕的是,它确实检查了nul终止字符

所以这将总是产生一个非空字符串。


另外||是逻辑OR运算符。你想要&&,

逻辑AND运算符。

blank = false;
break;
}
}
返回空白;

bool isBlankLine(char *line) {
char *tmp=line;
bool blank=true;

while(*tmp++!=''\0'') {
This increments tmp, so when *tmp is accessed from the
if statement below it accesses the character after the
one you want.
if((*tmp!='' '') || (*tmp!=''\n'')) {
This never checks the first character of the string;
Worse, it does check the nul termination character
so this will always produce a non-blank string.

Also || is the logical OR operator. You want &&, the
logical AND operator.
blank=false;
break;
}
}
return blank;




最后,您可以更轻松地使用来自< ctype.h>的空格()


尼克。



Finally, you may it easier to use isspace() from <ctype.h>

Nick.


Martin Ambuhl< ma ***** @ earthlink.net>写道:

[...]
Martin Ambuhl <ma*****@earthlink.net> wrote:
[...]
然后请注意,没有右关闭右括号。此功能不会结束。现在,我不知道你认为什么是空白。如果你的意思是只有空格,那么这是一个更好的方法。请注意,该功能的名称已更改。尽量避免使用''是'',''到'','str''或''E'开头的标识符。
可以学习它们的规则,但是避免使用它们会更好。我使用的名字来自
LISP成语,其中''p'代表''属性''。

#include< ctype.h>

int blanklinep(char * s)
{
如果(!s)返回0; / * null指针不能指向
空字符串* /
for(; * s&& isspace(* s); s ++)


由于* s是一个char,你必须在这里强制转换为unsigned char(isspace

取一个值为unsigned char范围的整数):


for(; * s&& isspace((unsigned char)* s); s ++)

/ * nothing * /;
/ *上述循环结束时* s为0或不是空格,对于''空白''行,* s将始终为0 * /
返回!(* s);
}
Then note that there is no closing right brace. This function does not
end. Now, I don''t know what you consider a blank line. If you mean one
that has nothing but whitespace, here is a better approach. Note that the
name of the function is changed. Try very hard to avoid indentifiers
beginning with ''is'', ''to'', ''str'', or ''E''. You could learn the rules for
when they are OK, but avoiding them is better. The name I use is from the
LISP idiom, where the ''p'' stands for ''property''.

#include <ctype.h>

int blanklinep(char *s)
{
if (!s) return 0; /* null pointers can''t point to
blank strings */
for(; *s && isspace(*s); s++)
Since *s is a char, you have to cast to unsigned char here (isspace
takes an integer with a value in the range of unsigned char):

for (; *s && isspace((unsigned char)*s); s++)
/* nothing */ ;
/* The loop above ends when either *s is 0 or not whitespace,
for a ''blank'' line, *s will always be 0 */
return !(*s);
}




- 凯文。



- Kevin.


这篇关于为什么代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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