-1 #Ind Question
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...
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");
}
2 Answers
It means that you do something illegal to a double or float (like taking the sqrt of a negative number). See also here:
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.)