7-1 一元多项式的加法和乘法

//库函数头文件包含
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//函数状态码定义
#define TRUE        1
#define FALSE       0
#define OK          1
#define ERROR       0
#define INFEASIBLE -1
#define OVERFLOW   -2

typedef int Status;
typedef struct {
    int c;
    int e;
}term,ElemType;

typedef struct LNode{
    ElemType elem;
    struct LNode* next;
}*LinkList,LNode;
typedef LinkList Poly;

Status PolyInit(Poly& P)
{
    P = (LNode*)malloc(sizeof(LNode));
    if (!P)exit(OVERFLOW);
    P->next = NULL;
    return OK;
}
void PolyCreate(Poly& P)
{
    int n, a, b;
    LNode* cur, * rear = P;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a >> b;
        cur = (LNode*)malloc(sizeof(LNode));
        if (!cur)exit(OVERFLOW);
        cur->elem.c = a;
        cur->elem.e = b;
        //cur->next = NULL;
        rear->next = cur;
        rear = rear->next;
    }
    rear->next = NULL;
}
void PolyPrint(Poly& P)
{
    LNode* p = P->next;
    if (!p)
    {
        cout << 0 << " " << 0 << endl;
        return;
    }
    while (p)
    {
        if (p->next != NULL)
            cout << p->elem.c << " " << p->elem.e << " ";
        else
            cout << p->elem.c << " " << p->elem.e;
        p = p->next;
    }
    cout << endl;
}
void PolyCopy(Poly& P, Poly& PP)
{
    LNode *p = PP->next, *q=P,*cur=NULL;
    while (p)
    {
        cur = (LNode*)malloc(sizeof(LNode));
        if (!cur)exit(OVERFLOW);
        cur->elem.c = p->elem.c;
        cur->elem.e = p->elem.e;
        //cur->next = NULL;
        q->next = cur;
        q = q->next;
        p = p->next;
    }
    q->next = NULL;
}
void PolySum(Poly& P1, Poly& P2)
{
    LNode *ha = P1, *hb = P2, *pa = P1->next, *pb = P2->next;
    while (pa && pb)
    {
        if (pa->elem.e > pb->elem.e)
        {
            pa = pa->next; ha = ha->next;
        }
        else if (pa->elem.e == pb->elem.e)
        {
            int sum = pa->elem.c + pb->elem.c;
            if (sum == 0)
            {
                ha->next = pa->next; free(pa); pa = ha->next;//pa = ha; ha = ha->next;
                hb->next = pb->next; free(pb); pb = hb->next;//pa = ha; ha = ha->next;
            }
            else
            {
                pa->elem.c = sum;
                ha = pa; pa = pa->next; hb = pb, pb = pb->next;
            }
        }
        else
        {
            hb->next = pb->next;
            pb->next = ha->next;
            ha->next = pb;
            pb = hb->next;
            ha = ha->next;
            //pa = ha->next;
        }

    }
    if (pb)
    {
        ha->next = pb;
    }
}
void PolyMul(Poly& P1,Poly &P2)
{
    Poly M[1000];
    int a, b,i=0;
    LNode *pb = P2->next,*pm=NULL;
    while (pb)
    {
        a = pb->elem.c; b = pb->elem.e;
        PolyInit(M[i]);
        PolyCopy(M[i], P1);
        pm = M[i]->next;
        while (pm)
        {
            pm->elem.c = pm->elem.c * a;
            pm->elem.e = pm->elem.e + b;
            pm = pm->next;
        }
        i++; pb = pb->next;
    }
    for (int j = 1; j < i; j++)
    {
        PolySum(M[0], M[j]);
    }
    PolyPrint(M[0]);
}
int main()
{
    Poly P1,P2,P3;
    PolyInit(P1);
    PolyCreate(P1);
    PolyInit(P2);
    PolyCreate(P2);
    PolyInit(P3);
    PolyCopy(P3, P1);

    PolyMul(P1, P2);//多项式乘法

    PolySum(P3, P2);//多项式加法
    PolyPrint(P3);
    return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值