7-1 一元多项式的乘法与加法运算(c++类法)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

代码长度限制

16 KB

时间限制

200 ms

内存限制

64 MB

代码如下:

#include<iostream>
using namespace std;
class PolyData {
public:
    int coef;//系数
    int expn;//幂
};

//创建链表
struct Node
{
    PolyData data;
    Node* next;
};


//定义一个多项式类
class Polynomial{
public:
    Node *head;
    Polynomial() {
        head = new Node;
        head->next = NULL;
    }
    ~Polynomial() = default;
    Node* Add(Node* list1, Node* list2);
    Node* perMul(Node* list1, Node* list2);
    Node* Mul(Node* list1, Node* list2);
    void InputList();
    void OutputList();

};

Node* Polynomial:: perMul(Node* list1, Node* list2) {
    Node* hhead = new Node;
    hhead->next = NULL;
    Node* last = hhead, * temp, * p1 = list1, * p2 = list2->next;
    while (p2) {
        temp = new Node;
        temp->next = NULL;
        temp->data.coef = p1->data.coef * p2->data.coef;
        temp->data.expn = p1->data.expn + p2->data.expn;
        last->next = temp;
        last = last->next;
        p2 = p2->next;
    }
    return hhead;
}

Node* Polynomial::Mul(Node* list1, Node* list2) {
    Node* thishead = new Node;
    thishead->next = NULL;
    Node* temp;
    for (Node* p1 = list1->next, *p2 = list2; p1; p1 = p1->next) {
        temp = perMul(p1, p2);
        thishead = Add(thishead, temp);
    }
    return thishead;
}


Node* Polynomial:: Add(Node* list1, Node* list2)
{
    Node* head = new Node;
    head->next = NULL;
    Node* last = head, * temp, * p1 = list1->next, * p2 = list2->next;
    while (p1 && p2)
    {
        temp = new Node;
        temp->next = NULL;
        if (p1->data.expn > p2->data.expn)
        {
            temp->data.coef = p1->data.coef;
            temp->data.expn = p1->data.expn;
            p1 = p1->next;
        }
        else if (p1->data.expn < p2->data.expn)
        {
            temp->data.coef = p2->data.coef;
            temp->data.expn = p2->data.expn;
            p2 = p2->next;
        }
        else
        {
            if (p1->data.coef + p2->data.coef != 0)
            {
                temp->data.coef = p1->data.coef + p2->data.coef;
                temp->data.expn = p1->data.expn;
            }
            else
            {
                delete temp;
                temp = NULL;
            }
            p1 = p1->next;
            p2 = p2->next;
        }
        if (temp)
        {
            last->next = temp;
            last = last->next;
        }
    }
    if (p1)  last->next = p1;
    if (p2)  last->next = p2;
    return head;
}

void Polynomial:: InputList() {
    Node* ip, * last;
    last = head;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        ip = new Node;
        cin >> ip->data.coef>> ip->data.expn;
        ip->next = NULL;
        last->next = ip;
        last = ip;
    }
    last->next = NULL;
    return;
}


void Polynomial::OutputList() {
    Node* op;
    op = this->head->next;
    if (!op) cout <<"0 0";
    while (op) {
        cout << op->data.coef << " " << op->data.expn;
        if (op->next)
            cout << " ";
        op = op->next;
    }
    cout << endl;
}

int main()
{
    Polynomial Sa, Sb, Sc, Sd;
    Sa.InputList();
    Sb.InputList();
    Sd.head = Sd.Mul(Sa.head, Sb.head);
    Sd.OutputList();
    Sc.head = Sc.Add(Sa.head, Sb.head);
    Sc.OutputList();
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值