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/p5etc. 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
pXfunctions have a first parameter (likeint p6inp6()) 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-a6just overwrites the singleavariable 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(fordouble) butxis afloat—use%finstead.printf("%d", sum)uses%d(for integers) butsumis adouble—use%lfto print it correctly.
- Inconsistent function parameter order:
p3()andp2()haveabeforex, while others havexbeforea—this will cause wrong calculations when you call them. - Non-standard
main()declaration: Useint main()instead of justmain()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
xto be a floating-point number, just changeint xtofloat xand update thescanfto%f—thepow()function will handle it fine since it acceptsdoublearguments (float gets promoted automatically). - You could even simplify this further by using a loop to calculate each term instead of writing separate
p2()top6()functions—this would eliminate redundant code entirely! For example:
This way you don't need all those separatesum = 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;pX()functions at all—way less redundancy!
内容的提问来源于stack exchange,提问作者learner
相关产品推荐
相关产品推荐

