| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

已结贴   问题点数：20  回复次数：2

#include<stdio.h>
#include<malloc.h>
typedef struct node{
float coef;
int expn;
node *next;
}Lnode, * Dxs;
Dxs create();
void printDxs(Dxs h);
void deleteNode(Dxs h, Dxs p);
int main()
{
Dxs ha, hb, hc;
printf("请依次输入第一个多项式的系数和指数逗号隔开,以-1，-1结束\n");
ha = create();
printf("请依次输入第二个多项式的系数和指数逗号隔开,以-1，-1结束\n");
hb = create();
printf("输入的第一个多项式是: ");
printDxs(ha->next);
printf("输入的第二个多项式是: ");
printDxs(hb->next);
printf("两个多项式的和为: ");
printDxs(hc->next); return 0;
} //创建链表(读入数据以 0 0 结束)
Dxs create()
{
float coef;
int expn;
Dxs first, qa, s;
first = (Dxs)malloc(sizeof(Lnode));
first->coef = -1;
first->expn = -1;
first->next = NULL;
qa = first;
while(1)
{ scanf("%f,%d", &coef;&expn;)
if(coef == -1 && expn == -1)
{ break; }
s = (Dxs)malloc(sizeof(Lnode));
s->coef = coef;
s->expn = expn;
s->next = NULL;
qa->next = s;
qa = s;
}
return first;
} //链表相加
{
Dxs firstc, ha, hb, pc, s;
int a, b;
float sum;
firstc = (Dxs)malloc(sizeof(Lnode));
firstc->coef = -1;
firstc->expn = -1;
firstc->next = NULL;
pc = firstc; ha = firsta->next;
hb = firstb->next;
while(ha!= NULL && hb != NULL)
{ a = ha->expn;
b = hb->expn;
if(a>b)
{ //将a加入c中,移动a和c的指针
pc->next = ha;
pc = pc->next;
ha = ha->next;
}
else if(a > b)
{ //将b加入c中,移动b和c的指针
pc->next = hb;
pc = pc->next;
hb = hb->next;
}
else
{
sum = ha->coef + hb->coef;
if(sum != 0.0)
{ //将和加入a中,再将a加入c中,移动c的指针
ha->coef = sum;
pc->next = ha;
pc = pc->next;
}else
{ //查找删除A中系数之和为0的那个节点
s = firsta;
while(s != ha)
{ s = s->next;
}
s->next = ha->next;
} //ab已经处理完成,同时后移一位
ha = ha->next;
hb = hb->next;
}
} //将剩余部分加入c后面
if(ha != NULL)
{ pc->next = ha; }
if(hb != NULL)
{ pc->next = hb; }
return firstc;
} //遍历显示链表
void printDxs(Dxs h)
{ while(h != NULL)
{ printf("%0.2f*X^%d", h->coef, h->expn);
if(h->next)
{
printf("+");
}
h=h->next;
}
printf("\n");
}

得分:0
aide上提示是第一句错误，但是不知道怎么改
得分:20

φ(゜▽゜*)♪
• 3
• 1/1页
• 1