一个突然出现的问题 [英] A popping question

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

问题描述

有人可以帮我理解K& R II练习的一个方面;

(第76页)


给定功能:


双流行(无效){


....如果存在则从堆栈返回双倍


}


这是如何工作的?


/ *增加2双* /


案例''+'':


push(pop()+ pop());


我原以为你不得不这么做类似


double firstpop,secondpop;


firstpop = pop();

secondpop = pop() ;

push(firstpop + secondpop);


即如果没有分配,双打去哪里。它显然在某个地方发生了,它确实有效,只是我看不出来。


提前致谢。

Could someone help me understand one aspect of an exercise in K&R II;
(page 76)

Given function:

double pop( void){

....return a double from a stack if present

}

how does this work?

/* adds 2 doubles*/

case ''+'' :

push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it''s just that I do not see how.

Thanks in advance.

推荐答案

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

这是如何工作的?
how does this work?



....

....


push(pop()+ pop());


我原本以为你必须要做一些像


double firstpop,secondpop;


firstpop = pop();

secondpop = pop();

push(firstpop + secondpop);


即在哪里如果他们没有被分配,他们会去。它显然在某个地方发生了b $ b,它确实有效,只是我不知道如何。
push ( pop() + pop()) ;

I would have thought you would have to do something like

double firstpop, secondpop;

firstpop=pop();
secondpop=pop();
push ( firstpop+secondpop);

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it''s just that I do not see how.



没关系。编译器的工作就是为表达式中使用的
temporaries预留空间。程序员没有必要担心这个问题。

-

程序员有权不了解许多细节您的代码

仍然会做出合理的更改。

--Kernighan和Plauger,_软件工具_

It doesn''t matter. It''s the compiler''s job to reserve space for
temporaries used in expressions. The programmer doesn''t have to
worry about it.
--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_




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


即如果没有分配,双打去哪里。它显然在某个地方发生了b $ b,它确实有效,只是我不知道如何。

ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it''s just that I do not see how.



Ben Pfaff写道:

Ben Pfaff wrote:


没关系。编译器的工作就是为表达式中使用的
temporaries预留空间。程序员不必担心这个问题。
It doesn''t matter. It''s the compiler''s job to reserve space for
temporaries used in expressions. The programmer doesn''t have to
worry about it.



因此,假设pop是公平的在某种意义上,它取而代之的是它的价值,并且任何赋值都是additonal。但是没有必要执行pop?


So is it fair to assume that "pop" is in a sense replaced by it''s
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?


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

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

即如果没有分配,双打去哪里。它显然在某个地方发生了b $ b,它确实有效,只是我不知道如何。
ie where do the doubles go if they are not assigned. It obviously goes
somewhere and it does work, it''s just that I do not see how.



Ben Pfaff写道:


Ben Pfaff wrote:


>这没关系。为表达式中使用的临时表预留空间是编译器的工作。程序员不必担心它。
>It doesn''t matter. It''s the compiler''s job to reserve space for
temporaries used in expressions. The programmer doesn''t have to
worry about it.



因此,假设pop是公平的在某种意义上,它取而代之的是它的价值,并且任何赋值都是additonal。但是没有必要执行pop?



So is it fair to assume that "pop" is in a sense replaced by it''s
value, and any assignment is "additonal" but not necessary to the
execution of "pop"?



您是否知道函数的结果必须立即分配给变量,或者否则会丢失?

这就是我解释你的话的方式。这个想法是错误的。给定一个适当的函数foo

和变量x:

foo();允许在C中允许以下所有

; / *抛弃返回值。 * /

x = foo(); / *指定返回值。 * /

x = foo()+ 1; / *指定返回值加1. * /

x = foo()+ foo(); / *调用foo两次,分配总和。 * /

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n" ,* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr (p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);} return 0; }

Do you have an idea that the result of a function must be
immediately assigned to a variable, or that otherwise it is lost?
That is how I interpret your words. This idea is wrong. All of
the following are allowed in C, given an appropriate function foo
and variable x:
foo(); /* Throws away return value. */
x = foo(); /* Assigns return value. */
x = foo() + 1; /* Assigns return value plus 1. */
x = foo() + foo(); /* Calls foo twice, assigns the sum. */
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


这篇关于一个突然出现的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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