练习7-1 [英] Exercise 7-1

查看:48
本文介绍了练习7-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这与C有什么关系,但是这里有。

这个练习是写一个简单的程序upper。这个名称由

调用。程序本身很简单......即使对我来说也是如此。 :-)

I am not sure how relevant to C this is, but here goes.
The exercise is to write a simple program "upper" that is invoked by
this name. The program itself is pretty easy...even for me. :-)


>>>>>
>>>>>



#include< stdio.h>

#include< ctype.h>

#include< string.h>

int main(int argc,const char * argv []){

int c ;

if(strcmp(argv [0]," / Users / m / Desktop / upper / build / Release /

upper")== 0)

while((c = getchar())!= EOF)

putchar(toupper(c));

else

putchar(tolower(c));

返回0;

}


<<<< <


但是,我的问题是这个吗?

当我输入" / ..... upper"紧接着是一个论点,

程序不起作用。

如果我输入/ ... upper返回,然后输入参数,它的工作原理。

任何原因?我已经在Xcode论坛上问了,但还没回答?

如果这是OT,那么我道歉。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (int argc, const char * argv[]) {
int c;
if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/
upper")==0)
while ( (c = getchar() ) != EOF)
putchar(toupper(c));
else
putchar(tolower(c));
return 0;
}

<<<<<<

But, my question is this?
When I type "/.....upper" immediately followed by an argument, the
program does not work.
If I type "/...upper" RETURN, then enter the arguments, it works.
Any reason? I have asked on Xcode forum, but no answer yet?
If this is OT, then I apologize.

推荐答案

mdh< md ** @ comcast.netwrites:
mdh <md**@comcast.netwrites:

我不确定这与C有什么关系,但是这里有。

练习是写一个简单的程序鞋面。这个名称由

调用。程序本身很简单......即使对我来说也是如此。 :-)
I am not sure how relevant to C this is, but here goes.
The exercise is to write a simple program "upper" that is invoked by
this name. The program itself is pretty easy...even for me. :-)

>>>>>>
>>>>>>



#include< stdio.h>

#include< ctype.h>

#include< string.h>

int main(int argc,const char * argv []){

int c;

if(strcmp(argv [0]," / Users / m / Desktop / upper / build / Release /

upper")== 0)

while((c = getchar())!= EOF)

putchar(toupper(c));

else

putchar( tolower(c));

返回0;

}


<<<<<<<


但是,我的问题是这个吗?

当我输入/ ..... upper时紧接着是一个论点,

程序不起作用。

如果我输入/ ... upper返回,然后输入参数,它的工作原理。

任何原因?我在Xcode论坛上问过,但还没回答?

如果这是OT,那么我道歉。


#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (int argc, const char * argv[]) {
int c;
if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/
upper")==0)
while ( (c = getchar() ) != EOF)
putchar(toupper(c));
else
putchar(tolower(c));
return 0;
}

<<<<<<

But, my question is this?
When I type "/.....upper" immediately followed by an argument, the
program does not work.
If I type "/...upper" RETURN, then enter the arguments, it works.
Any reason? I have asked on Xcode forum, but no answer yet?
If this is OT, then I apologize.



如果argv [0]没有

特定值是奇数,那么让程序默默无效,但如果是'这是你想要它做的,那是'

ok。


你的缩进是不一致的,也不对应于

代码的实际结构或

代码的预期结构。


这是'之后的程序''已经通过indent -kr过滤了(和我手动加入分割线后的
):


#include< stdio.h>

#include< ; ctype.h>

#include< string.h>


int main(int argc,const char * argv [])

{

int c;

if(strcmp(argv [0]," / Users / m / Desktop / upper / build / Release / upper" )== 0)

while((c = getchar())!= EOF)

putchar(toupper(c));

否则

putchar(tolower(c));

返回0;

}


这个应该足以告诉你问题是什么。对于所有结构化语句使用大括号,这也是一个很好的论据,无论是否需要

。例如,我总是写:


如果(条件){

声明;

}

而不是


如果(条件)

声明;


它使这种错误稍微困难一点。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做一些东西。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Having the program silently do nothing if argv[0] doesn''t have a
specific value is odd, but if that''s what you want it to do, that''s
ok.

Your indentation is inconsistent and doesn''t correspond either to the
actual structure of your code or to the intended structure of your
code.

Here''s your program after it''s been filtered through "indent -kr" (and
after I manually joined the split line):

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char *argv[])
{
int c;
if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/upper") == 0)
while ((c = getchar()) != EOF)
putchar(toupper(c));
else
putchar(tolower(c));
return 0;
}

This should be enough to tell you what the problem is. It''s also a
good argument for using braces for all structured statements, whether
they''re required or not. For example, I always write:

if (condition) {
statement;
}

rather than

if (condition)
statement;

It makes this kind of error slightly more difficult to make.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


9月2日,8日:53 * pm,Keith Thompson< ks ... @ mib.orgwrote:
On Sep 2, 8:53*pm, Keith Thompson <ks...@mib.orgwrote:

mdh< m ... @ comcast.netwrites:
mdh <m...@comcast.netwrites:

我不确定这与C有什么关系,但是这里有。


I am not sure how relevant to C this is, but here goes.
.



您的缩进是不一致的,并且与您的代码的实际结构或您的<的预期结构不对应

br />
代码。


Your indentation is inconsistent and doesn''t correspond either to the
actual structure of your code or to the intended structure of your
code.



对格式化很抱歉。我必须在某个时候弄明白。

Sorry about the formatting.I will have to figure that out sometime.


>

这是'你的程序经过'indent -kr'过滤后的程序(和我手动加入分割线后的
):


#include< stdio.h>

#include< ; ctype.h>

#include< string.h>


int main(int argc,const char * argv [])

{

* * int c;

* * if(strcmp(argv [0]," / Users / m / Desktop / upper / build /释放/上限)== 0)

* * * * while((c = getchar())!= EOF)

* * * * * * putchar( toupper(c));

* *其他

* * * * putchar(tolower(c));

* *返回0;


}


这应该足以告诉你问题所在。
>
Here''s your program after it''s been filtered through "indent -kr" (and
after I manually joined the split line):

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char *argv[])
{
* * int c;
* * if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/upper")== 0)
* * * * while ((c = getchar()) != EOF)
* * * * * * putchar(toupper(c));
* * else
* * * * putchar(tolower(c));
* * return 0;

}

This should be enough to tell you what the problem is.



解决方案,除了省略错误的第二个while((c

= getchar())!= EOF )"来自Tondo和Gimpel。我认为这很简单,可以说明K& R正在尝试制作的问题,而不是

使它成为防弹?

The solution, except for the omission in error of a second "while ((c
= getchar()) != EOF)" is from Tondo and Gimpel. I think it is really
simple to illustrate the issue that K&R are trying to make, instead of
making it bullet proof?


>>>>>
>>>>>



while((c = getchar())!= EOF){

putchar(toupper( c));

}

else {

while((c = getchar())!= EOF)

putchar(tolower(c)); }


<<<<<


但是,我错过了什么。这是一个需要2步

调用此程序的原因吗?

while ((c = getchar()) != EOF){
putchar(toupper(c));
}
else {
while ((c = getchar()) != EOF)
putchar(tolower(c)); }

<<<<<

But, have I missed something. Is that the reason one needs a 2 step
invocation of this program?


9月3日上午6:34, mdh< m ... @ comcast.netwrote:
On Sep 3, 6:34 am, mdh <m...@comcast.netwrote:

我不确定这与C有什么关系,但是这里有。
I am not sure how relevant to C this is, but here goes.



这是C书中的C练习,它的相关性如何?

It''s a C exercise from a C book, how more relevant can it be?


练习是写一个简单的程序鞋面。这个名称由

调用。程序本身很简单......即使对我来说也是如此。 :-)
The exercise is to write a simple program "upper" that is invoked by
this name. The program itself is pretty easy...even for me. :-)



你似乎遇到了问题......

You seem to be having problems with it though...


#include< ; stdio.h>

#include< ctype.h>

#include< string.h>


int main(int argc,const char * argv []){
#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (int argc, const char * argv[]) {



argv是char * argv []或char ** argv等。

掉落'const''。

argv is char *argv[] or char **argv, etc.
Drop `const''.


int c;

if(strcmp(argv [0]," / Users / m / desktop / upper / build / Release /

upper")== 0)
int c;
if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/
upper")==0)



argv [argc]始终为NULL。 argc可能是0.

argv[argc] is always NULL. argc may be 0.


while((c = getchar())!= EOF)

putchar(toupper(c));

其他

putchar(tolower(c));
while ( (c = getchar() ) != EOF)
putchar(toupper(c));
else
putchar(tolower(c));



如果strcmp返回非零值,则达到'else''

`c''未初始化,因此你是当

你使用它的值时调用未定义的行为。

`else'' is reached if strcmp returns a non-zero value.
`c'' is not initialized, thus you are invoking undefined behavior when
you use its value.


返回0;


}


<<<<<<


但是,我的问题是这个吗?

当我输入/ ..... upper时紧接着是一个论点,

程序不起作用。
return 0;

}

<<<<<<

But, my question is this?
When I type "/.....upper" immediately followed by an argument, the
program does not work.



因为您的代码中有多个错误。

Because there are several errors in your code.


如果我输入" / ...上部"返回,然后输入参数,它的工作原理。

任何原因?我在Xcode论坛上问过,但还没回答?

如果这是OT,那么我道歉。
If I type "/...upper" RETURN, then enter the arguments, it works.
Any reason? I have asked on Xcode forum, but no answer yet?
If this is OT, then I apologize.



因为你从stdin读取,你不会对参数做任何事情。

这是一个完整的程序,做什么你想要的。


#include< stdio.h>

#include< stdlib.h>

#include< ; string.h>

#include< ctype.h>


int main(void){

int(* f [2])(int)= {toupper,tolower},c,i;


如果(argc == 0)返回EXIT_FAILURE;

i = !! strcmp(argv [0]," upper");

while((c = getchar())!= EOF)putchar(f [i](c));

if(ferror(stdin))返回EXIT_FAILURE;

返回0;

}

Because you read from stdin, you don''t do anything with the arguments.
Here''s a complete program that does what you want.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void) {
int (*f[2])(int) = { toupper, tolower }, c, i;

if(argc == 0) return EXIT_FAILURE;
i = !!strcmp(argv[0], "upper");
while((c = getchar()) != EOF) putchar(f[i](c));
if(ferror(stdin)) return EXIT_FAILURE;
return 0;
}


这篇关于练习7-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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