用C语言实现一元多项式加法:详细代码及解析

本文将展示如何使用C语言实现一元多项式加法,并提供一个示例代码。我们将使用链表来存储多项式,并通过遍历链表来实现加法运算。

代码实现

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

typedef struct node {
    int coef;  // 系数
    int exp;   // 指数
    struct node* next;  // 下一个节点
} Node;

Node* createNode(int coef, int exp) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->coef = coef;
    newNode->exp = exp;
    newNode->next = NULL;
    return newNode;
}

void insertNode(Node** head, Node* newNode) {
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    Node* cur = *head;
    while (cur->next != NULL) {
        cur = cur->next;
    }
    cur->next = newNode;
}

void addPoly(Node* poly1, Node* poly2, Node** result) {
    while (poly1 != NULL && poly2 != NULL) {
        if (poly1->exp == poly2->exp) {
            int sum = poly1->coef + poly2->coef;
            if (sum != 0) {
                Node* newNode = createNode(sum, poly1->exp);
                insertNode(result, newNode);
            }
            poly1 = poly1->next;
            poly2 = poly2->next;
        } else if (poly1->exp > poly2->exp) {
            Node* newNode = createNode(poly1->coef, poly1->exp);
            insertNode(result, newNode);
            poly1 = poly1->next;
        } else {
            Node* newNode = createNode(poly2->coef, poly2->exp);
            insertNode(result, newNode);
            poly2 = poly2->next;
        }
    }
    while (poly1 != NULL) {
        Node* newNode = createNode(poly1->coef, poly1->exp);
        insertNode(result, newNode);
        poly1 = poly1->next;
    }
    while (poly2 != NULL) {
        Node* newNode = createNode(poly2->coef, poly2->exp);
        insertNode(result, newNode);
        poly2 = poly2->next;
    }
}

void displayPoly(Node* poly) {
    while (poly != NULL) {
        printf('%dx^%d ', poly->coef, poly->exp);
        if (poly->next != NULL && poly->next->coef > 0) {
            printf('+ ');
        }
        poly = poly->next;
    }
    printf('
');
}

int main() {
    Node* poly1 = NULL;
    Node* poly2 = NULL;
    Node* result = NULL;

    // 创建多项式1
    Node* node1 = createNode(3, 4);
    Node* node2 = createNode(2, 2);
    Node* node3 = createNode(1, 0);
    insertNode(&poly1, node1);
    insertNode(&poly1, node2);
    insertNode(&poly1, node3);
    printf('Polynomial 1: ');
    displayPoly(poly1);

    // 创建多项式2
    Node* node4 = createNode(2, 3);
    Node* node5 = createNode(1, 1);
    Node* node6 = createNode(4, 0);
    insertNode(&poly2, node4);
    insertNode(&poly2, node5);
    insertNode(&poly2, node6);
    printf('Polynomial 2: ');
    displayPoly(poly2);

    // 计算多项式1和多项式2的和
    addPoly(poly1, poly2, &result);
    printf('Sum: ');
    displayPoly(result);

    return 0;
}

代码解析

  1. 数据结构: 我们使用一个名为Node的结构体来表示多项式的每个项,它包含系数coef、指数exp和指向下一个节点的指针next
  2. 函数createNode: 该函数用于创建一个新的Node节点,并返回指向该节点的指针。
  3. 函数insertNode: 该函数用于将一个新的节点插入到链表的末尾。
  4. 函数addPoly: 该函数实现多项式加法。它遍历两个多项式的链表,并根据指数大小进行比较和加法运算。
  5. 函数displayPoly: 该函数用于以标准数学格式显示多项式。
  6. 主函数main: 该函数创建了两个多项式,调用addPoly函数计算它们的和,并使用displayPoly函数输出结果。

运行结果

Polynomial 1: 3x^4 + 2x^2 + 1
Polynomial 2: 2x^3 + 1x^1 + 4
Sum: 3x^4 + 2x^3 + 2x^2 + 1x^1 + 5

总结

本文提供了一个用C语言实现一元多项式加法的示例代码。该代码使用链表来存储多项式,并通过遍历链表来实现加法运算。代码中每个函数的功能都进行了详细解释,读者可以根据本文提供的代码和解释来学习和理解一元多项式加法的实现方法。

C语言实现一元多项式加法:详细代码及解析

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

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