在我的代码中,switch的情况4不起作用,它需要一个函数作为f/g,然后对其进行数值推导,
CASE 4 接受用户的输入,然后应用我使用过的导数函数,但它没有按预期工作,我认为错误在于指针的使用。有人可以修复吗?
我期待导数,但它没有导数。
这是代码
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include <math.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(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);
double derivative_divide_functions(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd);
int main(void)
{
double x;
double degree_;
int choice;
int terms;
int degree[100];
double coefficient[100];
int term_fx, term_gx;
double coefficient_fx1[MAX], coefficient_gx1[MAX];
int degree_fx1[MAX], degree_gx2[MAX];
double xd;
double coefficient_fx[MAX + 1], coefficient_gx[MAX + 1];
int degree_fx, degree_gx;
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);
// Input for the first polynomial (f(x))
int term_fx;
double coefficient_fx[MAX];
int degree_fx[MAX];
input_of_polynomials(&term_fx, coefficient_fx, degree_fx);
// Input for the second polynomial (g(x))
int term_gx;
double coefficient_gx[MAX];
int degree_gx[MAX];
input_of_polynomials(&term_gx, coefficient_gx, degree_gx);
// Display the input polynomials
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");
// Calculate the derivative using derivative_divide_functions
double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x);
printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\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[])
{
for (int i = 0;i < *terms;i++)
{
*(coefficient + i) = *(coefficient + i) * *(degree + i);
*(degree + i) = *(degree + i) - 1;
}
display_polynomial(terms, coefficient, degree);
}
void integration(int* terms, float 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 Constanct (c) \n");
scanf_s("%lf", &c);
double d = sqrt((b * b - (4 * a * c)));
if (d == 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", a, b, c, root1, root2);
}
else if (d > 0)
{
root1 = (-b + d) / (2 * a);
root2 = (-b - 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", 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);
// Calculate and display the derivative
double x_value;
printf("Enter the value of x for the derivative: ");
scanf_s("%lf", &x_value);
double derivative_result = derivative_divide_functions(coefficient_fx, degree_fx[0], coefficient_gx, degree_gx[0], x_value);
printf("Derivative of (f(x) / g(x)) at x = %.2lf is %.2lf\n", x_value, derivative_result);
}
double function_divide(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
double result_fx = 0.0, result_gx = 0.0;
// Calculate f(x)
for (int i = degree_fx; i >= 0; i--) {
result_fx += coefficient_fx[i] * pow(xd, i);
}
// Calculate g(x)
for (int i = degree_gx; i >= 0; i--) {
result_gx += coefficient_gx[i] * pow(xd, i);
}
double result_divide_function = result_fx / result_gx;
return result_divide_function;
}
double derivative_divide_functions(double coefficient_fx[], int degree_fx, double coefficient_gx[], int degree_gx, double xd) {
double const h = 1.0e-6;
// Calculate f(x + h)
double fx_plus_h = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd + h);
// Calculate f(x)
double fx = function_divide(coefficient_fx, degree_fx, coefficient_gx, degree_gx, xd);
// Calculate the derivative
double derivative = (fx_plus_h - fx) / h;
return derivative;
}
您已对其进行编程以对其进行数字区分。
您应该实施商规则来获得实际的导数。