所以在我的主要部分,我有两个多项式A和B,我试图添加多项式,输出新的多项式,然后从A中减去多项式B然后输出。但问题是在加法函数之后,当我调用减法函数时,它在求和的多项式上执行减法而不是新的多项式A和B.
主要
中的电话outfile << "Printing addition of the first polynomial and the second polynomial" << endl << endl;
linkedList polynomialD;
polynomialD = polynomialD.polynomialAddition(polynomialA, polynomialB);
polynomialD.printList(outfile);
outfile << endl;
outfile << "Printing subtraction of the second polynomial from the first polynomial" << endl << endl;
linkedList polynomialE;
polynomialE = polynomialE.polynomialSubtraction(polynomialA, polynomialB);
polynomialE.printList(outfile);
这是我的程序输出的内容:
Polynomial in canonical form -
(6x^6) + (9x^2) + (-5)
Polynomial in canonical form -
(7x^7) + (2x^5) + (7x^2) + (12)
Printing addition of the first polynomial and the second polynomial
Polynomial in canonical form -
(7x^7) + (6x^6) + (2x^5) + (16x^2) + (7)
Printing subtraction of the second polynomial from the first polynomial
Polynomial in canonical form -
(0x^7) + (6x^6) + (0x^5) + (9x^2) + (-5)
但它应该打印出来:
Polynomial in canonical form -
(6x^6) + (9x^2) + (-5)
Polynomial in canonical form -
(7x^7) + (2x^5) + (7x^2) + (12)
Printing addition of the first polynomial and the second polynomial
Polynomial in canonical form -
(7x^7) + (6x^6) + (2x^5) + (16x^2) + (7)
Printing subtraction of the second polynomial from the first polynomial
Polynomial in canonical form -
(-7x^7) + (6x^6) + (-2x^5) + (2x^2) + (-17)
由于某种原因,加法函数使其表现得像它存在,所以当它试图进行减法时,它会发现当前存在的节点,它不应该存在。
//Function to insert into the linked list
void listInsert(int coefficient, int exponent)
{
Node *spot = findSpot (coefficient, exponent);
if(spot->exponent == exponent )
{
int temp;
temp = spot->coefficient + coefficient;
spot->coefficient = temp;
}
else
{
Node* newNode = new Node(coefficient, exponent);
newNode->next = spot->next;
spot->next = newNode;
}
}
//Function used to insert into the linked list but subtracting like powers.
void listInsertSubtraction(int coefficient, int exponent)
{
Node *spot = findSpot (coefficient, exponent);
if(spot->exponent == exponent )
{
int temp;
temp = spot->coefficient - coefficient;
spot->coefficient = temp;
}
else
{
int tempcoeff;
tempcoeff = -coefficient;
Node* newNode = new Node(coefficient, exponent);
newNode->next = spot->next;
spot->next = newNode;
}
}
//Function to add two polynomials.
linkedList polynomialAddition (linkedList& polynomialA, linkedList& polynomialB)
{
linkedList newPolynomial = polynomialA;
//Temporary Nodes point to the first element of each linked list.
Node* tempNodeB = polynomialB.listHead->next;
while (tempNodeB != NULL)
{
newPolynomial.listInsert (tempNodeB->coefficient, tempNodeB->exponent);
tempNodeB = tempNodeB->next;
}
return newPolynomial;
}
//Function to subtract the 2nd polynomial from the first polynomial.
linkedList polynomialSubtraction (linkedList& polynomialA, linkedList& polynomialB)
{
linkedList newPolynomial = polynomialA;
//Temporary Nodes point to the first element of each linked list.
Node* tempNodeB = polynomialB.listHead->next;
while (tempNodeB != NULL)
{
newPolynomial.listInsertSubtraction (tempNodeB->coefficient, tempNodeB->exponent);
tempNodeB = tempNodeB->next;
}
return newPolynomial;
}
我尝试乱搞并更改通过引用传递的参数,作为指针,将返回更改为指针而不是值,但是当我尝试运行它时,它只是说运行失败。
编辑:添加findSpot功能
Node* findSpot (int coefficient, int exponent)
{
Node *spot = listHead;
while(spot->next != NULL && spot->next->exponent >= exponent)
{
spot = spot->next;
}
return spot;
}
如果需要,还有一个指向整个代码的pastebin链接 - http://pastebin.com/yL85Xif2
答案 0 :(得分:2)
当您执行linkedList newPolynomial = polynomialA;
时,对我来说,您不是使用polynomialA
列表创建新列表。
如果您在添加后打印polynomialA
,则会看到它已被修改,这就是为什么在您减去后会得到0x^7
之类的内容。
为了保留您的算法,您应该复制polynomialA
并将其用作newPolynomial
。