C 言語で多項式式を評価する

CBeginner
オンラインで実践に進む

はじめに

この実験では、C プログラミングにおいて多項式式を評価する方法を学びます。この実験では、以下の手順が行われます。ユーザー入力から多項式の係数と変数 x を読み取り、ホーナー法を使って多項式式を効率的に評価します。この実験が終了するまでに、C 言語における代数的な式の扱いと、効果的な多項式評価アルゴリズムの実装について、より深い理解を得ることができます。

係数と変数 x を読み取る

このステップでは、C プログラムにおいてユーザー入力から多項式の係数と変数 x を読み取る方法を学びます。これは、多項式評価アルゴリズムを実装する最初の部分です。

まず、多項式評価プログラム用の新しい C ファイルを作成します。

cd ~/project
nano polynomial_eval.c

次に、係数と変数 x を読み取るための次のコードを追加します。

#include <stdio.h>

#define MAX_DEGREE 10

int main() {
    int degree;
    double coefficients[MAX_DEGREE + 1];
    double x;

    // 多項式の次数を読み取る
    printf("Enter the degree of the polynomial (0-10): ");
    scanf("%d", &degree);

    // 係数を読み取る
    printf("Enter coefficients from highest degree to constant term:\n");
    for (int i = degree; i >= 0; i--) {
        printf("Coefficient for x^%d: ", i);
        scanf("%lf", &coefficients[i]);
    }

    // x の値を読み取る
    printf("Enter the value of x: ");
    scanf("%lf", &x);

    return 0;
}

出力例:

Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2

コードを分解してみましょう。

  • バッファオーバーフローを防ぐために、多項式の最大次数を定義します
  • degree は多項式の次数を格納します
  • coefficients 配列は、最高次数から定数項までの係数を格納します
  • x は多項式を評価する値を格納します
  • 次数、係数、および x の値に対するユーザー入力を読み取るために scanf() を使用します

コンパイルしてプログラムを実行して入力をテストします。

gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval

評価にホーナー法を使用する

このステップでは、多項式式を効率的に評価するためにホーナー法を実装します。ホーナー法は、多項式の値を計算するために必要な乗算の数を削減します。

前の C ファイルを開き、ホーナー法の評価を含むように修正します。

cd ~/project
nano polynomial_eval.c

ホーナー法の実装でコードを更新します。

#include <stdio.h>

#define MAX_DEGREE 10

double hornerMethod(int degree, double coefficients[], double x) {
    double result = coefficients[degree];

    for (int i = degree - 1; i >= 0; i--) {
        result = result * x + coefficients[i];
    }

    return result;
}

int main() {
    int degree;
    double coefficients[MAX_DEGREE + 1];
    double x, result;

    // 以前の入力コードは同じまま
    printf("Enter the degree of the polynomial (0-10): ");
    scanf("%d", &degree);

    printf("Enter coefficients from highest degree to constant term:\n");
    for (int i = degree; i >= 0; i--) {
        printf("Coefficient for x^%d: ", i);
        scanf("%lf", &coefficients[i]);
    }

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    // ホーナー法を使って多項式を評価する
    result = hornerMethod(degree, coefficients, x);

    printf("Polynomial value at x = %.2f is: %.2f\n", x, result);

    return 0;
}

更新されたプログラムをコンパイルして実行します。

gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval

出力例:

Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2
Polynomial value at x = 2.00 is: 11.00

ホーナー法を分解してみましょう。

  • 関数 hornerMethod() は、次数、係数、および x をパラメータとして受け取ります
  • 最高次数の係数から始めます
  • 反復的に現在の結果に x を掛け、次の係数を加えます
  • 計算量を O(n²) から O(n) に削減します

この方法は、x = 2 のときの 2x³ - 3x² + 0x + 5 のような多項式を効率的に評価します。

結果を表示する

この最後のステップでは、フォーマットされた出力とエラーハンドリングを追加することで、多項式評価プログラムを強化し、ユーザー体験を向上させます。

前の C ファイルを開き、改良された結果の表示で更新します。

cd ~/project
nano polynomial_eval.c

フォーマットされた結果の表示と入力検証でコードを更新します。

#include <stdio.h>

#define MAX_DEGREE 10

double hornerMethod(int degree, double coefficients[], double x) {
    double result = coefficients[degree];

    for (int i = degree - 1; i >= 0; i--) {
        result = result * x + coefficients[i];
    }

    return result;
}

void printPolynomial(int degree, double coefficients[]) {
    printf("Polynomial: ");
    for (int i = degree; i >= 0; i--) {
        if (coefficients[i]!= 0) {
            if (i == degree) {
                printf("%.2fx^%d ", coefficients[i], i);
            } else if (i > 1) {
                printf("%+.2fx^%d ", coefficients[i], i);
            } else if (i == 1) {
                printf("%+.2fx ", coefficients[i]);
            } else {
                printf("%+.2f", coefficients[i]);
            }
        }
    }
    printf("\n");
}

int main() {
    int degree;
    double coefficients[MAX_DEGREE + 1];
    double x, result;

    // 入力検証
    do {
        printf("Enter the degree of the polynomial (0-10): ");
        scanf("%d", &degree);
    } while (degree < 0 || degree > MAX_DEGREE);

    printf("Enter coefficients from highest degree to constant term:\n");
    for (int i = degree; i >= 0; i--) {
        printf("Coefficient for x^%d: ", i);
        scanf("%lf", &coefficients[i]);
    }

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    // 多項式の詳細を表示する
    printPolynomial(degree, coefficients);

    // ホーナー法を使って多項式を評価する
    result = hornerMethod(degree, coefficients, x);

    // フォーマットされた結果の出力
    printf("Polynomial Evaluation:\n");
    printf("P(x) = f(%.2f) = %.2f\n", x, result);

    return 0;
}

更新されたプログラムをコンパイルして実行します。

gcc polynomial_eval.c -o polynomial_eval
./polynomial_eval

出力例:

Enter the degree of the polynomial (0-10): 3
Enter coefficients from highest degree to constant term:
Coefficient for x^3: 2
Coefficient for x^2: -3
Coefficient for x^1: 0
Coefficient for x^0: 5
Enter the value of x: 2
Polynomial: 2.00x^3 -3.00x^2 +5.00
Polynomial Evaluation:
P(x) = f(2.00) = 11.00

主な改良点:

  • 多項式を表示する printPolynomial() 関数を追加
  • 多項式の次数に対する入力検証を実装
  • フォーマットされた出力で結果の表示を強化
  • 多項式とその評価値の両方を表示

まとめ

この実験では、まず C プログラムにおいてユーザー入力から多項式の係数と変数 x を読み取る方法を学びました。これには、多項式の最大次数を定義し、係数を格納する配列を作成し、scanf() を使ってユーザー入力を読み取ることが含まれました。次に、多項式式を効率的に評価するためにホーナー法を実装しました。これにより、多項式の値を計算するために必要な乗算の数が削減されます。これらの手順に従うことで、C 言語で堅牢な多項式評価アルゴリズムを実装することができます。