3

I am currently writing a c++ for binary trees. The whole thing is written, however, no matter what expression I want to evaluate, I keep having the command prompt tell me -1.#IND. Any ideas on a fix for this or even what this means?

Thanks in advance

Code:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
template<typename T> struct TreeNode
{
    TreeNode(const T& value, TreeNode<T>* left = NULL, TreeNode<T>* right = NULL)
    {
        Value = value;
        Left = left;
        Right = right;
    }

    T Value;
    TreeNode<T>* Left;
    TreeNode<T>* Right;

    bool IsLeaf() const
    {
        return Left == NULL && Right == NULL;
    }
};


double ValueOf(TreeNode<char>* treeNode)
{


    if ( treeNode->IsLeaf() )
    {
        return treeNode->Value - '0';
    }
    else
    {
        switch(treeNode->Value)
        {
        case '+':
            return ValueOf(treeNode->Left) + ValueOf(treeNode->Right);
            break;

        case '-':
            return ValueOf(treeNode->Left) - ValueOf(treeNode->Right);
            break;

        case '*':
            return ValueOf(treeNode->Left) * ValueOf(treeNode->Right);
            break;

        case '/':
            return ValueOf(treeNode->Left) / ValueOf(treeNode->Right);
            break;
        }


    }
}



void main()
{
    string expression;

    cout << "Please enter an expression: ";

    cin >> expression;


    TreeNode<char> *newLeaf;
    TreeNode<char> *treeRoot;
    TreeNode<char> *currentNode;
    TreeNode<char> *newRoot;
    TreeNode<char> *newChild;


    treeRoot = NULL;
    currentNode = treeRoot;


    for (int i = 0; i < expression.length(); i++)
    {

        if ( (expression[i] >= 0 ) || ( expression[i] <= 9 ) ) 
        {

            newLeaf = new TreeNode <char> (expression[i]);


            if ( currentNode == NULL)
            {
                treeRoot = currentNode = newLeaf;
            }
            else
            {
                currentNode->Right = newLeaf;
            }
        }

        else if ( (( expression[i] == '+' || expression[i] == '-') || (expression[i] == '*' || expression[i] == '/' )) && currentNode->Right == NULL ) 
        {
            newRoot = new TreeNode <char> (expression[i]);
            newRoot->Left = treeRoot;
            treeRoot = newRoot;
            currentNode = newRoot;
        }

        else if (expression[i] == '*' || expression[i] == '/')
        {
            newChild = new TreeNode <char> (expression[i]);
            newChild->Left = currentNode->Right;
            currentNode->Right = newChild;
            currentNode = newChild;
        }
    } 

    double result = ValueOf(treeRoot);
    cout << "The result is: " << result << endl;
    system("pause");
}
Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
Mike
  • 51
  • 1
  • 2
  • 7
  • 3
    "I have a car, and no matter how I turn the key it won't turn on. Why?" Can't tell you without seeing the entire car...Show us your code, what your input is, what you expect, and what you get. – GManNickG Mar 23 '11 at 23:05

2 Answers2

3

It means that you do something illegal to a double or float (like taking the sqrt of a negative number). See also here: http://www.johndcook.com/IEEE_exceptions_in_cpp.html

ChrisWue
  • 18,612
  • 4
  • 58
  • 83
3

Your ValueOf function will silently return random nonsense if it happens to be passed something that isn't a leaf and doesn't have value one of the four arithmetic-op characters. In that case, trying to display that random nonsense can produce all sorts of crazy results. This wouldn't matter if the rest of the code guaranteed that that couldn't happen. Unfortunately ...

The code that -- I assume -- is intended to parse expressions can very easily produce such things. For instance, suppose you enter the expression 12. Then first of all a node containing just 1 is created; then a 2 is created and made the right child of the 1 node. Which means that the latter is no longer considered a leaf. Bang.

Now, of course that isn't the sort of expression you had in mind. So why doesn't it work OK when you enter, say, 1+2? Well, here's the main thing that's killing you:

if ( (expression[i] >= 0 ) || ( expression[i] <= 9 ) ) 

That should be &&, not ||. So the other bits of your parser never get used at all! (And of course you end up with a tree that doesn't make the slightest bit of sense.)

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • You have another problem here, even if you change || to &&, because your input data are characters, but you are comparing them to numeric values. The characters '0..'9' do not lie in the range 0..9, therefore you will never construct a leaf node for your numbers. – Joel Lee Mar 23 '11 at 23:51
  • Well thank you very much, now that has fixed the -1#IND, however now no matter the expression I enter it outputs BinaryTree has stopped working in the command prompt. Thank you for the very detailed answer though. – Mike Mar 23 '11 at 23:52
  • (Did you replace numbers with characters as per Joel's remark?) – Gareth McCaughan Mar 23 '11 at 23:54
  • I find that after fixing the `||` bug and single-quoting those digits, at least some expressions are correctly parsed and evaluated. One simple one that isn't is `1*2+3`. Here's a hint for figuring out what goes wrong there: when the `+` is seen, `currentNode->Right` is not null. – Gareth McCaughan Mar 23 '11 at 23:58
  • Thanks for all the input guys. For converting the numeric values to characters would you do something like char *a[9] and then a[0] = b; Or anything like that? @Gareth, I will attempt to solve that problem after I have mine actually working. Thank you though. – Mike Mar 24 '11 at 02:03
  • Nevermind figured that out, now @Gareth McCaughan I see what you mean now. Any time * or / is in the expression it outputs the wrong answer. I will try to figure out what you mean and let you know how it goes. Thanks again – Mike Mar 24 '11 at 02:23
  • I figured it out! It was a bracket problem in the else if statement. currentNode->Right is NULL as far as I can tell. However thanks for all the help guys. Feels good to have this working. – Mike Mar 24 '11 at 02:35