python转c++语言_求帮忙将C++代码转换成Python语言

#includeusingnamespacestd;structnode{intzhi;intxi;node*next;};voidinput(node*p);node*sum(node*p,node*u);node*mult(node*p,node*u);voidprint(node*p);intmain(){str...

#include

using namespace std;

struct node{

int zhi;

int xi;

node *next;

};

void input(node *p);

node* sum(node *p,node *u);

node* mult(node *p,node *u);

void print(node *p);

int main(){

struct node *a=new node;

input(a);

struct node *b=new node;

input(b);

print(sum(a,b));

//mult(a,b);

print(mult(a,b));

return 0;

}

void input(node *p)

{

p->next=NULL;

node *u=new node;

while(cin>>u->xi>>u->zhi)

{

if(u->xi==0)break;

p->next=u;

u->next=NULL;

p=u;

u=new node;

}

}

node* sum(node *p,node *u)

{

node *a,*b,*l;

node *h=new node;

h->next=NULL;

l=h;

a=p->next;b=u->next;

while(a!=NULL&&b!=NULL)

{

if(a->zhi>b->zhi)

{

node *m=new node;

m->zhi=a->zhi;

m->xi=a->xi;

m->next=NULL;

l->next=m;

l=m;

a=a->next;

}

else if(a->zhizhi)

{

node *m=new node;

m->zhi=b->zhi;

m->xi=b->xi;

m->next=NULL;

l->next=m;

l=m;

b=b->next;

}

else

{

if(a->xi+b->xi!=0)

{

node *m=new node;

m->xi=a->xi+b->xi;

m->zhi=a->zhi;

m->next=NULL;

l->next=m;

l=m;

b=b->next;

a=a->next;

}

else

{

b=b->next;

a=a->next;

}

}

}

if(a==NULL&&b!=NULL){

while(b!=NULL)

{

node *m=new node;

m->xi=b->xi;

m->zhi=b->zhi;

m->next=NULL;

l->next=m;

l=m;

b=b->next;

}

}

else if(a!=NULL&&b==NULL){

while(a!=NULL)

{

node *m=new node;

m->xi=a->xi;

m->zhi=a->zhi;

m->next=NULL;

l->next=m;

l=m;

a=a->next;

}

}

return h;

}

node* mult(node *p,node *u)

{

int i=0;

node *a=p->next,*b=u->next,*d;

node *t=new node;

t->next=NULL;

d=t;

node *y=new node;

node *r=new node;

r->zhi=0;

r->xi=0;

r->next=NULL;

y->next=r;

while(a!=NULL)

{

b=u->next;

d=t;

while(b!=NULL)

{

node *c=new node;

c->zhi=a->zhi+b->zhi;

c->xi=a->xi*b->xi;

c->next=NULL;

d->next=c;

d=c;

b=b->next;

}

//print(t);

y=sum(y,t);//ÕâÀï³ö´í

//print(sum(g,t));

a=a->next;

}

return y;

}

void print(node *p)

{

node *u;

for(u=p->next;u!=NULL;u=u->next)

{

if(u->xi!=1&&u->xi!=-1&&u->xi!=0)cout<xi;

else if(u->xi==-1)cout<<"-";

else if(u->zhi==0&&u->xi!=0)cout<xi;

if(u->zhi>1)cout<<"x^"<zhi;

else if(u->zhi==1)cout<<"x";

if(u->next!=NULL&&u->next->xi>0)cout<<"+";

}

cout<

}

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值