通常在数学中对一元n次多项式可表示成如下的形式,设多项式的最高可能阶数为maxDegree,当前的最高阶数为n,各个项按指数递增的次序,从0到n顺序排列。
(1)一元n次多项式的数学式:
Pn(x)=a0+a1x+a2x2+a3x3+……+an−1xn−1+anxn=∑ni=0aixi
(2)一元n次多项式的存储表示:
编号 | 0 | 1 | 2 | 3 | …… | n | maxDegree |
---|---|---|---|---|---|---|---|
coef | a0 | a1 | a2 | a3 | …… | an | …… |
缺很多项而且各项指数跳动很大的一元多项式称为稀疏多项式。如果在存储多项式时只存储那些系数非零的项,可以节省存储空间。因此,对于每一个系数非零的项只存储它的系数 ai 和指数 ei ,并用一个存储各系数非零项的数组来表示这个多项式。其中,每个 ai 是 Pn(x) 中的非零系数,指数 ei 是递增的,即0≤ e0 < e1 <……< em
(1)一元多项式的数学式:
Pn(x)=a0xe0+a1xe1+a2xe2+a3xe3+……+amxem
(2)一元多项式的存储表示:
编号 | 0 | 1 | 2 | 3 | …… | i | …… | m | maxDegree |
---|---|---|---|---|---|---|---|---|---|
coef | a0 | a1 | a2 | a3 | …… | ai | …… | am | …… |
exp | e0 | e1 | e2 | e3 | …… | ei | …… | em | …… |
(1)文件:Term.h
#ifndef TERM_H_
#define TERM_H_
#include <iostream>
#include <string>
#include <strstream>
using namespace std;
struct Term //多项式结点的定义
{
float coef; //数据域——系数
int exp; //数据域——指数
Term *link; //指针域——后继指针
//初始化数据与指针成员的构造函数
Term(float c, int e, Term* ptr = NULL){ coef = c; exp = e; link = ptr; }
};
#endif /* TERM_H_ */
文件:Polynomial.h
#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_
#include "Term.h"
class Polynomial//带附加头结点
{
public:
Polynomial(); //构造函数
Polynomial(const Polynomial& L); //拷贝构造函数
~Polynomial(); //析构函数
public:
Term* Search(const int e)const; //搜索数据值为x的结点并返回
Term* InsertAfter(Term* lastNode, const float c, const int e); //后插法建立链表
void Output()const; //输出所有结点的数据值
void Sort(); //表排序——冒泡排序
void MakeEmpty(); //清空链表(保留附加头结点)
public:
Term* GetHead()const; //返回附加头结点的地址
int GetMaxOrder(); //获取最大阶数
private:
Term *first; //链表的头结点
};
//构造函数
Polynomial::Polynomial()
: first(new Term(0, -1))
{
cout << "$ 执行构造函数" << endl;
}
//拷贝构造函数
Polynomial::Polynomial(const Polynomial& L)
{
cout << "$ 执行拷贝构造函数" << endl;
Term *srcptr = L.GetHead();
Term *destptr = first = new Term(0, -1);
while (NULL != srcptr->link)
{
srcptr = srcptr->link;
destptr->link = new Term(srcptr->coef, srcptr->exp);
destptr = destptr->link;
}
}
//析构函数
Polynomial::~Polynomial()
{
cout << "$ 执行析构函数" << endl;
MakeEmpty();
}
//搜索数据值为x的结点并返回
Term* Polynomial::Search(const int e)const
{
Term *curNode = first->link;
while ((NULL != curNode) && (e != curNode->exp))
{
curNode = curNode->link;
}
return curNode;
}
//后插法建立链表
Term* Polynomial::InsertAfter(Term* lastNode, const float c, const int e)
{
if ((c == 0) || (e < 0))
{
return lastNode;
}
Term *curNode = Search(e);
if (NULL != curNode)
{
curNode->coef += c;
return lastNode;
}
Term *newNode = new Term(c, e);
lastNode->link = newNode;
return lastNode->link;
}
//输出所有结点的数据值
void Polynomial::Output()const
{
Term *curNode = first->link;
if (NULL != curNode)
{
cout << "Pn(x) = ";
}
while (NULL != curNode)
{
if ((first->link != curNode) && (curNode->coef > 0))
{
cout << "+";
}
if (0 == curNode->exp)
{
cout << curNode->coef;
}
else
{
cout << curNode->coef << "*x^" << curNode->exp;
}
curNode = curNode->link;
}
cout<<endl;
}
//表排序——冒泡排序
void Polynomial::Sort()
{
Term *curNode = first->link;
while (NULL != curNode)
{
Term *nextNode = curNode->link;
while (NULL != nextNode)
{
if (curNode->exp < nextNode->exp)
{
float coef_temp = curNode->coef;
curNode->coef = nextNode->coef;
nextNode->coef = coef_temp;
int exp_temp = curNode->exp;
curNode->exp = nextNode->exp;
nextNode->exp = exp_temp;
}
nextNode = nextNode->link;
}
curNode = curNode->link;
}
}
//清空链表(保留附加头结点)
void Polynomial::MakeEmpty()
{
Term *curNode = NULL;
while (NULL != first->link) //当链表不为空时,删去链表中所有结点
{
curNode = first->link; //保存被删结点
first->link = curNode->link; //将链表附加头结点中指针域的指针指向被删结点的下一个结点
delete curNode; //从链表上摘下被删结点
}
}
//返回附加头结点的地址
Term* Polynomial::GetHead()const
{
return first;
}
//获取最大阶数
int Polynomial::GetMaxOrder()
{
Sort();
if (NULL == first->link)
{
return -1;
}
return first->link->exp;
}
#endif /* POLYNOMIAL_H_ */
文件:PolynomialAdd.h
#ifndef POLYNOMIAL_ADD_H_
#define POLYNOMIAL_ADD_H_
#include "Polynomial.h"
Polynomial operator + (Polynomial& polynomial1, Polynomial& polynomial2)
{
Polynomial destpolynomial;
Term *destlastNode = destpolynomial.GetHead();
Term *curNode1 = polynomial1.GetHead()->link;
Term *curNode2 = polynomial2.GetHead()->link;
while ((NULL != curNode1) && (NULL != curNode2))
{
if (curNode1->exp == curNode2->exp)
{
if (0 != curNode1->coef + curNode2->coef)
{
destlastNode = destpolynomial.InsertAfter(destlastNode, curNode1->coef + curNode2->coef, curNode1->exp);
}
curNode1 = curNode1->link;
curNode2 = curNode2->link;
}
else if (curNode1->exp > curNode2->exp)
{
destlastNode = destpolynomial.InsertAfter(destlastNode, curNode1->coef, curNode1->exp);
curNode1 = curNode1->link;
}
else if (curNode1->exp < curNode2->exp)
{
destlastNode = destpolynomial.InsertAfter(destlastNode, curNode2->coef, curNode2->exp);
curNode2 = curNode2->link;
}
}
return destpolynomial;
}
#endif /* POLYNOMIAL_ADD_H_ */
文件:PolynomialMultiply.h
#ifndef POLYNOMIAL_MULTIPLY_H_
#define POLYNOMIAL_MULTIPLY_H_
#include "Polynomial.h"
Polynomial operator * (Polynomial& polynomial1, Polynomial& polynomial2)
{
Polynomial destpolynomial;
Term *destlastNode = destpolynomial.GetHead();
Term *curNode1 = polynomial1.GetHead()->link;
while (NULL != curNode1)
{
Term *curNode2 = polynomial2.GetHead()->link;
while (NULL != curNode2)
{
destlastNode = destpolynomial.InsertAfter(destlastNode, curNode1->coef * curNode2->coef, curNode1->exp + curNode2->exp);
curNode2 = curNode2->link;
}
curNode1 = curNode1->link;
}
return destpolynomial;
}
#endif /* POLYNOMIAL_MULTIPLY_H_ */
文件:main.cpp
#include "PolynomialAdd.h"
#include "PolynomialMultiply.h"
//返回输入的结点数据值
template <class T>
T get_value(const string& content)
{
string value;
cout << content;
cin >> value;
return StrToTtype<T>(value);
}
//类型转换——将string型转为模板类型T
template <class T>
T StrToTtype(const string& s_num)
{
T n_num;
strstream ss_num;
ss_num << s_num;
ss_num >> n_num;
return n_num;
}
//构造多项式
void construct_polynomial(Polynomial* linkedList)
{
Term *lastNode = linkedList->GetHead();
float coef = get_value<float>("newNode->coef = ");
int exp = get_value<int>("newNode->exp = ");
while ((coef != 0) && (exp >= 0))
{
lastNode = linkedList->InsertAfter(lastNode, coef, exp);
coef = get_value<float>("newNode->coef = ");
exp = get_value<int>("newNode->exp = ");
}
linkedList->Sort();
linkedList->Output();
}
int main(int argc, char* argv[])
{
Polynomial *polynomial1 = new Polynomial;
construct_polynomial(polynomial1);
Polynomial *polynomial2 = new Polynomial;
construct_polynomial(polynomial2);
Polynomial adddest = (*polynomial1) + (*polynomial2);
adddest.Output();
Polynomial multiplydest = (*polynomial1) * (*polynomial2);
multiplydest.Output();
delete polynomial1;
delete polynomial2;
system("pause");
return 0;
}
参考文献:
[1]《数据结构(用面向对象方法与C++语言描述)(第2版)》殷人昆——第二章
[2]《C/C++常用算法手册》秦姣华、向旭宇——第六章
[3] 百度搜索关键字:多项式、多项式的运算