新问题:但是为什么来自 firstFactorPtr == factor1 的表达式来自 n == number?
有人可以解释一下,这是如何工作的:
返回(因子 == 1);
这返回到“main”,“factor == 1”,为什么“isPrime”返回true?
如果我们 "*firstFactorPtr" 和 "*secondFactorPtr" 为什么它返回 false ?它正在搜索素数和非素数。
“main”中没有“int factor”,怎么能用任何表达式返回“main”呢?
#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 );
}