亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c語言怎么實現一元多項式運算

小億
105
2023-12-11 09:18:01
欄目: 編程語言

在C語言中,可以使用結構體來表示一元多項式,并定義相應的操作函數來實現多項式的運算。下面是一個簡單的實現示例:

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

typedef struct PolyNode {
    float coef;  // 系數
    int exp;  // 指數
    struct PolyNode* next;  // 指向下一項的指針
} PolyNode;

typedef PolyNode* Polynomial;  // 多項式類型

// 創建一個新的項
PolyNode* createNode(float coef, int exp) {
    PolyNode* newNode = (PolyNode*)malloc(sizeof(PolyNode));
    newNode->coef = coef;
    newNode->exp = exp;
    newNode->next = NULL;
    return newNode;
}

// 插入一項到多項式的末尾
void insertTerm(Polynomial* poly, float coef, int exp) {
    PolyNode* newNode = createNode(coef, exp);
    if (*poly == NULL) {
        *poly = newNode;
    } else {
        PolyNode* curr = *poly;
        while (curr->next != NULL) {
            curr = curr->next;
        }
        curr->next = newNode;
    }
}

// 打印多項式
void printPolynomial(Polynomial poly) {
    PolyNode* curr = poly;
    while (curr != NULL) {
        printf("%.2fx^%d", curr->coef, curr->exp);
        if (curr->next != NULL) {
            printf(" + ");
        }
        curr = curr->next;
    }
    printf("\n");
}

// 相加兩個多項式
Polynomial addPolynomials(Polynomial poly1, Polynomial poly2) {
    Polynomial sumPoly = NULL;
    PolyNode* curr1 = poly1;
    PolyNode* curr2 = poly2;

    while (curr1 != NULL && curr2 != NULL) {
        if (curr1->exp > curr2->exp) {
            insertTerm(&sumPoly, curr1->coef, curr1->exp);
            curr1 = curr1->next;
        } else if (curr1->exp < curr2->exp) {
            insertTerm(&sumPoly, curr2->coef, curr2->exp);
            curr2 = curr2->next;
        } else {
            float coefSum = curr1->coef + curr2->coef;
            if (coefSum != 0) {
                insertTerm(&sumPoly, coefSum, curr1->exp);
            }
            curr1 = curr1->next;
            curr2 = curr2->next;
        }
    }

    // 將剩余項加入和多項式中
    while (curr1 != NULL) {
        insertTerm(&sumPoly, curr1->coef, curr1->exp);
        curr1 = curr1->next;
    }
    while (curr2 != NULL) {
        insertTerm(&sumPoly, curr2->coef, curr2->exp);
        curr2 = curr2->next;
    }

    return sumPoly;
}

int main() {
    Polynomial poly1 = NULL;
    Polynomial poly2 = NULL;
    Polynomial sumPoly = NULL;

    // 初始化多項式1
    insertTerm(&poly1, 3.0, 2);
    insertTerm(&poly1, 4.0, 1);
    insertTerm(&poly1, 2.0, 0);

    // 初始化多項式2
    insertTerm(&poly2, 1.0, 3);
    insertTerm(&poly2, -2.0, 2);
    insertTerm(&poly2, 5.0, 0);

    // 打印多項式1和多項式2
    printf("Polynomial 1: ");
    printPolynomial(poly1);
    printf("Polynomial 2: ");
    printPolynomial(poly2);

    // 相加兩個多項式
    sumPoly = addPolynomials(poly1, poly2);

    // 打印結果多項式
    printf("Sum of polynomials: ");
    printPolynomial(sumPoly);

    return 0;
}

在上述示例中,我們使用PolyNode結構體表示多項式的每一項。PolyNode結構體包括coef(系數)和exp(指數)兩個成員變量,以及一個指向下一項的指針next。我們還定義了Polynomial類型作為多項式的別名,同時定義了一些操作函數,如createNode

0
陈巴尔虎旗| 自贡市| 德州市| 道孚县| 海盐县| 油尖旺区| 和平县| 峨眉山市| 新绛县| 宝山区| 阳西县| 武宣县| 北辰区| 永靖县| 永年县| 辽宁省| 凤城市| 镇江市| 曲沃县| 元朗区| 阿坝县| 马鞍山市| 朝阳县| 唐河县| 新巴尔虎左旗| 澄迈县| 眉山市| 牙克石市| 磐石市| 越西县| 鹤庆县| 武清区| 鸡泽县| 逊克县| 洛阳市| 昌宁县| 南投市| 雷波县| 蓝山县| 桂阳县| 福清市|