使用Linked List在C ++中进行表达式评估

时间:2013-10-06 00:56:17

标签: c++ class pointers

假设给出了2个多项式表达式,我试图编写一个执行3种不同操作的程序:1.match(相等)2.sum(加法)和3.dot(乘法)。

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开头的指针 有帮助吗?

1 个答案:

答案 0 :(得分:0)

只需更正while语句

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

还要确保Node可以LinkedList.cpp访问LinkedList.h(因此,此cpp包含#include <conio.h>中的istance)

错误是:

  

main.cpp:在成员函数'bool LinkedList :: match(LinkedList *,   LinkedList *)':main.cpp:78:错误:预期的primary-expression之前   '}'标记main.cpp:78:错误:预期';'在'}'标记

之前

此处您不需要{{1}}