问题描述
有人可以向我解释一下,它是如何工作的:
Can somebody explain me, how this works:
这将返回到"main"(主)其中"factor == 1" ,为什么"isPrime"返回true?
以及为什么如果我们"* firstFactorPtr"返回false.和"* secondFactorPtr" ?它正在搜索素数和非素数.
没有"int因素".在主要"中,如何返回到主要"?有任何表情吗?
This returns to "main" with "factor == 1 " , why does "isPrime" return true?
And why does it return false if we "*firstFactorPtr" and "*secondFactorPtr" ?It is searching for prime numbers and for non prime numbers.
There is no "int factor" in "main", how can it return to "main" with any expression?
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool Factor( int number, int *firstFactorPtr, int *secondFactorPtr );
int main(int argc, const char * argv[])
{
int n;
for ( n = 5; n <= 20; n++ ) {
bool isPrime;
int factor1, factor2;
isPrime = Factor( n, &factor1, &factor2 );
if ( isPrime )
printf( "the number %d is prime\n", n );
else
printf( "the number %d has %d and %d as factors\n", n, factor1, factor2 );
}
return 0;
}
bool Factor( int number, int *firstFactorPtr, int *secondFactorPtr )
{
if ( number < 1 )
return false;
int factor;
for ( factor = sqrt(number); factor > 1; factor-- ) {
if ( (number % factor) == 0 ) {
break;
}
}
*firstFactorPtr = factor;
*secondFactorPtr = number / factor;
return ( factor == 1 );
}
推荐答案
总结您的Factor
函数:
bool Factor( /* ... */ )
{
int factor;
/* ... */
factor = /* some value */;
/* ... */
return ( factor == 1 );
}
简单的解释是,如果factor
等于1
,则表达式( factor == 1 )
为true,否则为false.从该函数返回该值为true或false的值.对该函数的调用是布尔表达式,就像对返回int
的函数的调用是int
表达式一样.这意味着您可以编写如下内容:
The simple explanation is that the expression ( factor == 1 )
is true if factor
is equal to 1
, false if it's unequal. This value, either true or false, is returned from the function. A call to the function is a Boolean expression, just like a call to a function that returns an int
is an int
expression. Which means that you can write something like this:
/* some computations */
if (factor == 1) {
/* do something */
}
您可以这样写:
if (Factor( /* arguments */ )) {
/* do something */
}
在Factor
函数内部执行的计算.
with the computations performed inside the Factor
function.
布尔值(如"true"和"false")只是值,可以像其他任何值(如42
或1.23
)一样进行操作.
Boolean values like "true" and "false" are just values, and they can be manipulated like any other values like 42
or 1.23
.
但这要复杂得多.
==
是相等运算符.没什么特别的.像其他任何运算符一样(+
用于加法,/
用于除法等),它需要一个或多个某种类型的操作数,并产生某种类型的结果.
==
is the equality operator. There's nothing particularly special about it; like any other operator (+
for addition, /
for division, etc.) it takes one or more operands of some type(s) and yields a result of some type.
+
产生其操作数之和;结果的类型取决于操作数的类型.
+
yields the sum of its operands; the type of the result depends on the type of the operands.
==
产生类型为int
的结果.如果操作数彼此相等,则结果为1
,否则为0
.在这种情况下,操作数factor
和1
具有相同的数字类型int
,因此比较是有效的. ==
在其他标量类型上有相当复杂的规则,但是我们不必在这里担心它们.
==
yields a result of type int
. That result is 1
if the operands are equal to each other, 0
if they're not. In this case, the operands factor
and 1
are of the same numeric type, int
, so the comparison is valid; there are fairly complicated rules for ==
on other scalar types, but we needn't worry about them here.
出于历史原因,结果不是bool
类型的不是,尽管这样做更有意义.直到1999年标准,C才有了Boolean类型.而是int
类型.可以在布尔上下文(if
,while
等)中使用任何标量(整数,浮点数或指针)表达式,如果等于0
,则将其视为false;否则将其视为true.
For historical reasons, the result is not of type bool
, though that would make more sense. C didn't even have a Boolean type until the 1999 standard; rather, it's of type int
. Any scalar (integer, floating-point, or pointer) expression can be used in a Boolean context (if
, while
, etc.), treated as false if it's equal to 0
and true otherwise.
因此,如果在执行return语句时factor
的值恰好是1
,它将返回值1
.
So if the value of factor
happens to be 1
when the return statement is executed, it will return the value 1
.
您的Factor
函数被定义为返回类型为bool
而不是int
的结果.因此,表达式的值从int
(由==
产生的类型)隐式转换为bool
(函数结果的类型).该转换的结果只是1
,但类型为bool
,而不是int
.该值也可以拼写为true
,因为您已经#include
d <stdbool.h>
将true
定义为宏.
Your Factor
function is defined to return a result of type bool
, not int
. So the value of the expression is implicitly converted from int
(the type yielded by ==
) to bool
(the type of the function result). The result of that conversion is simply 1
, but of type bool
rather than int
. That value can also be spelled as true
since you've #include
d <stdbool.h>
, which defines true
as a macro.
(某些其他语言从一开始就具有布尔类型.在这种语言中,表达式( factor == 1 )
会产生布尔值"true",并且该值将由函数返回,而无需进行转换从概念上讲,此行为比C中的行为要简单,但结果实际上是相同的.将<stdbool.h>
与类型_Bool
以及宏false
和true
一起添加到语言中后,这样做的方式可以方便地编写使用新的内置布尔类型 的代码,而不会破坏将int
或其他标量类型用于布尔值和运算的现有代码.)
(Some other languages have had a Boolean type from the beginning. In such a language, the expression ( factor == 1 )
would yield a Boolean value of "true", and that value would be returned by the function, with no conversion necessary. The behavior would be conceptually simpler than what happens in C, but the result would be essentially the same. When <stdbool.h>
was added to the language, along with the type _Bool
and the macros false
and true
, it was done in a way that makes it convenient to write code that uses the new built-in Boolean type without breaking existing code that uses int
or other scalar types for Boolean values and operations.)
这篇关于退货在这里如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!