C语言用单链表实现一元多项式的乘法

输入两个一元多项式(指数从高到低),自定义项数,系数和指数,用单链表方法顺序输出求乘积后的多项式的各项系数和指数。

稍等,帮你写一个

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

typedef struct node
{
    int coef;
    int exp;
    struct node* next;
} Node;

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

Node* create_poly(int n)
{
    Node* head = NULL;
    Node* tail = NULL;
    int coef, exp;
    for (int i = 0; i < n; i++)
    {
        printf("请输入第%d项的系数和指数:", i + 1);
        scanf("%d%d", &coef, &exp);
        if (coef != 0)
        {
            Node* new_node = create_node(coef, exp);
            if (head == NULL)
            {
                head = new_node;
                tail = new_node;
            }
            else
            {
                tail->next = new_node;
                tail = new_node;
            }
        }
    }
    return head;
}

void print_poly(Node* head)
{
    Node* p = head;
    if (p == NULL)
    {
        printf("0\n");
    }
    else
    {
        printf("%dx^%d", p->coef, p->exp);
        p = p->next;
        while (p != NULL)
        {
            printf(" + %dx^%d", p->coef, p->exp);
            p = p->next;
        }
        printf("\n");
    }
}

Node* poly_mult(Node* poly1, Node* poly2)
{
    Node* result = NULL;
    Node* p1 = poly1;
    while (p1 != NULL)
    {
        Node* p2 = poly2;
        while (p2 != NULL)
        {
            int coef = p1->coef * p2->coef;
            int exp = p1->exp + p2->exp;
            Node* q = result;
            while (q != NULL && q->exp > exp)
            {
                q = q->next;
            }
            if (q == NULL)
            {
                Node* new_node = create_node(coef, exp);
                new_node->next = result;
                result = new_node;
            }
            else if (q->exp == exp)
            {
                q->coef += coef;
            }
            else
            {
                Node* new_node = create_node(coef, exp);
                new_node->next = q->next;
                q->next = new_node;
            }
            p2 = p2->next;
        }
        p1 = p1->next;
    }
    return result;
}

int main()
{
    int n1, n2;
    printf("请输入第一个多项式的项数:");
    scanf("%d", &n1);
    Node* poly1 = create_poly(n1);
    printf("第一个多项式为:");
    print_poly(poly1);
    printf("请输入第二个多项式的项数:");
    scanf("%d", &n2);
    Node* poly2 = create_poly(n2);
    printf("第二个多项式为:");
    print_poly(poly2);
    Node* result = poly_mult(poly1, poly2);
    printf("乘积为:");
    print_poly(result);
    return 0;
}


  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/349948
  • 这篇博客也不错, 你可以看下C语言编程常见问题分析,以及错误解决办法!
  • 除此之外, 这篇博客: c语言单链表实现一元多项式相加算法中的 给定两个一元多项式,实现两个一元多项式的相加算法。提示:用一条单链表表示一个一元多项式,每个节点包含三个域:指数域、系数域和后继结点链。 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 可直接运行

    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    struct Node;
    typedef struct Node *PNode;
    struct Node
    {
        int zhishu;
        int xishu;
        PNode next;
    };
    typedef struct Node *LinkList;
    LinkList createNullList_link(void)//创建空链表
    {
        LinkList llist = (LinkList)malloc(sizeof(struct Node));
        if(llist != NULL) llist -> next = NULL;
        else
        {
            printf("out of space!\n");
        }
        return llist;
    }
    PNode locate_link(LinkList llist,int zhishu)//求某元素的存储位置
    {
        PNode p;
        if(llist == NULL) return NULL;
        p = llist->next;
        while(p != NULL && p->zhishu!=zhishu)p=p->next;
        return p;
    }
    int insertPost_link(LinkList llist,PNode p,int zhishu,int xishu)//在p节点后添加新节点
    {
        PNode q = (PNode)malloc(sizeof(struct Node));
        if(q==NULL) {
            printf("out of space.");
            return 0;
        }
        else
        {
            q->zhishu=zhishu; 
            q->xishu=xishu;
            q->next=p->next;
            p->next=q;
            return 1;
        }
        
    }
    int main() 
    {
        LinkList a1 = createNullList_link();
        insertPost_link(a1,a1,1,4);
        PNode p1 = a1->next;
        insertPost_link(a1, p1, 0, 5);
        LinkList a2 = createNullList_link();
        insertPost_link(a2, a2, 1, 6);
        PNode p2 = a2->next;
        insertPost_link(a2, p2, 0, 3);
        //以上设置好了两个一元多项式
        PNode p11 = locate_link(a1,1);
        PNode p21 = locate_link(a2, 1);
        PNode p10 = locate_link(a1, 0);
        PNode p20 = locate_link(a2, 0);
        int xishu1 = p11->xishu + p21->xishu;
        int xishu0 = p10->xishu+p20->xishu;
        printf("the sum is %d x + %d",xishu1,xishu0);
        return 0;
    }
    
  • 您还可以看一下 韦语洋(Lccee)老师的一机一码加密、被破解自动销毁随时授权回收升级系列视频课程课程中的 演示如何破解一个软件绕过注册机(仅作为后续课程的了解)小节, 巩固相关知识点