以下是使用 C 语言解决一元多项式加法运算的示例代码:

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

// 定义多项式结构体
typedef struct Poly {
    int coef;  // 系数
    int exp;   // 指数
    struct Poly *next;  // 下一个节点
} Poly;

// 创建多项式
Poly *createPoly() {
    Poly *head = (Poly*)malloc(sizeof(Poly));  // 创建头节点
    head->next = NULL;
    return head;
}

// 插入项
void insertTerm(Poly *poly, int coef, int exp) {
    Poly *prev = poly;
    Poly *curr = poly->next;
    while (curr != NULL && curr->exp > exp) {  // 找到合适的位置插入
        prev = curr;
        curr = curr->next;
    }
    if (curr != NULL && curr->exp == exp) {  // 若指数相同,则合并同类项
        curr->coef += coef;
        if (curr->coef == 0) {  // 若系数为0,则删除该节点
            prev->next = curr->next;
            free(curr);
        }
    } else {  // 否则插入新项
        Poly *newTerm = (Poly*)malloc(sizeof(Poly));
        newTerm->coef = coef;
        newTerm->exp = exp;
        newTerm->next = curr;
        prev->next = newTerm;
    }
}

// 多项式加法
Poly *addPoly(Poly *poly1, Poly *poly2) {
    Poly *head = createPoly();
    Poly *p1 = poly1->next;
    Poly *p2 = poly2->next;
    while (p1 != NULL && p2 != NULL) {
        if (p1->exp > p2->exp) {
            insertTerm(head, p1->coef, p1->exp);
            p1 = p1->next;
        } else if (p1->exp < p2->exp) {
            insertTerm(head, p2->coef, p2->exp);
            p2 = p2->next;
        } else {
            insertTerm(head, p1->coef + p2->coef, p1->exp);
            p1 = p1->next;
            p2 = p2->next;
        }
    }
    while (p1 != NULL) {  // 将剩余项插入
        insertTerm(head, p1->coef, p1->exp);
        p1 = p1->next;
    }
    while (p2 != NULL) {
        insertTerm(head, p2->coef, p2->exp);
        p2 = p2->next;
    }
    return head;
}

// 输出多项式
void printPoly(Poly *poly) {
    Poly *curr = poly->next;
    while (curr != NULL) {
        printf('%d*x^%d', curr->coef, curr->exp);
        if (curr->next != NULL && curr->next->coef > 0) {
            printf('+');
        }
        curr = curr->next;
    }
    printf('
');
}

int main() {
    // 创建多项式1:3x^4+2x^3+4x^1+9x^0
    Poly *poly1 = createPoly();
    insertTerm(poly1, 3, 4);
    insertTerm(poly1, 2, 3);
    insertTerm(poly1, 4, 1);
    insertTerm(poly1, 9, 0);
    printf('多项式1:');
    printPoly(poly1);

    // 创建多项式2:-2x^3+7x^2+5x^1+1x^0
    Poly *poly2 = createPoly();
    insertTerm(poly2, -2, 3);
    insertTerm(poly2, 7, 2);
    insertTerm(poly2, 5, 1);
    insertTerm(poly2, 1, 0);
    printf('多项式2:');
    printPoly(poly2);

    // 计算多项式1 + 多项式2
    Poly *result = addPoly(poly1, poly2);
    printf('多项式1 + 多项式2 = ');
    printPoly(result);

    return 0;
}

以上代码中,我们首先定义一个多项式结构体 Poly,包含系数 coef、指数 exp 和下一个节点指针 next。然后,我们实现了三个函数:

  • createPoly():创建多项式头节点。
  • insertTerm():向多项式中插入一项。
  • addPoly():计算两个多项式的加法。

addPoly() 函数中,我们使用两个指针 p1p2 分别遍历两个多项式,并比较当前项的指数大小。如果指数相同,则合并同类项;否则插入较小的项。最后,将剩余的项插入到结果多项式中。

最后,我们使用 printPoly() 函数输出多项式。在输出过程中,我们需要特别处理一些情况,如系数为负数和系数为0的项。


原文地址: https://www.cveoy.top/t/topic/lRHw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录