一元多项式的乘法与加法运算——C++(链表)

时间:2021-07-29
本文章向大家介绍一元多项式的乘法与加法运算——C++(链表),主要包括一元多项式的乘法与加法运算——C++(链表)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include <iostream>

using namespace std;

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

List Read();
void Print(List L);
List GetMutiply(List L1, List L2);
List GetSum(List L1, List L2);
int Count(List L);
List Sort(List L);

List Sort(List L)
{
    List head = L;
    List temp;
    int cou = Count(L);
    int coef;
    int exp;
    int tcoef;
    while (cou--)
    {
        temp = head->next;
        while (temp->next)
        {
            if (temp->exp < temp->next->exp)
            {
                coef = temp->coef;
                exp = temp->exp;
                temp->exp = temp->next->exp;
                temp->coef = temp->next->coef;
                temp->next->coef = coef;
                temp->next->exp = exp;
                temp = temp->next;
            }
            else if (temp->exp == temp->next->exp)
            {
                tcoef = temp->coef + temp->next->coef;
                temp->coef = tcoef;
                temp->next = temp->next->next;
                temp = temp->next;
            }
            else
            {
                temp = temp->next;
            }
        }
    }
    return head;
}

List Read()
{
    List L = (struct Node *)malloc(sizeof(Node));
    List head;
    List add = NULL;
    L->next = NULL;
    head = L;
    int n, coef, exp;
    cin >> n;
    if (n != 0)
    {
        while (n--)
        {
            cin >> coef >> exp;
            if (coef != 0)
            {
                add = (struct Node *)malloc(sizeof(Node));
                add->coef = coef;
                add->exp = exp;
                L->next = add;
                L = add;
            }
        }
        L->next = NULL;
    }
    return head;
}

void Print(List L)
{
    int count = Count(L);
    if (L->next)
    {
        L = L->next;
        while (L->next)
        {
            if (L->coef != 0)
            {
                cout << L->coef << " " << L->exp << " ";
                L = L->next;
            }
            else
            {
                L = L->next;
            }
        }
        cout << L->coef << " " << L->exp << endl;
    }
    else if (count == 0)
    {
        cout << 0 << " " << 0 << endl;
    }
    else
    {
        cout << 0 << " " << 0;
    }
}

List GetSum(List L1, List L2)
{
    List head = (Node *)malloc(sizeof(Node)), temp1, temp2, newlist, add = NULL;
    head->next = NULL;
    newlist = head;
    temp1 = L1->next;
    temp2 = L2->next;
    int tcoef;
    while (temp1 && temp2)
    {
        if (temp1->exp > temp2->exp)
        {
            add = (struct Node *)malloc(sizeof(Node));
            add->coef = temp1->coef;
            add->exp = temp1->exp;
            newlist->next = add;
            newlist = add;
            temp1 = temp1->next;
        }
        else if (temp1->exp < temp2->exp)
        {
            add = (struct Node *)malloc(sizeof(Node));
            add->coef = temp2->coef;
            add->exp = temp2->exp;
            newlist->next = add;
            newlist = add;
            temp2 = temp2->next;
        }
        else
        {
            if (temp1->coef + temp2->coef != 0)
            {
                tcoef = temp1->coef + temp2->coef;
                add = (struct Node *)malloc(sizeof(Node));
                add->coef = tcoef;
                add->exp = temp1->exp;
                newlist->next = add;
                newlist = add;
            }
            temp1 = temp1->next;
            temp2 = temp2->next;
        }
    }
    if (temp1)
        newlist->next = temp1;
    else
        newlist->next = temp2;
    return head;
}

List GetMutiply(List L1, List L2)
{
    List head = (Node *)malloc(sizeof(Node)), temp1 = L1->next, temp2 = L2->next, newlist, add = NULL;
    head->next = NULL;
    newlist = head;
    int tcoef, texp;
    int count1, count2;
    if (temp1 && temp2)
    {
        count1 = Count(L1);
        count2 = Count(L2);
        for (int i = count1; i > 0; i--)
        {
            for (int j = count2; j > 0; j--)
            {
                tcoef = temp1->coef * temp2->coef;
                texp = temp1->exp + temp2->exp;
                add = (struct Node *)malloc(sizeof(Node));
                add->coef = tcoef;
                add->exp = texp;
                newlist->next = add;
                newlist = add;
                temp2 = temp2->next;
            }
            temp1 = temp1->next;
            temp2 = L2->next;
        }
    }

    head = Sort(head);
    return head;
}

int Count(List L)
{
    int count = 0;
    List t = L->next;
    while (t)
    {
        count++;
        t = t->next;
    }
    return count;
}

int main()
{
    List L1 = Read();
    List L2 = Read();
    List L = GetMutiply(L1, L2);
    Print(L);
    L = GetSum(L1, L2);
    Print(L);
}

原文地址:https://www.cnblogs.com/Transirizo/p/15075300.html