/*设计函数分别求两个一元多项式的乘积与和。输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。输出格式:输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。<span style="font-family: Arial, Helvetica, sans-serif;">零多项式应输出0 0。</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
解答说明:链表的每一个节点储存有系数和指数。求和的时候如果指数相等,则将系数相加,注意系数和为0的情况。
求积的过程相当于多次多次求和的过程,调用求和函数累加即可*/
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space: normal;"> </span></span>#include <iostream>
#include <cstdio>
using namespace std;
typedef struct Node{
<span style="white-space:pre"> </span>int XiShu;
<span style="white-space:pre"> </span>int ZhiShu;
<span style="white-space:pre"> </span>struct Node * link;
}*Pnode;
void add(int XiShu, int ZhiShu, Pnode *PtrRear){ //这里传入的必须是一个指向rear指针的指针,才能真正改rear的值
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Pnode p = new Node;
<span style="white-space:pre"> </span>p->XiShu = XiShu;
<span style="white-space:pre"> </span>p->ZhiShu = ZhiShu;
<span style="white-space:pre"> </span>(*PtrRear)->link = p;
<span style="white-space:pre"> </span>(*PtrRear) = p;
}
int Compare(int a, int b){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(a>b) return 1;
<span style="white-space:pre"> </span>else if(a<b) return -1;
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>return 0;
}
//计算多项式的和
Pnode Padd(Pnode p1, Pnode p2){
<span style="white-space:pre"> </span>Pnode front, temp, rear;
<span style="white-space:pre"> </span>int sum;
<span style="white-space:pre"> </span>rear = new Node;
<span style="white-space:pre"> </span>if(rear == 0){
<span style="white-space:pre"> </span>cout<<"Error";
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>front = rear;
<span style="white-space:pre"> </span>while(p1 && p2){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>switch(Compare(p1->ZhiShu,p2->ZhiShu)){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>case 1:
<span style="white-space:pre"> </span>add(p1->XiShu, p1->ZhiShu,&rear);
<span style="white-space:pre"> </span>p1 = p1->link;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case -1:
<span style="white-space:pre"> </span>add(p2->XiShu, p2->ZhiShu, &rear);
<span style="white-space:pre"> </span>p2 = p2->link;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>case 0:
<span style="white-space:pre"> </span>sum = p1->XiShu + p2->XiShu;
<span style="white-space:pre"> </span>if(sum)<span style="white-space:pre"> </span>add(sum, p1->ZhiShu, &rear);
<span style="white-space:pre"> </span>p1 = p1->link;
<span style="white-space:pre"> </span>p2 = p2->link;
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(;p1;p1 = p1->link) {
<span style="white-space:pre"> </span>if(p1->XiShu == 0) break;
<span style="white-space:pre"> </span>add(p1->XiShu, p1->ZhiShu, &rear);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(;p2;p2 = p2->link) {
<span style="white-space:pre"> </span>if(p2->XiShu == 0) break;
<span style="white-space:pre"> </span>add(p2->XiShu, p2->ZhiShu, &rear);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>rear->link = NULL;
<span style="white-space:pre"> </span>temp = front;
<span style="white-space:pre"> </span>front = front->link;
<span style="white-space:pre"> </span>delete temp;
<span style="white-space:pre"> </span>return front;
}
//计算多项式的乘积
Pnode Pmul(Pnode p1,int n1, Pnode p2, int n2){
<span style="white-space:pre"> </span>Pnode temp = 0;
<span style="white-space:pre"> </span>Node * rear = new Node;
<span style="white-space:pre"> </span>Pnode *pfront = new Pnode[n1];
<span style="white-space:pre"> </span>if(pfront == 0){
<span style="white-space:pre"> </span>cout<<"error";
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>while(p1 && p2){
<span style="white-space:pre"> </span>if(n1 == 0 || n2 == 0){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>rear->XiShu = 0;
<span style="white-space:pre"> </span>return rear;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>Pnode pt2 = p2; //用来保存p2的位置,为第二个for循环能再次进行,因为每次循环完都使得p2移动到末尾
<span style="white-space:pre"> </span>for(int i=0;p1;p1=p1->link,i++){
<span style="white-space:pre"> </span>rear = new Node;
<span style="white-space:pre"> </span>if(rear == 0){
<span style="white-space:pre"> </span>cout<<"error";
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>pfront[i] = rear;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(;p2;p2 = p2->link){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>add((p1->XiShu * p2->XiShu),(p1->ZhiShu + p2->ZhiShu),&rear);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>rear->link =NULL; //将分别相乘后的每个多项式最后一个节点指向NULL,才能使得while(p1 && p2)顺利结束
<span style="white-space:pre"> </span>temp =pfront[i];
<span style="white-space:pre"> </span>pfront[i] = pfront[i]->link;
<span style="white-space:pre"> </span>p2 = pt2;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>for(int i =0,j=1;j<n1;j++){
<span style="white-space:pre"> </span>pfront[j] =Padd(pfront[i],pfront[j]);
<span style="white-space:pre"> </span>i =j;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>rear = pfront[n1-1];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>delete temp;
<span style="white-space:pre"> </span>delete [] pfront;
<span style="white-space:pre"> </span>return rear;
}
int main()
{
<span style="white-space:pre"> </span>int n1,n2;
<span style="white-space:pre"> </span>Pnode p1,p2,temp;
<span style="white-space:pre"> </span>cin>>n1;
<span style="white-space:pre"> </span>Pnode front1 = new Node;
<span style="white-space:pre"> </span>if(front1 == 0) {
<span style="white-space:pre"> </span>cout<<"Error";
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>p1 = front1;
<span style="white-space:pre"> </span>if(n1 == 0){
<span style="white-space:pre"> </span>p1->XiShu = 0;
<span style="white-space:pre"> </span>p1->ZhiShu = 0;
<span style="white-space:pre"> </span>p1->link = NULL;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>for(int i=0; i<n1;i++){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Pnode ptr = new Node;
<span style="white-space:pre"> </span>cin>>ptr->XiShu>>ptr->ZhiShu;
<span style="white-space:pre"> </span>front1->link = ptr;
<span style="white-space:pre"> </span>front1 = ptr;
<span style="white-space:pre"> </span>front1->link =NULL;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>temp =p1;
<span style="white-space:pre"> </span>p1 = p1->link;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cin>>n2;
<span style="white-space:pre"> </span>Pnode front2 = new Node;
<span style="white-space:pre"> </span>if(front2 == 0) {
<span style="white-space:pre"> </span>cout<<"Error";
<span style="white-space:pre"> </span>exit(0);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>p2 = front2;
<span style="white-space:pre"> </span>if(n2 == 0){
<span style="white-space:pre"> </span>p2->XiShu = 0;
<span style="white-space:pre"> </span>p2->ZhiShu = 0;
<span style="white-space:pre"> </span>p2->link = NULL;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else{
<span style="white-space:pre"> </span>for(int i=0; i<n2;i++){
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Pnode ptr = new Node;
<span style="white-space:pre"> </span>cin>>ptr->XiShu>>ptr->ZhiShu;
<span style="white-space:pre"> </span>front2->link = ptr;
<span style="white-space:pre"> </span>front2 = ptr;
<span style="white-space:pre"> </span>front2->link = NULL;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>temp =p2;
<span style="white-space:pre"> </span>p2 = p2->link;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>temp = Pmul(p1,n1,p2,n2);//乘法结果为0 则temp->XiShu 为0 temp不为0
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>while(temp){
<span style="white-space:pre"> </span>if(temp->XiShu ==0){
<span style="white-space:pre"> </span>cout<<"0 0";
<span style="white-space:pre"> </span>break;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>cout<<temp->XiShu<<" "<<temp->ZhiShu;
<span style="white-space:pre"> </span>if(temp->link) cout<<" ";
<span style="white-space:pre"> </span>temp = temp->link;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>cout<<endl;
<span style="white-space:pre"> </span>temp = Padd(p1,p2);//合并多项式结果为0 则temp为0
<span style="white-space:pre"> </span>if(temp == 0) cout<<"0 0"; // 如果和为0 则输出“0 0”;
<span style="white-space:pre"> </span>while(temp){
<span style="white-space:pre"> </span>cout<<temp->XiShu<<" "<<temp->ZhiShu;
<span style="white-space:pre"> </span>if(temp->link) cout<<" ";
<span style="white-space:pre"> </span>temp = temp->link;
<span style="white-space:pre"> </span> }
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
delete temp;
<span style="white-space:pre"> </span>return 0;
}