C++链表实现多项式的创建及求和

前言

本项目中只包含简单的多项式的创建和两个多项式之间的求和等基础功能,适合有一定编辑语言基础的同学,本文中出现的代码都为基础的知识,由于是本人临时编写,其中有一定的错误的个人理解及代码,若有问题,请指正。本人发布该博客的意图在于基础的学习交流,希望该文章能够以一种交流工具的形式存在。☆*: .。. o(≧▽≦)o .。.:*☆  ☆*: .。. o(≧▽≦)o .。.:*☆  ╰(*°▽°*)╯  ╰(*°▽°*)╯

类的创建

用一个节点类来放置多项式中的每一项的指数与系数信息,再用一个链表类创建一个链表将各个项连接起来,用来表达一个多项式

//多项式的节点类
class Polynomial_Node
{
public:
	int Index;//指数
	int Coefficient;//系数
	Polynomial_Node* next;
};

//多项式的链表类
class Polynomial_List
{
public:
	Polynomial_Node* head;//头节点
    int Size;//多项式长度
};

多项式的创建函数

//传入想要创建的多项式的长度,即多项式中的项的个数
Polynomial_List* Create_Polynomial_List(int Length)
{
    Polynomial_List* PL = new Polynomial_List;
    PL->head = new Polynomial_Node;
    PL->head->next = NULL;//创建并初始化一个新的多项式链表
    Polynomial_Node* move = PL->head;//定义一个从链表头节点开始的move指针
    PL->Size = Length;//初始化多项式长度
    for (int i = 0; i < Length; i++)//循环输入多项式中每一项的信息
    {
        cout << "请输入多项式第" << i + 1 << "项的指数和系数:" << endl;
        int temp_Index, temp_Coefficient;
        cin >> temp_Index >> temp_Coefficient;

        Polynomial_Node* New_Polynomial_Node = new Polynomial_Node;
        //定义一个节点,用于将输入的项的数据连接到链表中

        New_Polynomial_Node->Index = temp_Index;
        New_Polynomial_Node->Coefficient = temp_Coefficient;
        New_Polynomial_Node->next = NULL;
        move->next = New_Polynomial_Node;//连接链表
        move = move->next;//move指针向后移动
    }
    return PL;//返回一个多项式链表指针
}

两个多项式之间的求和

Polynomial_List* Sum_Polynomial_Lists(Polynomial_List* PL_A, Polynomial_List* PL_B)
{
    //定义一个新的多项式链表并初始化,用于表示两个多项式的和
    Polynomial_List* PL_C = new Polynomial_List;
    PL_C->head = new Polynomial_Node;
    PL_C->head->next = NULL;
    PL_C->Size = 0;

    //分别定义从三条链表头节点及其下一项开始移动的move指针
    Polynomial_Node* PL_A_move = PL_A->head->next;
    Polynomial_Node* PL_B_move = PL_B->head->next;
    Polynomial_Node* PL_C_move = PL_C->head;

    while (PL_A_move && PL_B_move)//当两项系数均不为0时
    {
        Polynomial_Node* temp = new Polynomial_Node;
        if (PL_A_move->Index == PL_B_move->Index)//当两项的指数相同时,进行直接求和
        {
            temp->Coefficient = PL_A_move->Coefficient + PL_B_move->Coefficient;
            temp->Index = PL_A_move->Index;

            //move指针向后移动
            PL_A_move = PL_A_move->next;
            PL_B_move = PL_B_move->next;
        }
        //当后一项系数更大时,将前一项直接加入和链表
        else if (PL_A_move->Index < PL_B_move->Index)
        {
            temp->Coefficient = PL_A_move->Coefficient;
            temp->Index = PL_A_move->Index;
            PL_A_move = PL_A_move->next;
        }
        else
        {
            temp->Coefficient = PL_B_move->Coefficient;
            temp->Index = PL_B_move->Index;
            PL_B_move = PL_B_move->next;
        }

        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_C->Size++;
    }

    // 处理剩余项
    while (PL_A_move)
    {
        Polynomial_Node* temp = new Polynomial_Node;
        temp->Coefficient = PL_A_move->Coefficient;
        temp->Index = PL_A_move->Index;
        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_A_move = PL_A_move->next;
        PL_C->Size++;
    }

    while (PL_B_move)
    {
        Polynomial_Node* temp = new Polynomial_Node;
        temp->Coefficient = PL_B_move->Coefficient;
        temp->Index = PL_B_move->Index;
        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_B_move = PL_B_move->next;
        PL_C->Size++;
    }

    return PL_C;//返回指向多项式之和链表的指针
}

完整代码:

#include"iostream"
using namespace std;

class Polynomial_Node
{
public:
	int Index;
	int Coefficient;
	Polynomial_Node* next;
};

class Polynomial_List
{
public:
	Polynomial_Node* head;
    int Size;
};

Polynomial_List* Create_Polynomial_List(int Length)
{
    Polynomial_List* PL = new Polynomial_List;
    PL->head = new Polynomial_Node;
    PL->head->next = NULL;
    Polynomial_Node* move = PL->head;
    PL->Size = Length;
    for (int i = 0; i < Length; i++)
    {
        cout << "请输入多项式第" << i + 1 << "项的指数和系数:" << endl;
        int temp_Index, temp_Coefficient;
        cin >> temp_Index >> temp_Coefficient;
        Polynomial_Node* New_Polynomial_Node = new Polynomial_Node;
        New_Polynomial_Node->Index = temp_Index;
        New_Polynomial_Node->Coefficient = temp_Coefficient;
        New_Polynomial_Node->next = NULL;
        move->next = New_Polynomial_Node;
        move = move->next;
    }
    return PL;
}

Polynomial_List* Sum_Polynomial_Lists(Polynomial_List* PL_A, Polynomial_List* PL_B)
{
    Polynomial_List* PL_C = new Polynomial_List;
    PL_C->head = new Polynomial_Node;
    PL_C->head->next = NULL;
    PL_C->Size = 0;

    Polynomial_Node* PL_A_move = PL_A->head->next;
    Polynomial_Node* PL_B_move = PL_B->head->next;
    Polynomial_Node* PL_C_move = PL_C->head;

    while (PL_A_move && PL_B_move)
    {
        Polynomial_Node* temp = new Polynomial_Node;
        if (PL_A_move->Index == PL_B_move->Index)
        {
            temp->Coefficient = PL_A_move->Coefficient + PL_B_move->Coefficient;
            temp->Index = PL_A_move->Index;
            PL_A_move = PL_A_move->next;
            PL_B_move = PL_B_move->next;
        }
        else if (PL_A_move->Index < PL_B_move->Index)
        {
            temp->Coefficient = PL_A_move->Coefficient;
            temp->Index = PL_A_move->Index;
            PL_A_move = PL_A_move->next;
        }
        else
        {
            temp->Coefficient = PL_B_move->Coefficient;
            temp->Index = PL_B_move->Index;
            PL_B_move = PL_B_move->next;
        }

        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_C->Size++;
    }

    // 处理剩余项
    while (PL_A_move)
    {
        Polynomial_Node* temp = new Polynomial_Node;
        temp->Coefficient = PL_A_move->Coefficient;
        temp->Index = PL_A_move->Index;
        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_A_move = PL_A_move->next;
        PL_C->Size++;
    }

    while (PL_B_move)
    {
        Polynomial_Node* temp = new Polynomial_Node;
        temp->Coefficient = PL_B_move->Coefficient;
        temp->Index = PL_B_move->Index;
        temp->next = NULL;
        PL_C_move->next = temp;
        PL_C_move = PL_C_move->next;
        PL_B_move = PL_B_move->next;
        PL_C->Size++;
    }

    return PL_C;
}

void Print_Polynomial(Polynomial_List* PL)
{
    Polynomial_Node* PL_move = PL->head->next;
    for (int i = 0; i < PL->Size; i++)
    {
        if (PL_move->next == NULL)
        {
            cout << PL_move->Coefficient << " * (x ^ " << PL_move->Index << ")";
            break;
        }
        cout << PL_move->Coefficient << " * (x ^ " << PL_move->Index << ") + ";
        PL_move = PL_move->next;
    }
}

int main()
{
    int Num_A, Num_B;
    cout << "请输入你要创建的第一个多项式的项数:";
    cin >> Num_A;
    Polynomial_List* PL_A = Create_Polynomial_List(Num_A);
    cout << "请输入你要创建的第二个多项式的项数:";
    cin >> Num_B;
    Polynomial_List* PL_B = Create_Polynomial_List(Num_B);
    Polynomial_List* PL_C = Sum_Polynomial_Lists(PL_A, PL_B);
    cout << "它们的和为:" << endl;
    Print_Polynomial(PL_C);
    return 0;
}

总结:

本博客中只是简单实现了以链表的形式实现两条多项式的创建及其求和,再以简单的打印形式将其输出,其中注释及个人理解有部分差错的,若有发现,希望指正。本博客中代码并不严谨,对功能实现的方式存在局限性,后续可能会进行功能的完善,例如可以自由控制多项式的创建个数,及更加自由的多项式求和,也希望这份用于学习交流的博客可以给同学们带来启发,实现功能的完善与开发。(●'◡'●)  <( ̄︶ ̄)↗[GO!]

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值