一元多项式相加
请教,我现在的问题是在判断想的系数为0的多项式的地方出了问题,如下:#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int coef,exp;
struct node *next;
}NODE;
void Input(NODE * head, int x)
{
NODE * p;
p = head;
int y,z;
while(x--)
{
p->next = (NODE * )malloc(sizeof(NODE));
scanf("%d%d", &y, &z);
p->next->coef = y;
p->next->exp = z;
p = p->next;
p->next = NULL;
}
}
void ifZero(NODE * head1, NODE * head2) //判断项的系数是0就丢掉
{
NODE * p1, * p2, * p3;
p1 = head1->next;
p2 = head2;
p3 = head2->next;
while(p1 != NULL)
{
if(p1->coef != 0)
{
p2->next = (NODE * )malloc(sizeof(NODE));
p2->next->coef = p1->coef;
p2->next->exp = p1->exp;
p2 = p2->next;
p2->next = NULL;
p1 = p1->next;
}
else
{
p1 = p1->next;
}
}
if(p3 == NULL)
{
p2->next = (NODE * )malloc(sizeof(NODE));
p2->next->coef = 0;
p2->next->exp = 0;
p2 = p2->next;
p2->next = NULL;
}
}
void Output(NODE * head)
{
NODE * p;
p = head->next;
while(p->next != NULL)
{
printf("<%d,%d>", p->coef, p->exp);
p = p->next;
}
printf("<%d,%d>\n", p->coef, p->exp);
}
void addListTwo(NODE * head1, NODE * head2, NODE * head4)
{
NODE * p1, * p2, * p4;
p1 = head1->next;
p2 = head2->next;
p4 = head4;
while(p1 != NULL || p2 != NULL)
{
if(p1 != NULL && p2 != NULL)
{
if(p1->exp < p2->exp)
{
p4->next = (NODE * )malloc(sizeof(NODE));
p4->next->coef = p1->coef;
p4->next->exp = p1->exp;
p4 = p4->next;
p4->next = NULL;
p1 = p1->next;
}
else if(p1->exp == p2->exp)
{
p4->next = (NODE * )malloc(sizeof(NODE));
p4->next->coef = p1->coef + p2->coef;
p4->next->exp = p1->exp;
p4 = p4->next;
p4->next = NULL;
p1 = p1->next;
p2 = p2->next;
}
else
{
p4->next = (NODE * )malloc(sizeof(NODE));
p4->next->coef = p2->coef;
p4->next->exp = p2->exp;
p4 = p4->next;
p4->next = NULL;
p2 = p2->next;
}
}
else if(p1 != NULL&&p2 == NULL)
{
p4->next = (NODE * )malloc(sizeof(NODE));
p4->next->coef = p1->coef;
p4->next->exp = p1->exp;
p4 = p4->next;
p4->next = NULL;
p1 = p1->next;
}
else if(p1 == NULL&&p2 != NULL)
{
p4->next = (NODE * )malloc(sizeof(NODE));
p4->next->coef = p2->coef;
p4->next->exp = p2->exp;
p4 = p4->next;
p4->next = NULL;
p2 = p2->next;
}
}
}
void addListThree(NODE * head1, NODE * head2, NODE * head3, NODE * head5)
{
NODE * p1, * p2, * p3, * p5;
p1 = head1->next;
p2 = head2->next;
p3 = head3->next;
p5 = head5;
while(p1 != NULL||p2 != NULL||p3 != NULL)
{
if(p1 != NULL && p2 != NULL && p3 != NULL)
{
if(p1->exp < p2->exp && p1->exp < p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef;
p5->next->exp =p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
}
else if(p2->exp < p1->exp && p2->exp < p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef;
p5->next->exp =p2->exp;
p5 = p5->next;
p5->next = NULL;
p2 = p2->next;
}
else if(p3->exp < p1->exp && p3->exp < p2->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p3->coef;
p5->next->exp =p3->exp;
p5 = p5->next;
p5->next = NULL;
p3 = p3->next;
}
else if(p1->exp == p2->exp && p1->exp != p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef + p2->coef;
p5->next->exp = p1->exp;
p5 = p5 ->next;
p5->next = NULL;
p1 = p1->next;
p2 = p2->next;
}
else if(p1->exp == p3->exp && p1->exp != p2->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef + p3->coef;
p5->next->exp = p1->exp;
p5 = p5 ->next;
p5->next = NULL;
p1 = p1->next;
p3 = p3->next;
}
else if(p2->exp == p3->exp && p1->exp != p2->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef + p3->coef;
p5->next->exp = p2->exp;
p5 = p5 ->next;
p5->next = NULL;
p2 = p2->next;
p3 = p3->next;
}
else if(p1->exp == p2->exp && p1->exp == p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef + p2->coef + p3->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
p2 = p2->next;
p3 = p3->next;
}
}
else if(p1 != NULL && p2 != NULL && p3 == NULL)
{
if(p1->exp < p2->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
}
else if(p1->exp == p2->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef + p2->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
p2 = p2->next;
}
else
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef;
p5->next->exp = p2->exp;
p5 = p5->next;
p5->next = NULL;
p2 = p2->next;
}
}
else if(p1 != NULL && p3 != NULL && p2 == NULL)
{
if(p1->exp < p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
}
else if(p1->exp == p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef + p3->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
p3 = p3->next;
}
else
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p3->coef;
p5->next->exp = p3->exp;
p5 = p5->next;
p5->next = NULL;
p3 = p3->next;
}
}
else if(p2 != NULL && p3 != NULL && p1 == NULL)
{
if(p2->exp < p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef;
p5->next->exp = p2->exp;
p5 = p5->next;
p5->next = NULL;
p2 = p2->next;
}
else if(p2->exp == p3->exp)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef + p3->coef;
p5->next->exp = p2->exp;
p5 = p5->next;
p5->next = NULL;
p2 = p2->next;
p3 = p3->next;
}
else
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p3->coef;
p5->next->exp = p3->exp;
p5 = p5->next;
p5->next = NULL;
p3 = p3->next;
}
}
else if(p1 != NULL && p2 == NULL && p3 == NULL)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p1->coef;
p5->next->exp = p1->exp;
p5 = p5->next;
p5->next = NULL;
p1 = p1->next;
}
else if(p2 != NULL && p1 == NULL && p3 == NULL)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p2->coef;
p5->next->exp = p2->exp;
p5 = p5->next;
p5->next = NULL;
p2 = p2->next;
}
else if(p3 != NULL && p1 == NULL && p2== NULL)
{
p5->next = (NODE * )malloc(sizeof(NODE));
p5->next->coef = p3->coef;
p5->next->exp = p3->exp;
p5 = p5->next;
p5->next = NULL;
p3 = p3->next;
}
}
}
void clearList(NODE * head)
{
NODE * p1, * p2;
p1 = head->next;
while(p1 != NULL)
{
p2 = p1->next;
free(p1);
p1 = p2;
}
head->next = NULL;
}
int main()
{
int n;
scanf("%d", &n);
if(n==1)
{
int a,b,c;
NODE * head1, * head2, * head3, * head4, * head5, * head6;
head1 = (NODE * )malloc(sizeof(NODE));
head2 = (NODE * )malloc(sizeof(NODE));
head3 = (NODE * )malloc(sizeof(NODE));
head4 = (NODE * )malloc(sizeof(NODE));
head5 = (NODE * )malloc(sizeof(NODE));
head6 = (NODE * )malloc(sizeof(NODE));
scanf("%d", &a);
Input(head1,a);
scanf("%d", &b);
Input(head2, b);
scanf("%d", &c);
Input(head3, c);
addListTwo(head1,head2,head4);
addListThree(head1,head2,head3,head5);
ifZero(head1,head6);
Output(head6);
clearList(head6);
ifZero(head2,head6);
Output(head6);
clearList(head6);
ifZero(head3,head6);
Output(head6);
clearList(head6);
ifZero(head4,head6);
Output(head6);
clearList(head6);
ifZero(head5,head6);
Output(head6);
clearList(head6);
}
else if(n==0)
{
;
}
return 0;
} 已解决
页:
[1]