1--多项式加法
传入存储两个多项式的 List 链表,使用迭代器进行遍历,比较其指数的三种情况,将结果存储到新的多项式 List 链表之中;
std::list<PolyNode> PolyAdd(std::list<PolyNode> P1, std::list<PolyNode> P2){
std::list<PolyNode> P3;
std::list<PolyNode>::iterator p1 = P1.begin();
std::list<PolyNode>::iterator p2 = P2.begin();
PolyNode tmp(0, 0);
while(p1 != P1.end() && p2 != P2.end()){
switch(compare(p1->expon, p2->expon)){
case 1:
P3.push_back(*p1);
p1++;
break;
case -1:
P3.push_back(*p2);
p2++;
break;
case 0:
tmp.coef = p1->coef + p2->coef;
tmp.expon = p1->expon;
p1++;
p2++;
if(tmp.coef == 0){
break;
}
P3.push_back(tmp);
break;
}
}
for(; p1 != P1.end(); p1++) P3.push_back(*p1);
for(; p2 != P2.end(); p2++) P3.push_back(*p2);
return P3;
}
2--多项式乘法
传入存储两个多项式的 List 链表,使用双重循环计算乘积;
判断结果表达式是否存在,不存在则直接插入新链表中,存在则进行合并;
最后对结果多项式进行排序,并返回;
std::list<PolyNode> PolyMul(std::list<PolyNode> P1, std::list<PolyNode> P2){
std::list<PolyNode> P3;
std::list<PolyNode>::iterator p1 = P1.begin();
std::list<PolyNode>::iterator p2 = P2.begin();
PolyNode tmp(0, 0);
while(p1 != P1.end()){
while(p2 != P2.end()){
tmp.coef = p1->coef * p2->coef;
tmp.expon = p1->expon + p2->expon;
std::list<PolyNode>::iterator it = find(P3.begin(), P3.end(), tmp);
if(it != P3.end()){
it->coef += tmp.coef;
if (it->coef == 0) P3.erase(it);
}
else P3.push_back(tmp);
p2++;
}
p1++;
p2 = P2.begin();
}
P3.sort(MyCompare());
return P3;
}
3--测试代码
控制台输入多项式时,第一个数字表示项数N,接着每两个数字表示对应项式的系数和指数;
STL提供的 List 容器,不能使用标准的 sort 算法,需要使用容器内置的排序算法;
#include "iostream"
#include <list>
#include "algorithm"
#include "functional"
class PolyNode{
public:
PolyNode(int c, int e){
this->coef = c;
this->expon = e;
}
// 重载 == 号, 便于find()函数进行比较
bool operator==(const PolyNode &p){
if(this->expon == p.expon) return true;
else return false;
}
int coef;
int expon;
};
void ReadPolyNode(std::list<PolyNode> &P){
int N;
int c;
int e;
PolyNode tmp(0, 0);
std::cin >> N;
while(N--){
std::cin >> c >> e;
tmp.coef = c;
tmp.expon = e;
P.push_back(tmp);
}
}
class MyCompare{
public:
bool operator()(const PolyNode &p1, const PolyNode &p2){
return p1.expon > p2.expon;
}
};
int compare(int A, int B){
if(A > B) return 1;
else if(A < B) return -1;
else return 0;
}
std::list<PolyNode> PolyAdd(std::list<PolyNode> P1, std::list<PolyNode> P2){
std::list<PolyNode> P3;
std::list<PolyNode>::iterator p1 = P1.begin();
std::list<PolyNode>::iterator p2 = P2.begin();
PolyNode tmp(0, 0);
while(p1 != P1.end() && p2 != P2.end()){
switch(compare(p1->expon, p2->expon)){
case 1:
P3.push_back(*p1);
p1++;
break;
case -1:
P3.push_back(*p2);
p2++;
break;
case 0:
tmp.coef = p1->coef + p2->coef;
tmp.expon = p1->expon;
p1++;
p2++;
if(tmp.coef == 0){
break;
}
P3.push_back(tmp);
break;
}
}
for(; p1 != P1.end(); p1++) P3.push_back(*p1);
for(; p2 != P2.end(); p2++) P3.push_back(*p2);
return P3;
}
std::list<PolyNode> PolyMul(std::list<PolyNode> P1, std::list<PolyNode> P2){
std::list<PolyNode> P3;
std::list<PolyNode>::iterator p1 = P1.begin();
std::list<PolyNode>::iterator p2 = P2.begin();
PolyNode tmp(0, 0);
while(p1 != P1.end()){
while(p2 != P2.end()){
tmp.coef = p1->coef * p2->coef;
tmp.expon = p1->expon + p2->expon;
std::list<PolyNode>::iterator it = find(P3.begin(), P3.end(), tmp);
if(it != P3.end()){
it->coef += tmp.coef;
if (it->coef == 0) P3.erase(it);
}
else P3.push_back(tmp);
p2++;
}
p1++;
p2 = P2.begin();
}
P3.sort(MyCompare());
return P3;
}
void MyPrint(PolyNode p){
std::cout << "coef: " << p.coef << " expon: " << p.expon << std::endl;
}
int main(int argc, char* argv[]){
std::list<PolyNode> P1;
std::list<PolyNode> P2;
ReadPolyNode(P1);
ReadPolyNode(P2);
std::cout << "Add:--------------------" << std::endl;
std::list<PolyNode> P3 = PolyAdd(P1, P2);
for_each(P3.begin(), P3.end(), MyPrint);
std::cout << "Mul:--------------------" << std::endl;
std::list<PolyNode> P4 = PolyMul(P1, P2);
for_each(P4.begin(), P4.end(), MyPrint);
}