#include <stdio.h>
#include <stdlib.h>
struct space{
int xishu;
int zhishu;
struct space *next;
};
struct space* create();
void print(struct space *p);
struct space* mul(struct space *p1,struct space *p2);
struct space* add(struct space *p1,struct space *p2);
void attach(int c,int e,struct space **L2);
int main(){
struct space *p1;
struct space *p2;
struct space *p3;
struct space *p4;
p1 = create();
p2 = create();
p3 = mul(p1,p2);
p4 = add(p1,p2);
print(p3);
print(p4);
return 0;
}
struct space* create(){ //创建链表
int n,i;
scanf("%d",&n);
struct space *head;
struct space *rom;
rom=(struct space*)malloc(sizeof(struct space));
head=rom;
while(n--){
rom=rom->next;
rom=(struct space*)malloc(sizeof(struct space));
scanf("%d %d",&rom->xishu,&rom->zhishu);
}
rom->next=NULL;
return head;
}
struct space* mul(struct space *p1,struct space *p2){ //两个多项式相乘
struct space *L,*front1,*middle1,*middle2;
L=(struct space*)malloc(sizeof(struct space));
L->next=NULL;
front1=L;
p1=p1->next;
p2=p2->next;
middle1=p1;
L=L->next;
for(;middle1;middle1=middle1->next){
middle2=p2;
for(;middle2;middle2=middle2->next){
L->xishu=(middle1->xishu)+(middle2->xishu);
L->zhishu=(middle1->zhishu)*(middle2->zhishu);
L->next = NULL;
L=L->next;
L=(struct space*)malloc(sizeof(struct space));
L->next = NULL;
}
}
return front1;
}
struct space* add(struct space *p1,struct space *p2){ //两个多项式相加
struct space *L2,*front2;
L2=(struct space*)malloc(sizeof(struct space));
L2->next=NULL;
front2=L2;
p1=p1->next;
p2=p2->next;
while(p1&&p2){
if(p1->zhishu>p2->zhishu){
attach(p1->xishu,p1->zhishu,&L2);
p1=p1->next;
}else if(p2->zhishu>p2->zhishu){
attach(p2->xishu,p2->zhishu,&L2);
p2=p2->next;
}else{
if(p2->xishu+p1->xishu){
attach(p1->xishu+p2->xishu,p1->zhishu,&L2);
p2=p2->next;
p1=p1->next;
}else{
;
}
}
}
L2->next=p1?p1:p2;
return front2;
}
void attach(int c,int e,struct space **L2){ //将系数、指数插到新链表里
struct space *p;
p=(struct space*)malloc(sizeof(struct space));
p->ishu=c;
p->zhishu=e;
(*L2)->next=p;
(*L2)=p;
}
void print(struct space *p){ //打印
p=p->next;
while(p){
printf("%d %d",p->xishu,p->zhishu);
if(p->next){
printf(" ");
}
p=p->next;
}
}
```c
//代码太多了,眼会花,先改这个创建的函数,改完还有错就继续来
struct space* create(){ //创建链表
int n,i;
scanf("%d",&n);
struct space *head;
struct space *rom;
head=(struct space*)malloc(sizeof(struct space));
rom=head;
while(n--){
rom->next=(struct space*)malloc(sizeof(struct space));
rom=rom->next;
scanf("%d %d",&rom->xishu,&rom->zhishu);
}
rom->next=NULL;
return head;
}
```
#include <stdio.h>
#include <stdlib.h>
struct space{
int xishu;
int zhishu;
struct space *next;
};
struct space* create();
void print(struct space *p);
struct space* mul(struct space *p1,struct space *p2);
struct space* add(struct space *p1,struct space *p2);
struct space* attach(int c,int e,struct space *L2);
int main(){
struct space *p1;
struct space *p2;
struct space *p3;
struct space *p4;
p1 = create();
p2 = create();
p3 = mul(p1,p2);
p4 = add(p1,p2);
print(p3);
print(p4);
return 0;
}
struct space* create(){
int n,i;
scanf("%d",&n);
struct space *head;
struct space *rom;
head=(struct space*)malloc(sizeof(struct space));
rom=head;
while(n--){
rom->next=(struct space*)malloc(sizeof(struct space));
rom=rom->next;
scanf("%d %d",&rom->xishu,&rom->zhishu);
}
rom->next=NULL;
return head;
}
struct space* mul(struct space *p1,struct space *p2){ //两个多项式相乘
struct space *L,*front1,*middle1,*middle2;
L=(struct space*)malloc(sizeof(struct space));
L->next=NULL;
front1=L;
p1=p1->next;
p2=p2->next;
middle1=p1;
L=L->next;
for(;middle1;middle1=middle1->next){
middle2=p2;
for(;middle2;middle2=middle2->next){
L->xishu=(middle1->xishu)+(middle2->xishu);
L->zhishu=(middle1->zhishu)*(middle2->zhishu);
L=L->next;
L=(struct space*)malloc(sizeof(struct space));
}
}
L->next=NULL;
return front1;
}
struct space* add(struct space *p1,struct space *p2){ //两个多项式相加
struct space *L2,*front2;
L2=(struct space*)malloc(sizeof(struct space));
L2->next=NULL;
front2=L2;
p1=p1->next;
p2=p2->next;
while(p1&&p2){
if(p1->zhishu>p2->zhishu){
L2=attach(p1->xishu,p1->zhishu,L2);
p1=p1->next;
}else if(p2->zhishu>p2->zhishu){
L2=attach(p2->xishu,p2->zhishu,L2);
p2=p2->next;
}else{
if(p2->xishu+p1->xishu){
L2=attach(p1->xishu+p2->xishu,p1->zhishu,L2);
p2=p2->next;
p1=p1->next;
}else{
L2=attach(0,0,L2);
}
}
}
L2->next=p1?p1:p2;
return front2;
}
struct space* attach(int c,int e,struct space* L2){ //将系数、指数插到新链表里
struct space *p;
struct space *front3;
front3 = L2;
p=(struct space*)malloc(sizeof(struct space));
p->xishu=c;
p->zhishu=e;
L2->next=p;
L2=p;
L2->next=NULL;
return front3;
}
void print(struct space *p){ //打印
struct space* m = p->next;
while(m){
printf("%d %d",m->xishu,m->zhishu);
if(m->next){
printf(" ");
}
m=m->next;
}
}
#include <stdio.h>
#include <stdlib.h>
struct space{
int xishu;
int zhishu;
struct space *next;
};
struct space* create();
void print(struct space *p);
struct space* add(struct space *p1,struct space *p2);
struct space* mul(struct space* p1,struct space*p2);
int main(){
struct space *p1;
struct space *p2;
struct space *p3;
struct space *p4;
p1 = create();
p2 = create();
p4 = mul(p1,p2);
p3 = add(p1,p2);
print(p3);
print(p4);
return 0;
}
struct space* create(){//wode
int n;
scanf("%d",&n);
struct space *head;
struct space *rom;
head=(struct space*)malloc(sizeof(struct space));
rom=head;
while(n--){
rom->next=(struct space*)malloc(sizeof(struct space));
rom=rom->next;
scanf("%d%d",&(rom->xishu),&(rom->zhishu));
}
rom->next=NULL;
return head;
}
struct space* add(struct space* p1,struct space*p2){
struct space* L1;
L1=(struct space*)malloc(sizeof(struct space));
L1->next=NULL;
struct space* top;
struct space* a1;
struct space* a2;
top=L1;
a1=p1->next;
a2=p2->next;
while(a1&&a2){
if(a1->zhishu>a2->zhishu){
struct space* middle=(struct space*)malloc(sizeof(struct space));
middle->next=NULL;
middle->xishu=a1->xishu;
middle->zhishu=a1->zhishu;
top->next=middle;
top=middle;
middle->next=NULL;
a1=a1->next;
}else if(a1->zhishu<a2->zhishu){
struct space* middle=(struct space*)malloc(sizeof(struct space));
middle->next=NULL;
middle->xishu=a2->xishu;
middle->zhishu=a2->zhishu;
top->next=middle;
top=middle;
middle->next=NULL;
a2=a2->next;
}else{
if(a1->xishu+a2->xishu){
struct space* middle=(struct space*)malloc(sizeof(struct space));
middle->next=NULL;
middle->xishu=a2->xishu+a1->xishu;
middle->zhishu=a2->zhishu;
top->next=middle;
top=middle;
middle->next=NULL;
a1=a1->next;
a2=a2->next;
}
}
}
while(a1){
struct space* middle=(struct space*)malloc(sizeof(struct space));
middle->next=NULL;
middle->xishu=a1->xishu;
middle->zhishu=a1->zhishu;
top->next=middle;
top=middle;
middle->next=NULL;
a1=a1->next;
}
while(a2){
struct space* middle=(struct space*)malloc(sizeof(struct space));
middle->next=NULL;
middle->xishu=a2->xishu;
middle->zhishu=a2->zhishu;
top->next=middle;
top=middle;
middle->next=NULL;
a2=a2->next;
}
top->next=NULL;
return L1;
}
void print(struct space *p){ //打印
struct space* m = p->next;
while(m){
printf("%d %d",m->xishu,m->zhishu);
if(m->next){
printf(" ");
}else{
printf("\n");
}
m=m->next;
}
}
struct space* mul(struct space* p1,struct space*p2){
struct space* p5;
struct space* L3;
L3=(struct space*)malloc(sizeof(struct space));
L3->next=NULL;
struct space* p55;
struct space* p66;
struct space* pn;
struct space* phead;
p55=p1->next;
p66=p2->next;
if(p1&&p2){
for(p55=p1->next;p55;p55=p55->next){
p5=(struct space*)malloc(sizeof(struct space));
p5->next=NULL;
phead=p5;
for(p66=p2->next;p66;p66=p66->next){
pn=(struct space*)malloc(sizeof(struct space));
pn->xishu=p55->xishu*p66->xishu;
pn->zhishu=p55->zhishu+p66->zhishu;
phead->next=pn;
phead=pn;
}
L3=add(L3,p5);
free(p5);
}
}
return L3;
}