You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言实现多项式求和时出现invalid operands to binary +错误的求助

Fixing Your Polynomial Sum Code in C

Hey there! Let's work through the errors in your code and get that polynomial sum working correctly. The core error you're seeing (invalid operands to binary+) is just the tip of the iceberg—let's break down all the issues and fix them step by step.

Key Issues in Your Code

  • You're adding function addresses, not their return values: When you write sum=p6+p5+p4+p3+p2+a*x;, p6/p5 etc. are just pointers to your functions, not the result of running them. You need to call each function with the right arguments to get the value to add.
  • Unused/conflicting function parameters: All your pX functions have a first parameter (like int p6 in p6()) that you immediately overwrite—this is redundant and confusing (you're using the same name for the function and a parameter!).
  • Coefficients aren't stored: Your loop for inputting a1-a6 just overwrites the single a variable each time, so you lose all but the last coefficient. You need an array to store all 6 coefficients.
  • Format string mismatches:
    • scanf("%lf", &x) uses %lf (for double) but x is a float—use %f instead.
    • printf("%d", sum) uses %d (for integers) but sum is a double—use %lf to print it correctly.
  • Inconsistent function parameter order: p3() and p2() have a before x, while others have x before a—this will cause wrong calculations when you call them.
  • Non-standard main() declaration: Use int main() instead of just main() to follow C standards.

Corrected Code

Here's the fixed version with all these issues addressed:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Simplified functions: no unused parameter, consistent parameter order (x, a)
float p6(int x, int a){
    return a * pow(x, 6);
}
float p5(int x, int a){
    return a * pow(x, 5);
}
float p4(int x, int a){
    return a * pow(x, 4);
}
float p3(int x, int a){
    return a * pow(x, 3);
}
float p2(int x, int a){
    return a * pow(x, 2);
}

int main (){
    int i;
    int coeffs[6]; // Array to store a1-a6 coefficients
    double sum = 0;
    int x; // Using int since pow takes double, but x is input as integer from your prompts

    printf("Enter the value of x: ");
    scanf("%d", &x); // x is integer based on your original code's input

    printf("Enter coefficients a1 to a6 (separated by spaces): ");
    for(i = 0; i < 6; i++){ // Use 0-based index for the array
        scanf("%d", &coeffs[i]);
    }

    // Call each function with the correct x and corresponding coefficient, then sum
    sum = p6(x, coeffs[0]) + p5(x, coeffs[1]) + p4(x, coeffs[2]) + 
          p3(x, coeffs[3]) + p2(x, coeffs[4]) + coeffs[5] * x;

    printf("The sum of the polynomial is: %lf\n", sum);
    system("pause");
    return 0;
}

Extra Notes

  • If you want x to be a floating-point number, just change int x to float x and update the scanf to %f—the pow() function will handle it fine since it accepts double arguments (float gets promoted automatically).
  • You could even simplify this further by using a loop to calculate each term instead of writing separate p2() to p6() functions—this would eliminate redundant code entirely! For example:
    sum = 0;
    // Cover x^6 down to x^2 terms
    for(i = 0; i < 5; i++){
        sum += coeffs[i] * pow(x, 6 - i);
    }
    // Add the linear term (a6*x)
    sum += coeffs[5] * x;
    
    This way you don't need all those separate pX() functions at all—way less redundancy!

内容的提问来源于stack exchange,提问作者learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:37:40