您的位置:首页 > 其它

02-线性结构2 一元多项式的乘法与加法运算 (20分)

2017-06-02 12:17 537 查看
设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出
0 0


输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0


//用不带空白头结点的单链表
#include<iostream>
using namespace std;

struct PolyNode{ //树的结点
int coef;
int expon;
PolyNode * next;
};

/* 由于在本函数中需要改变当前结果表达式尾项指针的值,所以函数传递进来的是结点指针的地址,*pRear指向尾项*/
void Insert(int coef,int expon,PolyNode ** rear){
PolyNode * temp=new PolyNode;
temp->coef=coef;
temp->expon=expon;
temp->next=NULL;
(*rear)->next=temp; /*将P指向的新结点插入到当前结果表达式尾项的后面 */
*rear=temp; /*修改pRear值 */
}

PolyNode * PolyAdd(PolyNode * p1,PolyNode * p2){
PolyNode * t1=p1,* t2=p2,* front,* rear,* temp;
front=new PolyNode;front->next=NULL;
rear=front;
while(t1 && t2){
if(t1->expon > t2->expon){
Insert(t1->coef,t1->expon,&rear);
t1=t1->next;
}else if(t1->expon < t2->expon){
Insert(t2->coef,t2->expon,&rear);
t2=t2->next;
}else{
Insert(t1->coef+t2->coef,t1->expon,&rear);
t1=t1->next;
t2=t2->next;
}
}

while(t1){
Insert(t1->coef,t1->expon,&rear);
t1=t1->next;
}

while(t2){
Insert(t2->coef,t2->expon,&rear);
t2=t2->next;
}

rear->next=NULL;
temp=front;
front=front->next;
delete(temp);
return front;
}

void CoutList(PolyNode * p);

PolyNode * PolyMulti(PolyNode * p1,PolyNode * p2){
PolyNode * t1=p1,* t2=p2,* front,* rear,* temp;
front=new PolyNode; front->next=NULL;
rear=front;

if(!t1 || !t2)  return NULL;

//这个循环是为下面的二重循环服务(二重循环里有比较指数大小,得有多项式才能比吧)
while(t2){
Insert(t1->coef*t2->coef,t1->expon+t2->expon,&rear);
t2=t2->next;
}
t1=t1->next;

int c,e;
while(t1){
t2=p2;rear=front;//第二个多项式头指针和结果多项式的尾指针从头开始
while(t2){
c=t1->coef * t2->coef;
e=t1->expon + t2->expon;

while(rear->next && rear->next->expon > e) rear=rear->next; //大于
if(rear->next && rear->ne
4000
xt->expon == e){//等于
if(rear->next->coef + c){
rear->next->coef += c;
}else{
temp=rear->next;
rear->next=temp->next;
delete(temp);
}
}else{//小于
temp=new  PolyNode;
temp->coef=c;
temp->expon=e;
temp->next=rear->next;
rear->next=temp;
rear=rear->next;

}

t2=t2->next;
}
t1=t1->next;
}
rear->next=NULL;
temp=front;
front=front->next;
delete(temp);
return front;
}

void CoutList(PolyNode * p){
if(!p){//辅助输出格式
cout<<"0 0"<<endl;
return ;
}
bool flag=false;
PolyNode * temp=p;

while(temp){
if(!flag) flag=true;
else  cout<<" ";
cout<<temp->coef<<" "<<temp->expon;
temp=temp->next;
}
cout<<endl;
}

int main()
{
//freopen("input.txt","r",stdin);
int n1,n2,coef,expon;
PolyNode * p1_front, * p1_rear,* p2_front, * p2_rear, * temp, * poly_add, * poly_multi;
p1_front=new PolyNode, p2_front=new PolyNode;
p1_rear=p1_front;  p2_rear=p2_front;

cin>>n1;
for(int i=0;i<n1;i++){
cin>>coef>>expon;
Insert(coef,expon,&p1_rear);
}
p1_rear->next=NULL;
temp=p1_front;
p1_front=p1_front->next;
delete(temp);

cin>>n2;
for(int i=0;i<n2;i++){
cin>>coef>>expon;
Insert(coef,expon,&p2_rear);
}
p2_rear->next=NULL;
temp=p2_front;
p2_front=p2_front->next;
delete(temp);

/*CoutList(p1_front);cout<<endl;
CoutList(p2_front);cout<<endl;  return 0;*/

poly_add=PolyAdd(p1_front,p2_front);
poly_multi=PolyMulti(p1_front,p2_front);
CoutList(poly_multi);
CoutList(poly_add);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: