c语言萌新求助!做数据结构的问题遇到坎了!
请尽情吐槽变量命名,我会努力改的
题目:02-线性结构2 一元多项式的乘法与加法运算 (20分)
我的加法运算已经测试过了,没问题。但是乘法运算直接从中间输出,后面跟输出样例都对的上的(如图),前面不知道为什么漏了。
lz已经测试很久很久很久了,还是不会
代码如下
#include<stdio.h>
#include<stdlib.h>
typedef struct F *list;
struct F{
int coef;
int expon;
list next;
};
list read(int N);
list add(list l1,list l2);
list multiply(list l1,list l2);
list insert(list l,list i);
void print(list l);
int main(){
int N1,N2;
list l1,l2;
list result1,result2;
scanf("%d",&N1);
l1 = read(N1);
scanf("%d",&N2);
l2 = read(N2);
//¶ÁÈë²Ù×÷
//result1 = add(l1,l2);
result2 = multiply(l1,l2);
print(result2);
//print(result1);
}
list read(int N){
list l0,l;
l = (list)malloc(sizeof(F));
l0 = l;
for(int i = N;i >0; i--){
list rear = (list)malloc(sizeof(F));
scanf("%d %d",&rear ->coef,&rear ->expon);
l ->next = rear;
l = l ->next;
}
l ->next =NULL;
return l0;
}
list add(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear = result;
l1 = l1 ->next;
l2 = l2 ->next;
while(l1&&l2){
list new1 =(list)malloc(sizeof(F));
if(l1 ->expon > l2 ->expon){
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear ->next;
l1 = l1 ->next;
}
else if(l1 ->expon < l2 ->expon){
new1 ->coef = l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
else if(l1 ->expon = l2 ->expon){
new1 ->coef =l1 ->coef +l2 ->coef;
new1 ->expon=l1 ->expon;
rear ->next =new1;
rear = rear ->next;
l1 = l1 ->next;
l2 = l2 ->next;
}
}
while (l1){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear->next;
l1 = l1 ->next;
}
while (l2){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
rear ->next =NULL;
return result;
}
list insert(list l,list i){
list step =l;
while(step ->next && i){
if (step ->next->expon > i ->expon){
step = step ->next;
}
else if(step ->next ->expon = i->expon){
step ->next ->coef += i->coef;
free (i);
break;
}
else{
i ->next =step ->next->next;
step ->next->next =i;
break;
}
}
while(!step ->next){
step ->next =i;
i ->next =NULL;
}
return l;
}
list multiply(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear =result;
l1 =l1 ->next;
l2 =l2 ->next;
list a =l1;
while (a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef =a->coef *l2 ->coef;
new1 ->expon=a ->expon +l2 ->expon;
rear ->next =new1;
a =a->next;
}
rear ->next =NULL;
l2 = l2->next;
while(l2){
a =l1;
while(a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef = a ->coef*l2 ->coef;
new1 ->expon =a ->expon + l2 ->expon;
result =insert(result,new1);
a = a->next;
}
l2 =l2 ->next;
}
return result;
}
void print(list l){
l = l->next;
while(l){
printf("%d %d",l->coef,l->expon);
if(l ->next){
printf(" ");
}
else{
printf("\n");
}
l = l->next;
}
}

请尽情吐槽变量命名,我会努力改的
题目:02-线性结构2 一元多项式的乘法与加法运算 (20分)
我的加法运算已经测试过了,没问题。但是乘法运算直接从中间输出,后面跟输出样例都对的上的(如图),前面不知道为什么漏了。
lz已经测试很久很久很久了,还是不会
代码如下
#include<stdio.h>
#include<stdlib.h>
typedef struct F *list;
struct F{
int coef;
int expon;
list next;
};
list read(int N);
list add(list l1,list l2);
list multiply(list l1,list l2);
list insert(list l,list i);
void print(list l);
int main(){
int N1,N2;
list l1,l2;
list result1,result2;
scanf("%d",&N1);
l1 = read(N1);
scanf("%d",&N2);
l2 = read(N2);
//¶ÁÈë²Ù×÷
//result1 = add(l1,l2);
result2 = multiply(l1,l2);
print(result2);
//print(result1);
}
list read(int N){
list l0,l;
l = (list)malloc(sizeof(F));
l0 = l;
for(int i = N;i >0; i--){
list rear = (list)malloc(sizeof(F));
scanf("%d %d",&rear ->coef,&rear ->expon);
l ->next = rear;
l = l ->next;
}
l ->next =NULL;
return l0;
}
list add(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear = result;
l1 = l1 ->next;
l2 = l2 ->next;
while(l1&&l2){
list new1 =(list)malloc(sizeof(F));
if(l1 ->expon > l2 ->expon){
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear ->next;
l1 = l1 ->next;
}
else if(l1 ->expon < l2 ->expon){
new1 ->coef = l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
else if(l1 ->expon = l2 ->expon){
new1 ->coef =l1 ->coef +l2 ->coef;
new1 ->expon=l1 ->expon;
rear ->next =new1;
rear = rear ->next;
l1 = l1 ->next;
l2 = l2 ->next;
}
}
while (l1){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear->next;
l1 = l1 ->next;
}
while (l2){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
rear ->next =NULL;
return result;
}
list insert(list l,list i){
list step =l;
while(step ->next && i){
if (step ->next->expon > i ->expon){
step = step ->next;
}
else if(step ->next ->expon = i->expon){
step ->next ->coef += i->coef;
free (i);
break;
}
else{
i ->next =step ->next->next;
step ->next->next =i;
break;
}
}
while(!step ->next){
step ->next =i;
i ->next =NULL;
}
return l;
}
list multiply(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear =result;
l1 =l1 ->next;
l2 =l2 ->next;
list a =l1;
while (a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef =a->coef *l2 ->coef;
new1 ->expon=a ->expon +l2 ->expon;
rear ->next =new1;
a =a->next;
}
rear ->next =NULL;
l2 = l2->next;
while(l2){
a =l1;
while(a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef = a ->coef*l2 ->coef;
new1 ->expon =a ->expon + l2 ->expon;
result =insert(result,new1);
a = a->next;
}
l2 =l2 ->next;
}
return result;
}
void print(list l){
l = l->next;
while(l){
printf("%d %d",l->coef,l->expon);
if(l ->next){
printf(" ");
}
else{
printf("\n");
}
l = l->next;
}
}



