相乘->排序->合并重复指数项->去系数为0项
#include<iostream>
using namespace std;
typedef int Status;
typedef char ElemType;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
struct Poly
{
int coef; //系数
int exp; //指数
Poly* next;
//有参构造函数,默认参数为0,0,nullptr
Poly(int coef = 0, int exp = 0);
//重载<<用于输出结点信息
friend ostream& operator<<(ostream& os, const Poly* p);
//重载*用于结点相乘
Poly* operator*(Poly* p1);
//重载>用于比较两个结点大小
bool operator>(Poly* p1);
//重载+用于结点相加(系数相加),并释放被加的结点的空间
void operator+(Poly* p1);
};
//创建多项式,多项式为空返回false
Status Create(Poly* La);
//打印多项式
Status Display(const Poly* La);
//多项式乘法
Status Mul(Poly* L1, Poly* L2, Poly* ans);
//插入排序对链表排序
Status InsertSort(Poly* L);
//合并同指数的点
Status Merge(Poly* L);
//去掉系数为0的点
Status Standard(Poly* L);
int main()
{
Poly* La = new Poly;
Poly* Lb = new Poly;
Poly* ans = new Poly;
//禁止多项式为空!必须建立
do
{
cout << "请输入多项式A(以 0 0 结束):\n";
} while (!Create(La));
do
{
cout << "请输入多项式B(以 0 0 结束):\n";
} while (!Create(Lb));
if (Mul(La, Lb, ans))
{
cout << "多项式A和B相乘后的结果是:\n";
Display(ans);
}
if (InsertSort(ans))
{
cout << "按指数排序后的结果是:\n";
Display(ans);
}
if (Merge(ans))
{
cout << "合并重复指数项之后的结果是:\n";
Display(ans);
}
if (Standard(ans))
{
cout << "删除系数为0的项后的结果是:\n";
Display(ans);
}
}
Poly::Poly(int coef, int exp)
{
this->coef = coef;
this->exp = exp;
next = nullptr;
}
ostream& operator<<(ostream& os, const Poly* p)
{
//输出
os << '(' << p->coef << ")*x^" << p->exp;
return os;
}
Poly* Poly::operator*(Poly* p1)
{
//系数相乘,指数相加
Poly* p = new Poly(coef * p1->coef, exp + p1->exp);
return p;
}
bool Poly::operator>(Poly* p1)
{
//按指数大小排序,指数相同按系数大小排序
if (this->exp == p1->exp)
{
return this->coef > p1->coef;
}
return this->exp > p1->exp;
}
void Poly::operator+(Poly* p1)
{
//系数相加
coef += p1->coef;
this->next = p1->next;
delete(p1);
}
Status Create(Poly* La)
{
auto p = La;
int i = 1;
while (true)
{
int coef, exp;
cout << "请输入第" << i << "项的系数和指数:\n";
cin >> coef >> exp;
if (coef == 0 && exp == 0)
{
break;
}
i++;
//构造函数建立结点,并置next为nullptr,尾插法
Poly* q = new Poly(coef, exp);
p->next = q;
p = p->next;
}
//如果多项式为空返回ERROR
if (i == 1)
{
cout << "多项式为空" << endl;
return ERROR;
}
return OK;
}
Status Display(const Poly* La)
{
La = La->next;
//重载后的<<运算符输出结点,key控制第一次输出前没有+号
bool key = false;
while (La)
{
if (key)
{
cout << '+' << La;
}
else
{
cout << La;
key = true;
}
La = La->next;
}
cout << endl;
return OK;
}
Status Mul(Poly* L1, Poly* L2, Poly* ans)
{
if (L1->next == nullptr || L1->next == nullptr)
{
cout << "有一个多项式为空,无法相乘" << endl;
return ERROR;
}
L1 = L1->next;
auto p = L2->next;
auto q = ans;
while (L1)
{
while (p)
{
//通过重载后的*进行结点间的计算,并将结果尾插入ans
q->next = (*L1) * p;
q = q->next;
p = p->next;
}
p = L2->next;
L1 = L1->next;
}
return OK;
}
Status InsertSort(Poly* L)
{
if (L->next == nullptr)
{
cout << "多项式为空,无法排序" << endl;
return ERROR;
}
//递减排序
auto p = L->next;
auto q = p->next;
auto key = L;
while (q)
{
//借用重载后的>符号进行判断
if (*q > p)
{
//找到最后一个比他大的点
while (*(key->next) > q)
{
key = key->next;
}
//将q结点从链表摘下插入key后
p->next = q->next;
q->next = key->next;
key->next = q;
q = p->next;
key = L;
}
else
{
p = p->next;
q = q->next;
}
}
return OK;
}
Status Merge(Poly* L)
{
if (L->next == nullptr)
{
cout << "多项式为空,无法合并指数项" << endl;
return ERROR;
}
auto p = L->next;
while (p)
{
auto q = p->next;
//找到指数相同的结点,找到就指数相加,并且释放当前结点(以上全部由重载后的+实现)
while (q && q->exp == p->exp)
{
*p + q;
//判断下一个结点是否还是指数相同
q = p->next;
}
p = p->next;
}
if (L->next == nullptr)
{
cout << "多项式合并重复指数项之后已经为空";
return ERROR;
}
return OK;
}
Status Standard(Poly* L)
{
if (L->next == nullptr)
{
cout << "多项式为空,无法删除系数为0的项" << endl;
return ERROR;
}
auto p = L;
while (L->next)
{
//如果后续结点系数为0,则删除此结点,注意此时L不用后指
if (L->next->coef == 0)
{
auto p = L->next;
L->next = p->next;
delete(p);
}
else
{
L = L->next;
}
}
if (p->next == nullptr)
{
cout << "多项式删除系数为0的项后已经为空\n";
return ERROR;
}
return OK;
}