0

假设给定 2 个多项式表达式,我正在尝试编写一个执行 3 种不同操作的程序:1.match(equality) 2.sum(addition) 和 3.dot(multiplication)。

class Node
{
public:
int coef;
int expo;
Node *next;
Node(int coef=0, int expo=0, Node *next=NULL)
{
    this->coef = coef;
    this->expo = expo;
    this->next = next;
}
};
//
class LinkedList
{
public:
Node *head;
int size;
LinkedList()
{
    head = new Node(0, 0, NULL);
    head->coef = 0;
    head->expo = 0;
    head->next = NULL;
    size = 0;
}
int degree();
int coefficient(int);
bool match(LinkedList *, LinkedList*);
void insert();
LinkedList sum(LinkedList *, LinkedList *);
LinkedList dot(LinkedList *, LinkedList *);
};LinkedList x, y, z;

我还定义了 memeber 函数,如下所示:

bool LinkedList::match(LinkedList *expr1, LinkedList *expr2)
{
// Check if both expressions have a same length
if (expr1->size != expr2->size)
{
    cout << "The expressions do not match in length." << endl;
    return false;
}
else if (expr1->size == expr2->size)
{
    // Both expressions are the same in length, but not equal
    while (expr1->head->coef && expr2->head->coef)
}
}

问题是我无法访问 expr1 和 expr2 内的节点,它们是指向 expr1 和 expr2 开头的指针有什么帮助吗?

4

1 回答 1

0

只需更正 while 语句

    while (expr1->head->coef && expr2->head->coef) {
        // do something                            ^
    }
    ^

还要确保Node可以从LinkedList.cpp(所以只是LinkedList.h这个cpp包括)

错误是:

main.cpp: 在成员函数'bool LinkedList::match(LinkedList*, LinkedList*)'中:main.cpp:78: error: '}' token main.cpp:78 之前的预期主表达式:错误:预期'; ' 在 '}' 标记之前

你也不需要#include <conio.h>这里

于 2013-10-06T01:30:20.673 回答