您的位置:首页 > 理论基础 > 数据结构算法

数据结构多项式相加和相乘

2014-05-05 19:46 316 查看
这是数据结构的实验~~
由于某些实验要上交,所以一部分的程序的代码改为英语注释了
这是求解多项式相加和相乘的算法实现。
采用线性表就可以实现了。
代码可能有点繁杂,因为线性表的实现也包括在里面了。

Experiment1_1.h ,算法的实现的头文件。
[cpp] view plaincopy
/* Experiment1_1.h */

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//  实验题1.1  问题描述:
//  有两个指数递减的一元多项式,写一程序先求这两个多项式的和,再求它们的积。
//
//  提示 1 :
//  用带表头结点的单链表作为多项式的存储表示;
//  要建立两个单链表;
//  多项式相加就是要把一个单链表中的结点插入到另一个单链表中去,要注意插入、删除操作中指针的正确修改。
//  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#include <iostream>
using namespace std;

// - - - - base constant value - - - - -
const int OK    = 1;
const int FAIL  = 0;
const int EQUAL = 1;    // equal to
const int BIG   = 2;    // bigger than
const int LESS  = 3;    // less than

// - - - - data structure - - - - -

// element of Polynomial
typedef struct
{
float coef;
int   expn;
}term, ElemType;

// node of linklist
typedef struct LinkList
{
term e;
struct LinkList* next;
}LinkList, LNode, Polynomial;

// - - - - - - base functions - - - - - -

int  InitList     (LinkList*& L);
int  SetCurElem   (LNode*& h, const ElemType e);
int  MakeNode     (LNode*& s, const ElemType e);
int  InsFirst     (LNode*& q, LNode*& s);
int  compare      (const term a, const term b);
int  LocateElem   (LinkList* L, const ElemType e, LNode*& q);
void CreatPolyn   (Polynomial*& P, const int m);
void DestroyPolyn (Polynomial*& P);
void CopyPolyn    (Polynomial*& P, const Polynomial* copy);
int  PrintPolyn   (const Polynomial* P);
void AddPolyn     (Polynomial*& Pa, Polynomial*& Pb);
void MutilplyPolyn(Polynomial*& Pa, Polynomial*& Pb);

// add pa to pb and destroy pb
void AddPolyn(Polynomial*& Pa, Polynomial*& Pb)
{
// aCur point to the current node of pa
// aPrior point to the prior node of aCur
// bCur point to the current node of pb
// tmp pointer used temporary
LNode* aCur;
LNode* aPrior;
LNode* bCur;
LNode* tmp;

// initialize the pointers
aPrior = Pa;
aCur = aPrior->next;
bCur = Pb->next;

// result save the result of the compare function
// forDelete point to the node to be deleted
int result;
LNode* forDelete;

// polynomial addition
while (aCur && bCur)
{
result = compare(aCur->e, bCur->e);
switch(result)
{
// add the b's coef to a
case EQUAL:
aCur->e.coef += bCur->e.coef;

forDelete = bCur;
bCur = bCur->next;
delete forDelete;

if (aCur->e.coef == 0.0)
{
forDelete = aCur;
aCur = aCur->next;
aPrior->next = aCur;
delete forDelete;
}
break;
// aCur point to the next node
case BIG:
aPrior = aPrior->next;
aCur = aPrior->next;
break;
// insert bCur to aCur's prior node
case LESS:
tmp = bCur;
bCur = bCur->next;
aPrior->next = tmp;
tmp->next = aCur;
aPrior = aPrior->next;
break;
default:
break;
}
}
// if aCur=NULL then add b's rest nodes to the rear of a
if (!aCur)
{
aPrior->next = bCur;
}

delete Pb;
}

// multiply b to a, and destroy b
void MutilplyPolyn(Polynomial*& Pa, Polynomial*& Pb)
{
// Pa = A(x) = sum(ai * x ^ aei), i=1...m;
// Pb = B(x) = sum(bi * x ^ bei), i=1...n;
// Pa*Pb = sum(bi * A(x) * x^bei) = sum( sum((aj * bi) * x^(aej+bei)), j=1...m ), i=1...n;

// backupPa save the copy of Pa
// backupPa = A(x) = sum(ai * x ^ aei), i=1...m;
// tmp memorize the intermediate result
// tmp = sum((aj * bi) * x^(aej+bei)), j=1...m
Polynomial* backupPa;
Polynomial* tmp;

// aCur point to backupPa
LNode* aCur;
LNode* bCur;
LNode* tmpCur;

// copy pa to backupPa and tmp
CopyPolyn(backupPa, Pa);
CopyPolyn(tmp, Pa);

// preprocessing Pa:first destroy Pa then save the first result in Pa
bCur = Pb->next;
aCur = backupPa->next;
tmpCur = tmp->next;
while (aCur)
{
tmpCur->e.coef = bCur->e.coef * aCur->e.coef;
tmpCur->e.expn = bCur->e.expn + aCur->e.expn;
aCur = aCur->next;
tmpCur = tmpCur->next;
}
bCur = bCur->next;
DestroyPolyn(Pa);
CopyPolyn(Pa, tmp);

// multiply the two polynomial,add tmp to Pa when finish a intermediate result
while (bCur)
{
CopyPolyn(tmp, Pa);
aCur = backupPa->next;
tmpCur = tmp->next;
while (aCur)
{
tmpCur->e.coef = bCur->e.coef * aCur->e.coef;
tmpCur->e.expn = bCur->e.expn + aCur->e.expn;
aCur = aCur->next;
tmpCur = tmpCur->next;
}
AddPolyn(Pa, tmp);
bCur = bCur->next;
}

DestroyPolyn(Pb);
}

// initialize the linklist
int InitList(LinkList*& L)
{
L = new LinkList;
if (!L)
{
return FAIL;
}
L->next = NULL;
return OK;
}

// set the node h to the value e
int SetCurElem(LNode*& h, const ElemType e)
{
if (!h)
{
return FAIL;
}
else
{
h->e.coef = e.coef;
h->e.expn = e.expn;
return OK;
}
}

// make a node using value e, use the pointer s to point to the node
int MakeNode(LNode*& s, const ElemType e)
{
s = new LNode;
if (!s)
{
return FAIL;
}
else
{
s->e.coef = e.coef;
s->e.expn = e.expn;
s->next = NULL;
}
return OK;
}

// to insert the node s after node q
int InsFirst(LNode*& q, LNode*& s)
{
if (q == NULL || s == NULL)
{
return FAIL;
}
else
{
s->next = q->next;
q->next = s;
return OK;
}
}

// compare two term
int compare(const term a, const term b)
{
if (a.expn == b.expn)
{
return EQUAL;
}
else if (a.expn > b.expn)
{
return BIG;
}
else
{
return LESS;
}
}

// locate the value e, if successed, return OK and q point to the position;
// else return FAIL and q point to the position which node first less than e
int LocateElem(LinkList* L, const ElemType e, LNode*& q)
{
LinkList* h;
h = L->next;
q = L;
int result;
while (h)
{
result = compare(h->e, e);
if (result == EQUAL)
{
q = h;
return OK;
}
else if (result == LESS)
{
return FAIL;
}
else
{
h = h->next;
q = q->next;
}
}
return FAIL;
}

// create the polunomial with the input value
void CreatPolyn(Polynomial*& P, const int m)
{
InitList(P);

term e;
e.coef = 0.0;
e.expn = -1;
SetCurElem(P, e);

int i;
LNode* q;
LNode* s;
for (i=1; i<=m; i++)
{
cout<<"/ - ";
cin>>e.coef>>e.expn;
if (e.coef == 0.0)
{
continue;
}
if (LocateElem(P, e, q) == FAIL)
{
if (MakeNode(s, e))
{
InsFirst(q, s);
}
}
}
}

// destroy the polynomial
void DestroyPolyn(Polynomial*& P)
{
if (P)
{
LNode* q;
LNode* s;
q = P;
while (q->next)
{
s = q;
q = q->next;
delete s;
}
delete q;
}
}

// print the polynomial
int PrintPolyn(const Polynomial* P)
{
if (!P)
{
return FAIL;
}
LNode* s;
s = P->next;
if (s)
{
cout<<s->e.coef<<"*X^"<<s->e.expn;
s = s->next;
}
while (s)
{
cout<<" + "<<s->e.coef<<"*X^"<<s->e.expn;
s = s->next;
}
return OK;
}

// copy the polynomial 'copy' to the polynomial p
void CopyPolyn(Polynomial*& P, const Polynomial* copy)
{
InitList(P);

term e;
e.coef = 0.0;
e.expn = -1;
SetCurElem(P, e);

LNode* q;
LNode* r;
LNode* s;
r = P;
s = copy->next;
while (s)
{
q = new LNode;
q->e.coef = s->e.coef;
q->e.expn = s->e.expn;
q->next = NULL;
r->next = q;
r = r->next;

s = s->next;
}
}

test.cpp,应用算法完成多项式的加法和乘法
[cpp] view plaincopy
/* test.cpp */

#include "Experiment1_1.h"

void main()
{
Polynomial* P1;
Polynomial* P2;
Polynomial* backupP1;
Polynomial* backupP2;
int m1;
int m2;

cout<<"/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /"<<endl;
cout<<"/ -            实验题1.1  求两多项式的和,积              - /"<<endl;
cout<<"/ -                                                       - /"<<endl;
cout<<"/ -    输入规则:系数 指数                                - /"<<endl;
cout<<"/ -    例:多项式:3 * x ^ 5,输入3 5                     - /"<<endl;
cout<<"/ -                                                       - /"<<endl;
cout<<"/ -    注:各项的指数不能相同,否则会忽略相同项           - /"<<endl;
cout<<"/ -    注:系数不能为0,否则会忽略该项                    - /"<<endl;
cout<<"/ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /"<<endl;

cout<<endl;
cout<<"/ - 输入多项式P1的项数:";
cin>>m1;
cout<<"/ - 输入多项式的数据:"<<endl;
CreatPolyn(P1, m1);
cout<<"/ - P1 = ";
PrintPolyn(P1);
cout<<endl;
cout<<endl;

cout<<"/ - 输入多项式P2的项数:";
cin>>m2;
cout<<"/ - 输入多项式的数据:"<<endl;
CreatPolyn(P2, m2);
cout<<"/ - P2 = ";
PrintPolyn(P2);
cout<<endl;
cout<<endl;

CopyPolyn(backupP1, P1);
CopyPolyn(backupP2, P2);

cout<<"/ - P1 + P2 = ";
AddPolyn(P1, P2);
PrintPolyn(P1);
cout<<endl;
cout<<endl;
DestroyPolyn(P1);

cout<<"/ - P1 * P2 = ";
MutilplyPolyn(backupP1, backupP2);
PrintPolyn(backupP1);
cout<<endl;
cout<<endl;

DestroyPolyn(backupP1);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: