qq_31663901 2016-09-25 06:36 采纳率: 100%
浏览 972

多项式加法和乘法有点bug

#include
using namespace std;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
ElementType Ratio;
ElementType Index;
PtrToNode Next;
};
typedef PtrToNode List;
List read(void ){
List head,p1,p2;
int n;
cin>>n;
head=new Node;
if(n==0)
return NULL;

    p1=p2=new Node;
    cin>>p1->Ratio>>p1->Index;
head->Next=p1;
for(int i=0;i<n-1;i++){
        p1=new Node;
        p2->Next=p1;
        p2=p1;
        cin>>p1->Ratio>>p1->Index;
}
p1->Next=NULL;
return head;

}
void print(List L){
if(L==NULL){
cout<<'0'<<' '<<'0'< return ;
}
List p=L->Next;
int flag=1;
do{
if(flag)
flag=0;
else
cout<<' ';
cout<Ratio<<' '<Index;

} while(p=p->Next);
cout< }
List Lplus(List L1,List L2){
if(!L1&&!L2)
return NULL;
if(!L1&&L2)
return L2;
if(L1&&!L2)
return L1;
List head,p1=L1->Next,p2=L2->Next,p3,p4;
head=new Node;
p4=p3=new Node;
head->Next=p3;
p3->Index=0,p3->Ratio=0;
while(p1&&p2){

    if(p1->Index==p2->Index){
    /*  int sum=p1->Ratio+p2->Ratio;
    if(sum){
                p3->Index=p1->Index;
                p3->Ratio=sum;
        p1=p1->Next;
        p2=p2->Next; 

        }*/

        if((p1->Ratio+p2->Ratio)==0){
        p1=p1->Next;
        p2=p2->Next;
        continue;
        }

        p3->Ratio=p1->Ratio+p2->Ratio;
        p3->Index=p1->Index;
           p1=p1->Next;
        p2=p2->Next;
    }
    else if(p1->Index>p2->Index){
        p3->Ratio=p1->Ratio,p3->Index=p1->Index;
        p1=p1->Next;
    }
    else{
        p3->Ratio=p2->Ratio,p3->Index=p2->Index;
        p2=p2->Next;
    }
    if(p1&&p2){
    p3=new Node;
    p4->Next=p3;
    p4=p3;
}

}

while(p1){
p3=new Node;
p4->Next=p3;
p4=p3;
p3->Ratio=p1->Ratio,p3->Index=p1->Index;
p1=p1->Next;

}
while(p2){
p3=new Node;
p4->Next=p3;
p4=p3;
p3->Ratio=p2->Ratio,p3->Index=p2->Index;
p2=p2->Next;
}

p3->Next=NULL;
return head;

}
List mul(List p1,List p2){
List head=new Node,p4,p3;
p4=p3=new Node;
head->Next=p3;
while(p2){
p3->Index=p1->Index+p2->Index;
p3->Ratio=p1->Ratio*p2->Ratio;
p2=p2->Next;
if(p2){
p3=new Node;
p4->Next=p3;
p4=p3;
}

}
p3->Next=NULL;
return head;

}
List multi(List L1,List L2){
if(!L1||!L2)
return NULL;
List p1=L1->Next,p2=L2->Next,p3;
p3= mul(p1,p2);
p1=p1->Next;
while(p1){
p3=Lplus(p3,mul(p1,p2));
p1=p1->Next;
}
return p3;

}

int main(){
List L1,L2,L3;
L1=read();
L2=read();
L3=multi(L1,L2);
print(L3);
L3=Lplus(L1,L2);
print(L3);
return 0;
}

  • 写回答

1条回答

  • threenewbee 2016-09-25 16:16
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用