#include"iostream"
using namespace std;
struct Node {
float coef;//多项式系数
int expn;//指数
Node *next;//<T>可省略
Node(float _coef = 0,int _expn = 0, Node*_next = NULL) {
coef = _coef;
expn = _expn;
next = _next;
}
};
class PolyLinklist{
private:
Node*front;
public:
PolyLinklist() { front = new Node; front->next = NULL; front->coef = 0; front->expn = 0; }//默认构造函数
PolyLinklist(float a[],int b[], int n);//多项式类的构造函数
PolyLinklist(PolyLinklist&a) { front = new Node; front->next=a.GetFront()->next;};//复制构造函数
void append(float c,int e);//在末尾插入元素
void add(PolyLinklist &a);//相加函数
void minus(PolyLinklist&a);//相减函数
void Derivation();//求导求导函数
void PrintList();//遍历输出多项式
void ReadPoly();//输入多项式
double cal(double x = 0);//计算
Node*GetFront() { return front; }//返回对应的头结点
void clear();//清空类
};
PolyLinklist::PolyLinklist(float a[],int b[], int n)
{
front = new Node;
Node*p = NULL;
for (int k = n - 1; k >= 0; k--)
{
p = new Node;
p->expn = b[k];
p->coef = a[k];
p->next = front->next;
front->next = p;
}
}
void PolyLinklist::add(PolyLinklist &b) {
Node*p_prior = front;
Node*p = p_prior->next;
Node*q = b.GetFront()->next;
while (p&&q) {
if (p->expn < q->expn)//当节点的指数p<q时
{
p_prior = p;
p = p->next;
}
else if (p->expn > q->expn)//指数p>q时
{
p_prior->next = q;
p_prior = q;
q = q->next;
p_prior->next = p;
}
else {
p->coef += q->coef;
if (p->coef == 0)//系数相加为零时,删除
{
p_prior->next = p->next;
delete p;
p = p_prior->next;
}
else {//不为零时,p向下移动
p_prior = p;
p = p_prior->next;
}
q = q->next;
}
}
if (q) {//当b的项数大于对应的a的项数时
while (q) {
Node*temp = new Node;
p_prior->next = temp;
p_prior = temp;
temp->expn = q->expn;
temp->coef = q->coef;
q = q->next;
}
}
}
void PolyLinklist::minus(PolyLinklist&b)
{
Node*p_prior = front;
Node*p = p_prior->next;
Node*q = b.GetFront()->next;
while (p&&q) {
if (p->expn < q->expn)
{
p_prior = p;
p = p->next;
}
else if (p->expn > q->expn)
{
p_prior->next = q;
p_prior = q;
q = q->next;
p_prior->next = p;
}
else {
p->coef -= q->coef;
if (p->coef == 0)
{
p_prior->next = p->next;
delete p;
p = p_prior->next;
}
else {
p_prior = p;
p = p_prior->next;
}
q = q->next;
}
}
if (q) {
while (q) {
Node*temp = new Node;
p_prior->next = temp;
p_prior = temp;
temp->expn = q->expn;
temp->coef = -q->coef;
q = q->next;
}
}
}
void PolyLinklist::PrintList() {
Node*p =front->next;
while (p) {
cout << "(" << p->coef << "," << p->expn << ")" ;
p = p->next;
}
cout << endl;
}
void PolyLinklist::Derivation() {
Node*p_front = front;
Node*p = p_front->next;
while (p)
{
if (p->expn == 0)//当对应节点为常数时,删去
{
p_front->next = p->next;
Node*temp = p;
p = p->next;
delete temp;
}
p->coef *= p->expn;//对于非常数的项,进行对应的求导运算法则
p->expn -= 1;
p = p->next;
}
}
void PolyLinklist::append(float c,int e) {
Node*rear = front;
while (rear->next){rear = rear->next;}
Node*s = new Node;
s->coef = c;
s->expn = e;
rear->next = s;
s->next = NULL;
}
void PolyLinklist::ReadPoly()
{
cout << "输入多项式的项数" << endl;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
float c; int e;
cin >> c >> e;
append(c, e);
}
}
double PolyLinklist::cal(double x) {//计算值
Node*p = front->next;
double sum = 0;
while (p)
{
sum += p->coef*pow(x, p->expn);
p = p->next;
}
return sum;
}
void PolyLinklist::clear() {//清空
Node*temp = GetFront()->next;
while (temp)
{
Node*p = temp->next;
delete temp;
temp = p;
}
GetFront()->next = NULL;
}
PolyLinklist Mul(PolyLinklist&a,PolyLinklist&b) {//乘法
Node*p = a.GetFront()->next;
PolyLinklist clist;
while (p)
{
Node*q = b.GetFront()->next;
PolyLinklist dlist;//生成对应的添加元素对象
while (q) {
float coef = (p->coef)*(q->coef);
int expn = (p->expn) + (q->expn);
dlist.append(coef, expn);
q = q->next;
}
clist.add(dlist);//两个对象相加
dlist.clear();//对dlist清空,确保不会影响下一次内循环
p = p->next;
}
return clist;//返回对象
}
int main()
{
float a[3] = { 1,2,3 }, c[4] = { 1,2,3,4 };
int b[3] = { 1,3,4 }, d[4] = { 1,3,4,5 };
PolyLinklist alist(a,b,3), blist(c,d,4);
cout << "alist" << endl;
alist.PrintList();
cout << "blist" << endl;
blist.PrintList();
cout << "alist,blist相加" << endl;
alist.add(blist);
alist.PrintList();
cout << "alist计算" << endl;
cout << alist.cal() << endl;
cout << alist.cal(2) << endl;
cout << "alist求导" << endl;
alist.Derivation();
alist.PrintList();
cout << "alist,blist求乘" << endl;
PolyLinklist clist = Mul(alist, blist);
clist.PrintList();
PolyLinklist dlist;
dlist.ReadPoly();
dlist.PrintList();
return 0;
}
c++数据结构试验用链表实现多项式类
最新推荐文章于 2023-03-04 16:03:26 发布