一元多项式的*与+

调了好久的bug,,不过在看到最后全是红色的答案正确,激动到起飞
哈哈哈喝O泡果奶把清凉抱抱\( ̄︶ ̄*))

下面是总结:
1.零多项式是指系数全为0的多项式
2.别忘记开始的特判,加法是 if(p1==NULL&&p2==NULL) return 0;
乘法是 if(t1==NULL||t2==NULL) return 0;
3.因为自己做的是带头结点的链表,所以主函数里面的if(pa) TraverseList(pa); else cout<<"0 0"<<endl;pa不可能为空,所以在前面返回head的时候要判断一下if(head->next ) return head; return NULL;
4.在打印输出的函数里,就要看对零多项式的理解了,如果最后head里面是2 0 0 0 0 这种,就属于零多项式,就要打印输出0 0,怎么办呢,添加flag看看是不是系数全是0,如果系数全是0 ,最后直接打印0 0,
还要说的一点是,之前打印输出自己一直用那个模板,即

当前末尾输出空格或者输出回车,是由 p->next 是否为空来决定的

但是这道题如果p之后的系数都是0了,if里面就进不去,导致结尾多了空格少了回车,所以就改成这样,www大佬就是大佬orz
flag发挥了两次作用,1判断零多项式 2判断在输出系数前输出空格
5.基本操作一定仔细好嘛www

//零多项式的定义:系数全为零的多项式;

#include<bits/stdc++.h>
using namespace std;
typedef struct node{
	int coef;
	int expon;//指数`1 
	struct node* next;
}node,*pnode;

pnode CreateNode(){
	pnode head,end,p;
	head=new node();
	head->next =NULL;
	end=head;
	int n;cin>>n;
	while(n--){
		p=new node();
		cin>>p->coef>>p->expon;
		end->next =p;
		end=p;
	}
	end->next =NULL;
	return head;
}
int com(int x1,int x2){
	if(x1>x2) return 1;
	if(x1<x2) return 2;
	return 3;
}


void Attach1(int coef,int expon,pnode &p){
	pnode ptr;
	ptr=new node();
	ptr->coef =coef;
	ptr->expon =expon;
	ptr->next =NULL;
	p->next=ptr;
	p=ptr; 
}

pnode Add1(pnode p1,pnode p2){
	p1=p1->next ;p2=p2->next ;
    if(p1==NULL&&p2==NULL) return 0;
	pnode head,p;
	head=new node();
	head->next =NULL;
	p=head;

	int sum;
	while(p1&&p2){
		switch(com(p1->expon,p2->expon )){
			case 1:
				Attach1(p1->coef ,p1->expon ,p);
				p1=p1->next ;
				break;
			case 2:
				Attach1(p2->coef ,p2->expon ,p);
				p2=p2->next ;
				break;
			case 3:
				sum=p1->coef +p2->coef ;
				if(sum) 
					Attach1(sum,p1->expon ,p);
				p1=p1->next ;
				p2=p2->next ;
				break;   
		}
	}
	for(;p1;p1=p1->next )//
		Attach1(p1->coef ,p1->expon ,p);
	for(;p2;p2=p2->next )//
		Attach1(p2->coef ,p2->expon ,p);
	p->next =NULL;

	if(head->next )
		return head;
	return NULL;

	
} 
pnode Mult(pnode p1,pnode p2){
	pnode head,t,p,t1,t2;
	int e,c;
	head=new node();
	head->next =NULL;
	p=head;
	t1=p1->next ;t2=p2->next ;
	if(t1==NULL||t2==NULL) return 0;
	
	
	while(t2){
		Attach1(t1->coef *t2->coef ,t1->expon +t2->expon ,p);
		t2=t2->next ;
	}
	t1=t1->next ;
	while(t1){
		t2=p2->next ;p=head;
		while(t2){
			e=t1->expon +t2->expon ;
			c=t1->coef *t2->coef ;
			while(p->next &&p->next ->expon>e){
				p=p->next ;
			}
			if(p->next &&p->next ->expon==e){
				if(p->next ->coef+c){
					p->next ->coef+=c;
				}
				else{
					t=p->next;
					p->next =p->next ->next;
					free(t);
				}
			}
			else{
				t=new node();
				t->coef =c;t->expon =e;
				t->next =p->next ;
				p->next =t;p=t ;
			}
			t2=t2->next ;
		} 
		t1=t1->next ;
	}
	if(head->next )
		return head;
	return NULL;
}


void TraverseList(pnode head){
	pnode p=head->next;
	int flag=0;
	while(p){
		if(p->coef!=0){
			if(!flag) flag=1;
			else cout<<" "; 
			cout<<p->coef <<" "<<p->expon ;
		} 	
		p=p->next;
	} 
	if(flag==0){
		 cout<<"0 0";
	}	
	cout<<endl;
} 

int main(){
	pnode p1,p2,pa,pm;
	p1=CreateNode();
	p2=CreateNode();
	pm=Mult(p1,p2);
	if(pm) TraverseList(pm);
	else cout<<"0 0"<<endl;

	pa=Add1(p1,p2);
	if(pa) TraverseList(pa);
	else cout<<"0 0"<<endl;

	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值