C语言多项式分式求导功能异常(switch case4)求助
C语言多项式运算程序case4分支修复方案
核心错误分析
- 多项式存储逻辑不匹配:
input_of_polynomials按单个项存储多项式:coefficient[i]是第i个项的系数,degree[i]是对应项的次数(比如输入2x²+3x,数组存储为coefficient[0]=2, degree[0]=2; coefficient[1]=3, degree[1]=1)。- 但
function_divide错误地将系数数组当成按次数索引的结构(认为coefficient[i]是x^i的系数),导致f(x)和g(x)的计算完全错误。
- 次数参数传递错误:
- 调用
derivative_divide_functions时传入degree_fx[0],这只是第一个项的次数,并非多项式的最高次数,且存储格式错误的前提下该参数毫无意义。
- 调用
- 数值微分精度不足:
- 使用前向差分
(f(x+h)-f(x))/h截断误差较大,中心差分(f(x+h)-f(x-h))/(2h)精度更高,更适合数值导数计算。
- 使用前向差分
- 次要错误:
integration函数参数coefficient声明为float,但调用时传入double数组,存在类型不匹配问题。quadratic_equation中根的计算错误:-(b / 2 * a)应为-(b/(2*a)),运算优先级处理错误。
修复后的完整代码
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #define MAX 100 void display_polynomial(int *terms, double coefficient[], int degree[]); void derivative_polynomial(int *terms, double coefficient[], int degree[]); void integration(int *terms, double coefficient[], int degree[]); void input_of_polynomials(int *terms, double coefficient[], int degree[]); void quadratic_equation(); void derivative_divide(); double function_divide(int terms_fx, double coefficient_fx[], int degree_fx[], int terms_gx, double coefficient_gx[], int degree_gx[], double xd); double derivative_divide_functions(int terms_fx, double coefficient_fx[], int degree_fx[], int terms_gx, double coefficient_gx[], int degree_gx[], double xd); int main(void) { int choice; int terms; int degree[100]; double coefficient[100]; printf("|************************************************************************************************************|\n"); printf("|\t\t Polynomial Operations |\n"); printf("|************************************************************************************************************|\n"); printf("|\t\t Choose an Operation: |\n"); printf("| 1. Calculate Derivative |\n"); printf("| 2. Perform Integration |\n"); printf("| 3. Solve Quadratic Equation |\n"); printf("| 4. Calculate Derivative of Type f(x) / g(x) |\n"); printf("|************************************************************************************************************|\n"); printf(" Your Choice: "); scanf_s("%d", &choice); switch (choice) { case 1: { input_of_polynomials(&terms, coefficient, degree); printf("f(x)= "); display_polynomial(&terms, coefficient, degree); printf("\n\nf`(x)="); derivative_polynomial(&terms, coefficient, degree); printf("\n"); break; } case 2: { input_of_polynomials(&terms, coefficient, degree); printf("f(x)= "); display_polynomial(&terms, coefficient, degree); printf("\n\nIntegral = "); integration(&terms, coefficient, degree); printf("\n"); break; } case 3: { quadratic_equation(); break; } case 4: { printf("Enter Value of X: "); double x; scanf_s("%lf", &x); int term_fx; double coefficient_fx[MAX]; int degree_fx[MAX]; input_of_polynomials(&term_fx, coefficient_fx, degree_fx); int term_gx; double coefficient_gx[MAX]; int degree_gx[MAX]; input_of_polynomials(&term_gx, coefficient_gx, degree_gx); printf("f(x)/g(x) = \n\n"); display_polynomial(&term_fx, coefficient_fx, degree_fx); printf("\n------------\n"); display_polynomial(&term_gx, coefficient_gx, degree_gx); printf("\n"); double derivative_result = derivative_divide_functions(term_fx, coefficient_fx, degree_fx, term_gx, coefficient_gx, degree_gx, x); printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.4lf\n", x, derivative_result); break; } default: { printf("Invalid Input \n"); } break; } } void display_polynomial(int *terms, double coefficient[], int degree[]) { for (int i = 0; i < *terms; i++) { if (coefficient[i] == 0) { continue; } else if (coefficient[i] > 0) { if (i != 0) { printf(" + "); } } else if (coefficient[i] < 0) { printf(" - "); } if (degree[i] == 0) { printf("%.1lf", fabs(coefficient[i])); } else if (degree[i] == 1) { if (fabs(coefficient[i]) != 1) { printf("%.1lf", fabs(coefficient[i])); } printf("x"); } else { printf("%.1lf", fabs(coefficient[i])); printf("x^%d", degree[i]); } } } void derivative_polynomial(int *terms, double coefficient[], int degree[]) { int valid_terms = 0; for (int i = 0; i < *terms; i++) { coefficient[i] = coefficient[i] * degree[i]; degree[i] = degree[i] - 1; if (coefficient[i] != 0) { valid_terms++; } } *terms = valid_terms; display_polynomial(terms, coefficient, degree); } void integration(int *terms, double coefficient[], int degree[]) { for (int i = 0; i < *terms; i++) { degree[i] = degree[i] + 1; coefficient[i] = coefficient[i] / degree[i]; } display_polynomial(terms, coefficient, degree); printf(" + C \n"); } void input_of_polynomials(int *terms, double coefficient[], int degree[]) { printf("Enter Number of Terms in the polynomial: "); scanf_s("%d", terms); for (int i = 0; i < *terms; i++) { printf("Enter Co-Efficient of Term %d: ", i + 1); scanf_s("%lf", &coefficient[i]); printf("Enter Degree of Term %d: ", i + 1); scanf_s("%d", °ree[i]); printf("\n"); } } void quadratic_equation() { double root1, root2; double a, b, c; printf(" QUADRATIC ROOTS CALCULATOR \n\n"); printf("Enter the Co-Efficient of x^2 (a) \n"); scanf_s("%lf", &a); printf("Enter the Co-Efficient of x (b) \n"); scanf_s("%lf", &b); printf("Enter the Constant (c) \n"); scanf_s("%lf", &c); double discriminant = b*b - 4*a*c; if (discriminant >= 0) { double sqrt_d = sqrt(discriminant); if (discriminant == 0) { printf("\t\t Roots Are Real And Equal\n"); root1 = root2 = -b/(2*a); printf("Roots of the Equation\t ( %.1lfx^2 + %.1lfx + %.1lf ) Are :\n\t Root 1 : %.1lf \n\t Root 2 : %.1lf\n", a, b, c, root1, root2); } else { root1 = (-b + sqrt_d)/(2*a); root2 = (-b - sqrt_d)/(2*a); printf("Roots Are Real and Distinct \n"); printf(" Roots of the Equation %.1lfx^2 + %.1lfx + %.1lf Are :\n\t Root 1 : %.1lf \tRoot2 : %.1lf\n", a, b, c, root1, root2); } } else { printf("Roots Are Imaginary\n"); } } void derivative_divide() { printf("Function of Type f(x) / g(x)\n"); int term_fx; double coefficient_fx[MAX]; int degree_fx[MAX]; input_of_polynomials(&term_fx, coefficient_fx, degree_fx); int term_gx; double coefficient_gx[MAX]; int degree_gx[MAX]; input_of_polynomials(&term_gx, coefficient_gx, degree_gx); printf("Function is:\n"); display_polynomial(&term_fx, coefficient_fx, degree_fx); printf("\n-----\n"); display_polynomial(&term_gx, coefficient_gx, degree_gx); double x_value; printf("Enter the value of x for the derivative: "); scanf_s("%lf", &x_value); double derivative_result = derivative_divide_functions(term_fx, coefficient_fx, degree_fx, term_gx, coefficient_gx, degree_gx, x_value); printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.4lf\n", x_value, derivative_result); } double function_divide(int terms_fx, double coefficient_fx[], int degree_fx[], int terms_gx, double coefficient_gx[], int degree_gx[], double xd) { double result_fx = 0.0, result_gx = 0.0; for (int i = 0; i < terms_fx; i++) { result_fx += coefficient_fx[i] * pow(xd, degree_fx[i]); } for (int i = 0; i < terms_gx; i++) { result_gx += coefficient_gx[i] * pow(xd, degree_gx[i]); } if (fabs(result_gx) < 1e-9) { printf("Error: g(x) is zero at x = %.2lf\n", xd); exit(EXIT_FAILURE); } return result_fx / result_gx; } double derivative_divide_functions(int terms_fx, double coefficient_fx[], int degree_fx[], int terms_gx, double coefficient_gx[], int degree_gx[], double xd) { double const h = 1.0e-6; double fx_plus_h = function_divide(terms_fx, coefficient_fx, degree_fx, terms_gx, coefficient_gx, degree_gx, xd + h); double fx_minus_h = function_divide(terms_fx, coefficient_fx, degree_fx, terms_gx, coefficient_gx, degree_gx, xd - h); double derivative = (fx_plus_h - fx_minus_h) / (2 * h); return derivative; }
关键修复点说明
- 统一多项式存储逻辑:
修改function_divide和derivative_divide_functions的参数,传入项数、系数数组、次数数组,遍历每个项计算多项式值,匹配input_of_polynomials的存储格式。 - 修正参数传递逻辑:
不再传递单个次数值,直接传入完整的次数数组,确保函数能遍历所有项完成计算。 - 提升数值微分精度:
将前向差分改为中心差分,减少截断误差,导数结果更准确。 - 修复类型与运算错误:
把integration的float参数改为double,避免类型转换问题;修正二次方程根的计算优先级,确保结果正确。 - 增加异常处理:
在function_divide中判断g(x)是否接近0,避免程序因除以0崩溃。
内容的提问来源于stack exchange,提问作者RobbaN
相关产品推荐
相关产品推荐

