簡體   English   中英

計算表達式二叉樹C ++

[英]evaluate expression binary tree c++

我正在嘗試學習這種對表達式進行評估的二叉樹的實現。 我無法運行它並查看輸出。 我將如何獲得3 *(7 + 1)/ 4 +(17-5),結果為18。這是鏈接http://math.hws.edu/eck/cs225/s03/binary_trees/

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)

沒有一個類具有默認構造函數。
value返回評估表達式的結果,並且在構造表達式時,需要將表達式的必要部分作為其參數傳遞。
(尚不清楚如何期望將值5分配給二進制表達式。)

您需要從葉子(將是常量)到根部構建一棵樹。
例如,這是表達式5 + 3

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

我認為問題出在您的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:當ExpNode的構造函數中需要BinOpNode的對象時,我們可以傳遞ConstNode類的對象,因為ConstNode繼承自ExpNode抽象基類。

在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構造函數:

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

嘿,這不是默認的構造函數!

您有兩個選擇:向該類添加默認構造函數:

BinOpNode() { ... }

或在調用new時使用參數:

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

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM