我必须用 C 编写一个程序,我必须添加两个单变量多项式。我可以部分做到,但我最终得到了错误的答案。
考虑两个多项式:
5x^2 + 6x^3 + 9
6x^3 + 5x^2 + 3x + 2
我知道答案会是什么,手动。这是我的逻辑:
if(term1->exp == term2->exp){ // same power of x
// add them, store them in the final answer linked list
// increment pointers of both the term1 and term2 linkedlist
}
if(term1->exp > term2->exp){ // term1 has higher power of x than term2
// increment term1 linked list in search of lower power of x
// ** term1 is now pending **
}
if(term1->exp < term2->exp){ // term1 has lesser power of x than term2
// increment term2 linked list in search of lower power of x
// ** term2 is now pending **
}
我面临的问题是未决条款。我如何处理未决条款?
如何正确添加它们?
代码在这里: http: //pastebin.com/70UJdNiQ