#include<iostream>
#include<list>
using namespace std;
typedef struct PNode{//结构体定义
int coef;//系数
int expn;//指数
}Polynomial;
class myCompare{//类判断
public:
bool operator()(Polynomial p1, Polynomial p2){
return p1.expn < p2.expn;
}
};
void printList(const list<Polynomial>&L){//输出
for(list<Polynomial>::const_iterator it = L.begin(); it != L.end(); it++){
if(it == L.begin()){//对首位进行判断
if(it->expn == 0) cout << it->coef;//指数为0
else cout << it->coef << "x^" << it->expn;
}
else{
if(it->coef > 0) cout << "+" << it->coef << "x^" << it->expn;
else if(it->coef < 0) cout << it->coef << "x^" << it->expn;
else continue;//系数为 0, 不输出
}
}
cout << endl;
}
void create(list<Polynomial>&L, int n){//链表的创建
while(n--){
cout << "请输入项的系数和指数" << endl;
Polynomial temp;
cin >> temp.coef >> temp.expn;
L.push_back(temp);
}
L.sort(myCompare());//list 容器并不提供随机访问迭代器,只提供双向迭代器,因此不能对 list 中
//的元素使用 sort() 算法。但是,还是可以进行元素排序,因为 list 模板定义了自己的 sort() 函数。
}
void opposite(list<Polynomial>&L){//对链表中的系数求反
for(list<Polynomial>::iterator it = L.begin(); it != L.end(); it++)
it->coef = -it->coef;
}
void add(list<Polynomial>&L1, list<Polynomial>&L2){//加法
list<Polynomial>::iterator vit = L2.begin();
for(list<Polynomial>::iterator it = L1.begin(); ;){
if(it->expn == vit->expn){//指数相同
it->coef += vit->coef;
it++; vit++;
}
else if(it->expn > vit->expn){//
L1.push_back(*vit);
vit++;
}else{// (it->expn < vit->expn)
it++;
}
if(vit == L2.end()) break;
}
L1.sort(myCompare());
}
void addOrSubstract(list<Polynomial>L1, list<Polynomial>L2, int flag){
//对加减法的判断与最终结果形式输出
//具体见主代码
if(flag) opposite(L2);
add(L1, L2);
if(!flag) printList(L1);
else{
if(flag == 1) printList(L1);
else{
opposite(L1); printList(L1);
}
}
}
void multip(list<Polynomial>L1, list<Polynomial>L2){//乘法
list<Polynomial>L4;
for(list<Polynomial>::iterator it = L1.begin(); it != L1.end(); it++){
list<Polynomial>L3;//临时
for(list<Polynomial>::iterator vit = L2.begin(); vit != L2.end(); vit++){
Polynomial temp;
temp.coef = it->coef * vit->coef;
temp.expn = it->expn + vit->expn;
L3.push_back(temp);
}
L3.sort(myCompare());
if(L4.size() == 0){//为空,直接取L3
L4.assign(L3.begin(), L3.end());
continue;
}
if(L4.back().expn > L3.back().expn)//逐行相加
add(L4, L3);
else{
add(L3, L4);
L4.assign(L3.begin(), L3.end());//L4是结果,L3是当前阶段的加值
}
}
printList(L4);
}
int main(){
list<Polynomial> L1,L2;
int flag = 0;
while(1){
if(flag) break;
cout << "-----------------------------" << endl;
cout << "--------1.创建多项式---------" << endl;
cout << "--------2.多项式相加---------" << endl;
cout << "--------3.多项式相减---------" << endl;
cout << "--------4.多项式相乘---------" << endl;
cout << "--------0.退出---------------" << endl;
cout << "-----------------------------" << endl;
cout << "请输入选项:" << endl;
int choice;
cin >> choice;
switch(choice){
case 1://1.创建多项式
cout << "请输入项数:" << endl;
int n;
cin >> n;
create(L1, n);
cout << "第一个多项式为:" << endl;
printList(L1);
cout << "请输入项数:" << endl;
cin >> n;
create(L2, n);
cout << "第二个多项式为:" << endl;
printList(L2);
break;
case 2://2.多项式相加
cout << "多项式相加结果为:" << endl;
if(L1.back().expn > L2.back().expn)//要考虑最高项的影响,可能出现L1运行完,但L2没有
addOrSubstract(L1, L2, 0);
else
addOrSubstract(L2, L1, 0);
break;
case 3://3.多项式相减
cout << "多项式相减结果为:" << endl;
if(L1.back().expn > L2.back().expn)
addOrSubstract(L1, L2, 1);
else
addOrSubstract(L2, L1, 2);
break;
case 4://4.多项式相乘
cout << "多项式相乘结果为:" << endl;
multip(L1, L2);
break;
case 0://0.退出
flag = 1;
break;
}
}
return 0;
}