问题描述
该计划是从K&安培; R课本(第71号),这是什么意思
如果(S [I] =(S [I] ==' - ') - 1:1:
什么做以下DO:
-
S [I] ==' - ')
-
- ?1
我重写code这是打字错误道歉
71页上的实际code是:
签署=(S [I] ==' - ')? -1:1;
S [我] == -
产生一个真正的价值,如果 S [I]
为负签收,如有虚假事实并非如此。
的?
是有条件的经营者。它产生的第二个操作数,如果第一个操作数是真实的,它的第三个操作数,如果它是假的。
所以上面套标志
到 1
如果 S [I]
是一个减号,或 +1
,如果不是这样的。
这是一个示例实施的ATOF
函数,将字符串转换为浮点值的一部分。转换字符串的其余部分的数字结果乘以标志
来产生最终值。
在code在你的问题:
如果(S [I] =(S [I] ==' - ') - 1:1:
似乎受到严重乱码。它显示的确切的询问它时,code是非常重要的。书中code不是一个如果
语句,它不会改变的值S [I]
中,括号正确匹配,L它有更多的空白空间,以使其更具可读性,并正确使用 1
,而不是非法 - 1
。
这是可能的code已经在本书的不同版本略有改变(我有第二版,第一次印刷)。请再次检查您的书的副本。
This program is from K&R text book (page number 71), what does it mean?
if(s[i] =(s[i]=='-')?-1:1:
What do the following do:
s[i]=='-')
?--1?
I rewrite code it was typing mistake sorry
The actual code on page 71 is:
sign = (s[i] == '-') ? -1 : 1;
s[i] == '-'
yields a true value if s[i]
is a minus sign, false if it isn't.
The ? :
is the conditional operator. It yields its second operand if the first operand is true, its third operand if it's false.
So the above sets sign
to -1
if s[i]
is a minus sign, or to +1
if it's not.
It's part of a sample implementation of the atof
function, which converts a string to a floating-point value. The numeric result of converting the rest of the string is multiplied by sign
to yield the final value.
The code in your question:
if(s[i] =(s[i]=='-')?--1:1:
appears to be badly garbled. It's important to show the exact code when asking about it. The code in the book is not an if
statement, it does not change the value of s[i]
, the parentheses match correctly,l it has more white space to make it more readable, and it correctly uses -1
rather than the illegal --1
.
It's possible the code has changed slightly in different editions of the book (I have the second edition, first printing). Please check your copy of the book again.
这篇关于如果(S [I] =(S [I] ==' - ') - 1:1:这是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!