2015-12-04 10:16

# C语言 多项式加法中的问题

#include
#include

struct PolyNode{
int coef; //系数
int expon; //指数

};
typedef struct PolyNode *Polynomial;
Polynomial P1, P2, P3, P4,P5;

void Attach(int c, int e, Polynomial *pRear)
{
Polynomial P;
P = (Polynomial)malloc(sizeof(struct PolyNode));
P->coef = c;
P->expon = e;
*pRear = P;
}
int Commpare(int a, int b)
{
if (a>b)
return 1;
else if (a == b)
return 0;
else
return -1;
}
{
Polynomial front, rear, temp;
int sum;
rear = (Polynomial)malloc(sizeof(struct PolyNode));
front = rear;
while (P1&&P2)
{
switch (Commpare(P1->expon, P2->expon)){
case 1:
Attach(P1->coef, P1->expon, &rear);
break;
case -1:
Attach(P2->coef, P2->expon, &rear);
break;
case 0:
sum = P1->coef + P2->coef;
if (sum)Attach(sum, P1->expon, &rear);
break;
}
for (; P1; P1 = P1->link)Attach(P1->coef, P1->expon, &rear);
for (; P2; P2 = P2->link)Attach(P2->coef, P2->expon, &rear);
free(temp);
return front;
}
printf("wrong!");
}
main()
{
P2 = (Polynomial)malloc(sizeof(struct PolyNode));
P2->coef = 1;
P2->expon = 1;

``````P1 = (Polynomial)malloc(sizeof(struct PolyNode));
P1->coef = 2;
P1->expon = 2;

P4 = (Polynomial)malloc(sizeof(struct PolyNode));
P4->coef = 1;
P4->expon =1;

P3 = (Polynomial)malloc(sizeof(struct PolyNode));
P3->coef = 2;
P3->expon = 2;

P5 = (Polynomial)malloc(sizeof(struct PolyNode));

while (P5!=NULL)
{
printf("%dX%d ", P5->coef, P5->expon);
}
``````

}

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• 程序太长我没仔细看
不过
```typedef struct PolyNode *Polynomial; 应该放在``` struct PolyNode{
`int coef; //系数`int expon; //指数
`struct PolyNode *link;//指向下一个节点的指针`};
之前

点赞 评论 复制链接分享