我在C代码中遇到上述错误。我也收到一个警告,说明整数转换的指针是不兼容的。赋值的目的是打印用户输入的整数的阶乘和两个近似阶乘。我可以获得如何修复错误的帮助吗?
以下是代码:
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
#define e 2.71828
#define TRUE 1
#define FALSE 0
long long fact(int n);
double stirling1(int n);
double stirling2(int n);
/*--------------------------- main function -------------------------------
Purpose: The purpose of this program is to give the user the value of the
factorial of the nonnegative integer the user inputs.
---------------------------------------------------------------------------*/
int main ()
{
char ch = 'y';
int flag, again;
int n;
again = TRUE;
while ( again == TRUE )
{
printf("Please enter a nonnegative integer:"); //ask the user for the input
flag = scanf("%d\n\n", &n);
if ( flag != 1 ) //run this statement if the user inputs a noninteger
{
printf("Input must be an integer.");
continue;
}
else if ( n < 0 ) //run this statement if the user inputs a negative integer
{
printf("The factorial is undefined...");
continue;
}
else if ( n <= 14 ) //run this statement if the user inputs an integer less than or equal to 14
{
printf("Number Factorial Aprroximation Aproxximation2\n------------------------------------------------------------------\n"); //prints the header for first table
for ( n = 0; n <= 14; n++ )
{
++n;
printf("%d%1411lld%9e%9e\n", n, fact( n ), stirling1( n ), stirling2( n )); //calls functions to input factorials
}
}
else //run this statement if the user inputs a number greater than 14
{
printf("Number Approximation1 Approximation2\n-----------------------------------------------------\n"); //prints the header for second table
for ( n = 0; n > 14; n++ )
{
++n;
printf("%3d%9e%e\n", n, stirling1( n ), stirling2( n )); //calls functions to input approximate factorials
}
}
printf("Do you want to compute another factorial? (y/n):"); //ask user if they want another factorial of a different number
scanf("%c\n", &ch);
if (ch != 'y')
again = FALSE; //if user does not input 'y' then do not compute another factorial
}
printf( "**** Program Terminated ****" ); //ends program
}
long long fact( int n ) //function to find exact factorial
{
fact *= n; //equation for exact factorial
return fact; //return exact factorial to main
}
double stirling1( int n ) //function to find first approximate factorial
{
n = pow( n, n ) * pow( e, -n ) * sqrt( 2 * PI * n); //equation to find first approximate factorial
return n; //return approximate factorial to main
}
double stirling2( int n ) //function to find second approximate factorial
{
n = pow( n, n ) * pow( e, -n ) * sqrt( 2 * PI * n) * ( 1 + ( 1 / (12 * n) ));
//equation to find second approximate factorial
return n; //return approximate factorial to main
}
这是错误:
Lab_Assignment_4_Sarah_H.c:97:7: error: invalid operands to binary
expression ('long long (int)' and 'int')
fact *= n; //equation for exact factorial
~~~~ ^ ~
Lab_Assignment_4_Sarah_H.c:99:9: warning: incompatible pointer to integer
conversion returning 'long long (int)' from a function with result type
'long long' [-Wint-conversion]
return fact; //return exact factorial to main
^~~~
答案 0 :(得分:0)
您尝试将函数名称用作变量,在期望整数类型的上下文中使用它。
你要做的是再次调用函数,值为n-1
,将结果乘以n
,然后返回乘法的结果。
return n * fact(n-1);
还有一件丢失。如果你这样做,没有什么可以阻止递归函数调用。该函数将一直调用自己,直到你的堆栈空间不足为止。
需要的是基本情况,它会停止递归。
if (n <= 1) {
return 1;
} else {
return n * fact(n-1);
}