我正在用C++编写一个多项式程序,我们应该使用单链接列表来实现。是的,这是一项家庭作业。我已经完成了大部分程序,并且只对乘法运算符重载感到困惑。这是我的运算符*函数:
LinkedList operator*(const LinkedList& a, const LinkedList& b)
{
LinkedList product;
Node* nodeA = a.head;
Node* nodeB = b.head;
int coeff, powr;
if (nodeA == NULL && nodeB == NULL)
return product;
else if (nodeA == NULL && nodeB != NULL)
return b;
else if (nodeA != NULL && nodeB == NULL)
return a;
else {
while (nodeA != NULL) {
while (nodeB != NULL) {
coeff = nodeA->getCoeff() * nodeB->getCoeff();
powr = nodeA->getPow() + nodeB->getPow();
product.addElement(coeff, powr);
nodeB = nodeB->getNext();
}
nodeB = b.head;
nodeA = nodeA->getNext();
}
}
return product;
}
作为引用,我现在仅在链接列表的末尾添加一个新元素。
这是我的AddElement函数:
void LinkedList::addElement(int coeff, int powr)
{
Node *newNode = new Node();
// Set the Node's data
newNode->setPowAndCoefficient(coeff, powr);
newNode->setNextNode(NULL);
Node *temp = head;
if (temp != NULL) {
// Go to the last element of the list
while (temp->getNext() != NULL) {
temp = temp->getNext();
}
// temp is now the last element and its next element is null
// Set temp's next node to be the newNode
temp->setNextNode(newNode);
}
else
head = newNode;
}
节点只是我的课,拥有私有(private)数据成员的权限,能力和指向下一个节点的指针。 LinkedList是我的主要类,其中包括私有(private)Node *头成员和公共(public)运算符重载函数以及几个构造函数。这里使用的构造函数是默认的构造函数,其中我只是将head设置为NULL。
我在第二个while循环之后放了一些cout语句,然后将两个多项式相乘以测试我的乘法函数。
因此,在这种情况下,我的main.cpp文件中包含以下代码:
LinkedList poly1, poly2, result;
// The first polynomial: 3x^3 + 7x^2 - 7
poly1.addElement(3, 3);
poly1.addElement(7, 2);
poly1.addElement(-7, 0);
cout << "Polynomial A: " << poly1 << endl;
// The second polynomial: -5x^5 - 14x^3 + 7x^2 + 14
poly2.addElement(-5, 14);
poly2.addElement(-14, 3);
poly2.addElement(7, 2);
poly2.addElement(14, 0);
cout << "Polynomial B: " << poly2 << endl;
此外,<问题是当我尝试执行此操作时:
result = poly1 * poly2;
我遇到了细分错误。而且我不知道为什么。正如我所说的,我将cout语句放入第一个while循环中,这就是我执行poly1 * poly2时得到的结果:
-15x^17 - 42x^6 + 21x^5 + 42x^3 - 35x^16 - 98x^5 + 49x^4 + 98x^2 + 35x^14 + 98x^3 - 49x^2 - 98
[1] 39009 segmentation fault ~/Desktop/run
是的,这很丑陋,但这是在我将所有这些内容加在一起之前的。但是无论如何,这基本上是对的。在评估最后一个常数之后,我只是遇到了分段错误。
我不知道为什么要这么做,而只对乘法运算符这样做。其他东西虽然工作正常。我可能在某个地方有一个错误,过去几个小时一直在寻找它,但我不知道自己做错了什么。有人可以帮忙吗?
谢谢。
我的节点类:
class Node {
private:
int power;
int coefficient;
Node *next;
public:
Node(); // in implementation: coeff = 0, power = 0, next = NULL;
Node(const int coeff, const int powr = 1);
void setPowAndCoefficient(const int coeff, const int powr);
inline int getPow() const { return power; };
inline int getCoeff() const { return coefficient; };
inline void setNextNode(Node *aNode) { next = aNode; };
inline Node *getNext() const { return next; };
};
Node::Node()
{
coefficient = 0;
power = 1;
next = NULL;
}
Node::Node(const int coeff, const int powr)
{
coefficient = coeff;
power = powr;
next = NULL;
}
void Node::setPowAndCoefficient(const int coeff, const int powr)
{
coefficient = coeff;
power = powr;
next = NULL;
}
最佳答案
杜德
我有点懒得无法阅读整个帖子。
但是,这就是我将两个多项式相乘的方法..
LinkedList operator*(const LinkedList& a, const LinkedList& b){
int coef,pow;
LinkedList temp = new LinkedList();
for(node * a1 = a->head;a1!=NULL;a1=a1->next)
for(node * b1 = b->head;b1!=NULL;b1=b1->next){
coef = a1->getCoeff() * b1-> getCoeff();
pow = a1->getPow()+b1->getPow();
node ab = new node(coef,pow);//Writting it java style, cant remember if this is how u //declare objects in c++ :(
temp.addNode(ab);
}
return temp;
}
很抱歉,如果它没有帮助您..但是,我想向您介绍一个想法。
关于c++ - 在cpp链表程序中将两个多项式相乘,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12869125/