C语言实现多项式的乘法运算(修正版)

创作灵感: 多项式乘法是数学中的基本操作,也常常在计算机科学和工程中得到应用。本文将介绍一个用C语言编写的程序,演示如何实现多项式的乘法运算。通过这个程序,我们可以学习如何使用链表数据结构来表示和操作多项式,以及如何进行多项式的乘法计算。

技术笔记要点: 在这个C语言程序中,我们首先定义了一个多项式的每一项的结构体Node,包括系数(coefficient)、指数(exponent)和指向下一个项的指针(next)。

主要函数和步骤如下:

  1. createTerm函数用于创建一个新的多项式项,分配内存并初始化其成员。
  2. addTerm函数用于向多项式中添加一个项,将新项插入到链表中。
  3. combineLikeTerms函数用于合并同类项,即将具有相同指数的项的系数相加,从而简化多项式。
  4. printPolynomial函数用于打印多项式,格式化输出多项式的表达式。
  5. freePolynomial函数用于释放多项式链表的内存。
  6. multiplyPolynomials函数用于计算两个多项式的乘积,将结果保存在一个新的多项式链表中。

main函数中,我们首先定义了两个多项式poly1和poly2,并从标准输入中读取用户输入的多项式系数和指数。然后,我们打印输入的两个多项式,并调用multiplyPolynomials函数计算它们的乘积。最后,我们打印乘积的结果,并释放内存以避免内存泄漏。

修正版主要解决了输入读取问题,确保了程序能够正确读取多项式的系数和指数,以及正确地处理多项式中的零项。

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

// 定义多项式的每一项的结构体
typedef struct Node {
    double coefficient;
    int exponent;
    struct Node *next;
} Node;

// 创建一个新的多项式项
Node *createTerm(double coefficient, int exponent) {
    Node *term = (Node *)malloc(sizeof(Node));
    term->coefficient = coefficient;
    term->exponent = exponent;
    term->next = NULL;
    return term;
}

// 向多项式中添加一个项
void addTerm(Node **poly, double coefficient, int exponent) {
    Node *newTerm = createTerm(coefficient, exponent);
    if (*poly == NULL) {
        *poly = newTerm;
    } else {
        Node *current = *poly;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newTerm;
    }
}

// 合并同类项
void combineLikeTerms(Node *poly) {
    if (poly == NULL) {
        return;
    }

    Node *current = poly;
    while (current != NULL) {
        Node *runner = current;
        while (runner->next != NULL) {
            if (runner->next->exponent == current->exponent) {
                current->coefficient += runner->next->coefficient;
                Node *temp = runner->next;
                runner->next = runner->next->next;
                free(temp);
            } else {
                runner = runner->next;
            }
        }
        current = current->next;
    }
}

// 打印多项式
void printPolynomial(const char *name, Node *poly) {
    printf("%s=", name);
    if (poly == NULL) {
        printf("0.0\n");
        return;
    }
    int firstTerm = 1;
    while (poly != NULL) {
        if (firstTerm) {
            firstTerm = 0;
        } else {
            printf(poly->coefficient >= 0 ? "+" : "");
        }
        if (poly->exponent == 0) {
            printf("%.1f", poly->coefficient);
        } else if (poly->exponent == 1) {
            if (poly->coefficient == 1) {
                printf("X");
            } else if (poly->coefficient == -1) {
                printf("-X");
            } else {
                printf("%.1fX", poly->coefficient);
            }
        } else {
            if (poly->coefficient == 1) {
                printf("X^%d", poly->exponent);
            } else if (poly->coefficient == -1) {
                printf("-X^%d", poly->exponent);
            } else {
                printf("%.1fX^%d", poly->coefficient, poly->exponent);
            }
        }
        poly = poly->next;
    }
    printf("\n");
}

// 释放多项式链表
void freePolynomial(Node *poly) {
    while (poly != NULL) {
        Node *temp = poly;
        poly = poly->next;
        free(temp);
    }
}

// 计算两个多项式的乘积
Node *multiplyPolynomials(Node *poly1, Node *poly2) {
    Node *result = NULL;

    for (Node *term1 = poly1; term1 != NULL; term1 = term1->next) {
        for (Node *term2 = poly2; term2 != NULL; term2 = term2->next) {
            double coefficient = term1->coefficient * term2->coefficient;
            int exponent = term1->exponent + term2->exponent;
            addTerm(&result, coefficient, exponent);
        }
    }

    // 合并同类项
    combineLikeTerms(result);

    return result;
}

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

    // 读取第一个多项式
    double coefficient;
    int exponent;
    while (1) {
        scanf("%lf,%d", &coefficient, &exponent);
        if (coefficient == 0.0 && exponent == 0) {
            break;
        }
        addTerm(&poly1, coefficient, exponent);
    }

    // 读取第二个多项式
    while (1) {
        scanf("%lf,%d", &coefficient, &exponent);
        if (coefficient == 0.0 && exponent == 0) {
            break;
        }
        addTerm(&poly2, coefficient, exponent);
    }

    // 打印两个多项式
    printPolynomial("f(X)", poly1);
    printPolynomial("g(X)", poly2);

    // 计算并打印两个多项式的乘积
    Node *result = multiplyPolynomials(poly1, poly2);
    printPolynomial("f(X)*g(X)", result);

    // 释放内存
    freePolynomial(poly1);
    freePolynomial(poly2);
    freePolynomial(result);

    return 0;
}

 

 

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要用C语言链表实现多项式乘法,首先需要定义一个多项式的结构体,其中包含多项式的系数和指数。然后可以通过创建链表的方式存储多项式。 下面是一个示例代码,实现多项式乘法的函数: ```c #include <stdio.h> #include <stdlib.h> // 定义多项式节点结构体 typedef struct PolynomialNode { int coefficient; // 系数 int exponent; // 指数 struct PolynomialNode* next; // 下一个节点指针 } PolynomialNode; // 创建多项式节点 PolynomialNode* createNode(int coefficient, int exponent) { PolynomialNode* node = (PolynomialNode*)malloc(sizeof(PolynomialNode)); node->coefficient = coefficient; node->exponent = exponent; node->next = NULL; return node; } // 在链表末尾插入节点 void insertNode(PolynomialNode** head, int coefficient, int exponent) { PolynomialNode* newNode = createNode(coefficient, exponent); if (*head == NULL) { *head = newNode; } else { PolynomialNode* curr = *head; while (curr->next != NULL) { curr = curr->next; } curr->next = newNode; } } // 打印多项式 void printPolynomial(PolynomialNode* head) { PolynomialNode* curr = head; while (curr != NULL) { printf("%dx^%d ", curr->coefficient, curr->exponent); curr = curr->next; if (curr != NULL) { printf("+ "); } } printf("\n"); } // 多项式乘法 PolynomialNode* multiplyPolynomials(PolynomialNode* poly1, PolynomialNode* poly2) { PolynomialNode* result = NULL; PolynomialNode* curr1 = poly1; while (curr1 != NULL) { PolynomialNode* curr2 = poly2; while (curr2 != NULL) { int coefficient = curr1->coefficient * curr2->coefficient; int exponent = curr1->exponent + curr2->exponent; insertNode(&result, coefficient, exponent); curr2 = curr2->next; } curr1 = curr1->next; } return result; } // 释放链表内存 void freeList(PolynomialNode* head) { PolynomialNode* curr = head; while (head != NULL) { head = head->next; free(curr); curr = head; } } int main() { PolynomialNode* poly1 = NULL; PolynomialNode* poly2 = NULL; // 多项式1: 3x^2 + 4x^1 + 2x^0 insertNode(&poly1, 3, 2); insertNode(&poly1, 4, 1); insertNode(&poly1, 2, 0); // 多项式2: 2x^2 + 5x^0 insertNode(&poly2, 2, 2); insertNode(&poly2, 5, 0); printf("多项式1:"); printPolynomial(poly1); printf("多项式2:"); printPolynomial(poly2); PolynomialNode* result = multiplyPolynomials(poly1, poly2); printf("乘法结果:"); printPolynomial(result); // 释放内存 freeList(poly1); freeList(poly2); freeList(result); return 0; } ``` 运行上述代码,即可实现多项式乘法。代码中通过创建链表的方式存储多项式,然后使用两重循环遍历两个链表,并将乘积的结果插入到一个新的链表中。最后打印新链表中的多项式结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值