#include <iostream>
class Node
{
public:
int a;//系数
double b;//指数
bool is_used = false;//合并的时候判断是否用过
Node() = default;
~Node() = default;
Node* next;
void creat(Node*L,int n);
void destroy(Node*L);
void visit(Node*L);
};
//根据多项式的长度按照指数升序的顺序创建
void Node::creat(Node*L,int n){
Node*p = L;
int t = n;
while(t--){
int _a;
double _b;
std::cin>>_a>>_b;
if(t==n-1){
p->a = _a;
p->b = _b;
}
else{
Node* q = new Node();
q->a = _a;
q->b = _b;
p->next = q;
p = q;
}
}
}
//销毁
void Node::destroy(Node*L){
Node*p = L->next;
while(L->next){
Node*p = L->next;
delete L;
L = p;
}
if(L){
delete L;
}
}
//访问
void Node::visit(Node*L){
while(L){
if(L->a==0){
L = L->next;
continue;
}
std::cout<<L->a<<"x"<<L->b;
if(L->next){
std::cout<<" + ";
}
L = L->next;
}
}
//去重合并
void merge(Node*a,Node*b){
Node*p1 = a;
Node*p3 = b;
int cnt = 0;
while(p1){
if(p1->is_used){
p1 = p1->next;
continue;
}
Node*p2 = p1->next;
if(!cnt){
p3->a=p1->a;
p3->b=p1->b;
while(p2){
if(p2->is_used){
continue;
}
if(p2->b==p1->b){
p3->a+=p2->a;
p2->is_used = true;
}
p2 = p2->next;
}
cnt++;
if(p3->a==0){
cnt--;
}
}
else{
Node*p = new Node();
p->a = p1->a;
p->b = p1->b;
while(p2){
if(p2->is_used){
continue;
}
if(p2->b==p1->b){
p->a+=p2->a;
p2->is_used = true;
}
p2 = p2->next;
}
if(p->a){
p3->next = p;
p3 = p;
}
}
p1 = p1->next;
}
}
//相加
void add(Node*a,Node*b,Node*c){
Node*p1 = a;
Node*p2 = b;
Node*p3 = c;
while(p1&&p2){
if(p1->b==p2->b){
p3->a = p1->a+p2->a;
p3->b = p1->b;
p1 = p1->next;
p2 = p2->next;
Node*p = new Node();
p3->next = p;
p3 = p;
}
else if(p1->b<p2->b){
p3->a = p1->a;
p3->b = p1->b;
Node*p = new Node();
p3->next = p;
p1 = p1->next;
p3 = p;
}
else{
p3->a = p2->a;
p3->b = p2->b;
Node*p = new Node();
p3->next = p;
p2 = p2->next;
p3 = p;
}
}
while(p1){
Node*p = new Node();
p->a = p1->a;
p->b = p1->b;
p3->next = p;
p3 = p;
p1 = p1->next;
}
while(p2){
Node*p = new Node();
p->a = p2->a;
p->b = p2->b;
p3->next = p;
p3 = p;
p2 = p2->next;
}
}
//相乘
void multiple(Node*a,Node*b,Node*c){
Node*p1 = a;
Node*p3 = new Node();
Node*p4 = p3;
int cnt = 0;
while(p1){
Node*p2 = b;
while(p2){
if(!cnt){
p3->a = p1->a*p2->a;
p3->b = p1->b+p2->b;
p2 = p2->next;
cnt++;
}
else{
Node*p = new Node();
p->a = p1->a*p2->a;
p->b = p1->b+p2->b;
p2 = p2->next;
p3->next = p;
p3 = p;
}
}
p1 = p1->next;
}
merge(p4,c);
}
//相减
void sub(Node*a,Node*b,Node*c){
Node*p1 = a;
Node*p2 = b;
Node*p3 = c;
while(p1&&p2){
if(p1->b==p2->b){
p3->a = p1->a-p2->a;
p3->b = p1->b;
p1 = p1->next;
p2 = p2->next;
Node*p = new Node();
p3->next = p;
p3 = p;
}
else if(p1->b<p2->b){
p3->a = p1->a;
p3->b = p1->b;
Node*p = new Node();
p3->next = p;
p1 = p1->next;
p3 = p;
}
else{
p3->a = -p2->a;
p3->b = p2->b;
Node*p = new Node();
p3->next = p;
p2 = p2->next;
p3 = p;
}
}
while(p1){
Node*p = new Node();
p->a = p1->a;
p->b = p1->b;
p3->next = p;
p3 = p;
p1 = p1->next;
}
while(p2){
Node*p = new Node();
p->a = p2->a;
p->b = p2->b;
p3->next = p;
p3 = p;
p2 = p2->next;
}
}
int main(){
Node*p1 = new Node();
Node*p2 = new Node();
Node*p3 = new Node();
int n = 0;
std::cin>>n;
p1->creat(p1,n);
std::cin>>n;
p2->creat(p2,n);
add(p1,p2,p3);
p3->visit(p3);
p3->destroy(p3);
std::cout<<std::endl;
p3 = new Node();
multiple(p1,p2,p3);
p3->visit(p3);
std::cout<<std::endl;
p3->destroy(p3);
p3 = new Node();
sub(p1,p2,p3);
p3->visit(p3);
p1->destroy(p1);
p2->destroy(p2);
p3->destroy(p3);
}
【数据结构】一元稀疏多项式
于 2023-10-18 23:09:22 首次发布