简体   繁体   English

我在C代码中遇到错误:二进制表达式的操作数无效('long long(int)'和'int')我不知道如何修复

[英]I am getting an error in C code: invalid operands to binary expression ('long long (int)' and 'int') and I don't know how to fix

I am getting the above stated error in my C code. 我在C代码中收到了上述错误。 I am also getting a warning about the same thing saying the pointer to integer conversion is incompatible. 我也收到一个警告,说明整数转换的指针是不兼容的。 The objective of the assignment is to print the factorial and two approximating factorials of the integer the user inputs. 赋值的目的是打印用户输入的整数的阶乘和两个近似阶乘。 Could I just get help on how to fix the errors? 我可以获得如何解决错误的帮助吗?

Here's the code: 这是代码:

#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
}

Here's the error: 这是错误:

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
           ^~~~

You're attempting to use the name of a function as a variable, using it in a context where an integer type is expected. 您试图将函数的名称用作变量,在期望整数类型的上下文中使用它。

What you want to do is call the function again with a value of n-1 , multiply the result by n , then return the result of the multiplication. 你想要做的是再次使用值n-1 调用函数,将结果乘以n ,然后返回乘法的结果。

return n * fact(n-1);

There's still one piece missing. 还有一件丢失。 If you just do that, there's nothing to stop the recursive function calls. 如果你这样做,没有什么可以阻止递归函数调用。 The function will keep calling itself until you run out of stack space. 该函数将一直调用自己,直到你的堆栈空间不足为止。

What's needed is a base case which stops the recursion. 需要的是一个停止递归的基本情况

if (n <= 1) {
    return 1;
} else {
    return n * fact(n-1);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误,“对二进制 * 的操作数无效(有“long unsigned int”和“int *”)” - Error, 'invalid operands to binary * (have 'long unsigned int' and 'int *')' 如何在 c 中执行此操作:错误:二进制表达式的操作数无效('char [3]' 和 'int') - how to do this in c: error: invalid operands to binary expression ('char [3]' and 'int') 类型&#39;int *&#39;和&#39;long unsigned int&#39;类型的无效操作数到C中的二进制&#39;operator *&#39;错误 - invalid operands of types ‘int*’ and ‘long unsigned int’ to binary ‘operator*’ error in C C 编程错误 二进制 int 和 long long 无符号操作数无效 integer - C Programmıng Error invalid operands to binary int and long long unsigned integer C:无效的二进制操作数&gt;(具有&#39;float&#39;和&#39;float(*)(float **,long int,long int,short int *)&#39;) - C: invalid operands to binary > (have ‘float’ and ‘float (*)(float **, long int, long int, short int *)’) 二进制错误的无效操作数(有&#39;long unsigned int *&#39;和&#39;int&#39;) - Invalid operands to binary errors (have 'long unsigned int *' and 'int') 我在做什么错误错误:二进制 * 的无效操作数(有 &#39;float *&#39; 和 &#39;int&#39;)| - what am i doing wrong error: invalid operands to binary * (have 'float *' and 'int')| 如何解决“对二进制表达式无效的操作数”错误 - How to fix a error “invalid operands to binary expression” 为什么在我的C代码中不能将“ long long int”和“ int”一起使用? - Why can't I use “long long int” with “int” in my C code? C错误:类型为&#39;int *&#39;和&#39;unsigned int&#39;的无效操作数为二进制&#39;operator *&#39;| - C error: invalid operands of types 'int*' and 'unsigned int' to binary 'operator*'|
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM