2016-11-04 10 views
1

私のCコードに上記のエラーが表示されています。整数変換へのポインタが互換性がないと言って同じことについての警告も出ています。代入の目的は、ユーザーが入力する整数の階乗と2つの近似階乗を出力することです。エラーを修正する方法を教えてもらえますか?Cコードでエラーが発生しました:無効なオペランドをバイナリ式( 'long long(int)'と 'int')に変換すると、修正方法がわかりません

ここでは、コードです:

#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 
      ^~~~ 
+2

'fact'は関数の名前で、変数として使用しているようです。再帰的に 'fact'関数を使用しようとしていますか? – AntonH

+1

'fact'は関数であり、変数ではありません。あなたのコードは、次のことをはっきりと示しています: 'long long fact(int n);'関数にintを掛けようとしていて、その新しい値に関数を設定しようとしています。 –

答えて

0

あなたは整数型のコンテキストでそれを使用して、変数として関数の名前を使用しようとしているが、期待されています。あなたが何をしたいか

コールn-1の値と再び関数であり、その後、乗算の結果を返し、nで結果を掛けます。

return n * fact(n-1); 

まだ欠落しています。これを行うだけで、再帰関数呼び出しを停止するものは何もありません。この関数は、スタック領域を使い果たすまで自身を呼び出し続けます。

ベースケースは、再帰を停止します。

if (n <= 1) { 
    return 1; 
} else { 
    return n * fact(n-1); 
} 
関連する問題