2015-12-01 101 views
0

我試圖學習評估表達式的二叉樹的實現。我無法運行它並查看輸出。我怎麼會得到3 *(7 + 1)/ 4 +(17-5),這將導致18 這裏是鏈接http://math.hws.edu/eck/cs225/s03/binary_trees/評估表達式二叉樹C++

class ExpNode { 
      // Represents a node of any type in an expression tree. 
      // This is an "abstract" class, since it contains an undefined 
      // function, value(), that must be defined in subclasses. 
      // The word "virtual" says that the defintion can change 
      // in a subclass. The "= 0" says that this function has 
      // no definition in this class. 

    public:  

     virtual double value() = 0; // Return the value of this node. 

    }; // end class ExpNode 


class ConstNode : public ExpNode { 
      // Represents a node that holds a number. (The 
      // ": public ExpNode" says that this class is 
      // a subclass of ExpNode.) 

     double number; // The number in the node. 

    public: 

     ConstNode(double val) { 
      // Constructor. Create a node to hold val. 
      number = val; 
     } 

     double value() { 
      // The value is just the number that the node holds. 
      return number; 
     } 

    }; // end class ConstNode 


class BinOpNode : public ExpNode { 
      // Represents a node that holds an operator. 

     char op;  // The operator. 
     ExpNode *left; // The left operand. 
     ExpNode *right; // The right operand. 

    public: 

     BinOpNode(char op, ExpNode *left, ExpNode *right) { 
      // Constructor. Create a node to hold the given data. 
      this->op = op; 
      this->left = left; 
      this->right = right; 
     } 

     double value() { 
      // To get the value, compute the value of the left and 
      // right operands, and combine them with the operator. 
      double leftVal = left->value(); 
      double rightVal = right->value(); 
      switch (op) { 
       case '+': return leftVal + rightVal; 
       case '-': return leftVal - rightVal; 
       case '*': return leftVal * rightVal; 
       case '/': return leftVal/rightVal; 
      } 
     } 

    }; // end class BinOpNode 

這是我試圖做一個主要功能:

int main() { 
    BinOpNode *opnode; 
    opnode = new BinOpNode; 
    opnode->value()=5; 
    ExpNode *expnode; 
    expnode = opnode; 
    expnode->value(); 
    return 0; 

} 

它不能編譯,這是錯誤

15:58:27 **** Incremental Build of configuration Debug for project ExpNode **** 
Info: Internal Builder is used for build 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\ExpNode.o" "..\\src\\ExpNode.cpp" 
..\src\ExpNode.cpp: In function 'int main()': 
..\src\ExpNode.cpp:60:15: error: no matching function for call to 'BinOpNode::BinOpNode()' 
..\src\ExpNode.cpp:36:2: note: candidates are: BinOpNode::BinOpNode(char, ExpNode*, ExpNode*) 
..\src\ExpNode.cpp:30:33: note:     BinOpNode::BinOpNode(const BinOpNode&) 
..\src\ExpNode.cpp:61:18: error: lvalue required as left operand of assignment 

15:58:28 Build Finished (took 405ms) 
+0

你怎麼*無法運行它,看到輸出*?它沒有編譯?如果沒有,你應該發佈編譯器錯誤。 – NathanOliver

+0

我剛編輯! – sm15

+1

@ sm15您正在構造一個沒有參數的'BinOpNode'對象:'opnode = new BinOpNode;'。看看你的'BinOpNode'類。你看到沒有這樣的構造函數沒有任何參數。這正是錯誤告訴你的。 – PaulMcKenzie

回答

0

在C++中,默認的構造函數有趣地工作。

如果不定義任何構造,爲您生成一個默認的構造函數:

class A {}; 

int main() 
{ 
    A a; // perfectly fine 

但是,如果你定義任何其他構造,這些構造產生走開:

class A 
{ 
    A(int) { ... } 
}; 

int main() 
{ 
    A a; // ERROR! 
} 

在在這種情況下,缺省構造函數不存在是因爲您定義了一個構造函數,編譯器也沒有爲您生成。

這是你的問題,因爲在這裏main,你有這樣一行:

opnode = new BinOpNode; 

它運行的BinOpNode默認構造函數。看看你BinOpNode構造函數:

BinOpNode(char op, ExpNode *left, ExpNode *right) 

嘿看:那不是一個默認的構造函數!

你有兩個選擇:要麼是默認的構造函數添加到類:

​​

或致電new時所使用的參數:

opnode = new BinOpNode(op, left, right); 

祝您好運!

0

無類的有默認構造函數。
value返回評估表達式的結果,並且在構建表達式時需要傳遞表達式的必要部分作爲其參數。
(目前還不清楚你如何指望能值5分配給二進制表達。)

你需要建立從葉一樹(這將是常數),邁向根。
作爲一個例子,這裏的表達5 + 3

ConstNode five(5); 
ConstNode three(3); 
BinOpNode fiveplusthree('+', &five, &three); 
std::cout << fiveplusthree.value(); // Should print 8 
0

我認爲這個問題是在你的main()函數的邏輯。

根據給定類別的定義,首先應該在表達式中爲每個數字創建一個類型爲ConstNode的對象。然後,您應該爲該表達式中的每個運算符創建BinOpNode

順便說一句,該表達式評估爲18,而不是82!

像這樣:

//3*(7+1)/4+(17-5) = 18 
int main() 
{ 
    BinOpNode *a, *b; 
    a = new BinOpNode('+', new ConstNode(7), new ConstNode(1)); 
    a = new BinOpNode('*', new ConstNode(3), a); 
    a = new BinOpNode('/', a, new ConstNode(4)); 
    b = new BinOpNode('-', new ConstNode(17), new ConstNode(5)); 
    b = new BinOpNode('+', a, b); 
    cout << b->value(); 
} 

PS:我們可以通過ConstNode類的對象時的ExpNode中的對象的BinOpNode作爲ConstNode繼承構造預計從ExpNode抽象基類。