为什么我的evaluate()总是返回1或0,无法评估输入前缀表达式

时间:2016-11-26 21:52:52

标签: c++ tree expression prefix evaluate

我在我的ExprTree()类中遇到了我的evaluate()问题。这个函数总是只返回两个结果,即1或0.我不知道为什么它总是这样。它应该计算输入前缀表达式的结果,但事实并非如此。我花了整整一个时间来弄清楚这个问题,但没有结果。有人可以帮我解决这个问题。非常感谢!

附加的是使用evaluate()辅助函数的evaluate()。

/*
 //Data Members  
class ExprTreeNode   // inner node class
{
  public:
    /*---   Constructor   ---*/
    ExprTreeNode(char elem, ExprTreeNode *leftPtr, ExprTreeNode *rightPtr);

    /*---   Data Members   ---*/
    char dataItem;          // Expression tree data item
    ExprTreeNode* left;     // Pointer to the left child
    ExprTreeNode* right;    // Pointer to the right child
};
*/

/*Evaluates the value of the corresponding arithmetic expression.
*/
template <typename DataType>
DataType ExprTree<DataType>::evaluate() const throw(logic_error)
{
    //Throws logic error when the expression tree is empty.
    if (root == NULL)
    {
        throw logic_error("The expression tree is empty");
    }
    return evaluateHelper(root);
}

/*Helper function for the evaluate() function. Returns the vlaue od subtree pointed to by node
*/
template <typename DataType>
DataType ExprTree<DataType>::evaluateHelper(ExprTreeNode *node) const
{
    DataType l, r, result;
    if (isdigit(node->dataItem))
        //converts char to number
        result = node->dataItem - '0';
    else
    {
        //evaluates left subtree
        l = evaluateHelper(node->left);
        //evaluates right subtree
        r = evaluateHelper(node->right);
        //sum the left and right
        switch (node->dataItem)
        {
        case '+': result = l + r; break;
        case '-': result = l - r; break;
        case '*': result = l * r; break;
        case '/': result = l / r;
        }
    }
    return result;
}


//This is my main,please only read the test 1. When I use the input 4, the result is 1; when the input is the prefix expression +34, the result is 1 as well.
//--------------------------------------------------------------------
//
// 
//
//  Test program for the operations in the Expression Tree ADT
//
//--------------------------------------------------------------------

#include <iostream>
#include <stdexcept>

using namespace std;

#include "ExprTree.cpp"
//#include "ExpressionTree.cpp"
#include "config.h"

//--------------------------------------------------------------------
//  Function prototype

template <typename DataType>
void dummy(ExprTree<DataType> copyTree);   // copyTree is passed by value

                                           //--------------------------------------------------------------------

int main()
{
#if !LAB8_TEST1 || LAB8_TEST2 || LAB8_TEST3
    // Don't do this if testing boolean tree, unless also testing programming 
    // exercises 2 or 3 (for which this section is mostly needed).
    // The tricky part occurs if testing exercise 1 and (2 or 3), or if
    // someone is trying to test the basic class and one of the other exercises
    // in parallel. Hence the #if expression above.
    cout << "Start of testing the basic expression tree" << endl;
    ExprTree<float> testExpression;  // Test expression

    cout << endl << "Enter an expression in prefix form : ";

    testExpression.build();
    testExpression.showStructure();
    testExpression.expression();
    cout << " = " << testExpression.evaluate() << endl;

    // Test the copy constructor.
    dummy(testExpression);
    cout << endl << "Original tree:" << endl;
    testExpression.showStructure();
#endif

#if LAB8_TEST1
    cout << "Start of testing the boolean expression tree" << endl;
    ExprTree<bool> boolTree;
    cout << endl << "Enter a boolean expression in prefix form : ";
    boolTree.build();
    boolTree.showStructure();
    boolTree.expression();
    cout << " = " << boolTree.evaluate() << endl;
    cout << "** End of testing the boolean expression tree" << endl;
#endif

#if LAB8_TEST2
    cout << "Start of testing commute()" << endl;
    testExpression.commute();
    cout << endl << "Fully commuted tree: " << endl;
    testExpression.showStructure();
    testExpression.expression();
    cout << " = " << testExpression.evaluate() << endl;
    cout << "End of testing commute()" << endl;
#endif

#if LAB8_TEST3
    cout << "Start of testing isEquivalent()" << endl;
    ExprTree<float> same = testExpression;
    cout << "same is equal (tests copy constructor) ?  ";
    cout << (same.isEquivalent(testExpression) ? "Yes" : "No") << endl;

    ExprTree<float> empty;
    cout << "empty is equal?  ";
    cout << (empty.isEquivalent(testExpression) ? "Yes" : "No") << endl;

    ExprTree<float> userExpression;
    cout << "Enter another expression in prefix form: ";
    userExpression.build();
    cout << "new expression is equal?  ";
    cout << (userExpression.isEquivalent(testExpression) ? "Yes" : "No") << endl;
    cout << "** End of testing isEquivalent()" << endl;
#endif

#if !LAB8_TEST1 && !LAB8_TEST2 && !LAB8_TEST3
    // Don't bother with this if testing any of the programming exercises
    cout << endl << "Clear the tree" << endl;
    testExpression.clear();
    testExpression.showStructure();
    cout << "** End of testing the basic expression tree" << endl;
#endif

    return 0;
}

//--------------------------------------------------------------------

template <typename DataType>
void dummy(ExprTree<DataType> copyTree)

// Dummy routine that is passed an expression tree using call by
// value. Outputs copyTree and clears it.

{
    cout << endl << "Copy of tree:  " << endl;
    copyTree.showStructure();
    copyTree.clear();
    cout << "Copy cleared:   " << endl;
    copyTree.showStructure();
}

0 个答案:

没有答案