gpt4 book ai didi

c++ - 链表加法/减法函数正在改变参数的值?

转载 作者:行者123 更新时间:2023-11-28 02:05:10 24 4
gpt4 key购买 nike

所以在我的 main 中,我有两个多项式 A 和 B,我试图将多项式相加,输出新的多项式,然后从 A 中减去多项式 B,然后输出。但问题是,在加法函数之后,当我调用减法函数时,它对求和多项式而不是新的多项式 A 和 B 执行减法。

main 中的调用

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

最佳答案

对我来说,当您执行 linkedList newPolynomial = polynomialA; 时,您并不是在创建新列表,而是使用 polynomialA 列表。

如果在加法之后打印 polynomialA,您会看到它已被修改,这就是为什么在减法之后会得到类似 0x^7 的原因。要保留您的算法,您应该复制 polynomialA 并将其用作 newPolynomial

关于c++ - 链表加法/减法函数正在改变参数的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37798226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com