ugh this program is driving me up a wall. u said u got it to actually calculate numbers and display the answer correct. but I set type now........and it's not giving errors but it still wont work AGGHGGH
here is what i have thus far.
Code:
//Matt Smith
//Cs 215 Section 2
//Program 5
#include "CalcTree.h"
CalcTree::CalcTree()
{
root = NULL;
}
CalcTree::~CalcTree()
{
destHelper(root);
}
CalcTree::CalcTree(CalcTree& otherTree) //copy constructor
{
root = copyHelper(otherTree.root);
}
CTNode* CalcTree::copyHelper(CTNode *thatroot)
{
if(thatroot==NULL) return NULL;
CTNode *pwned;
pwned = new CTNode;
pwned->type = thatroot->type;
pwned->operand = thatroot->operand;
pwned->left=NULL; pwned->right=NULL;
if(( thatroot->left != NULL ) || ( thatroot->right != NULL ))
{
if( thatroot->left != NULL )
pwned->left = copyHelper(thatroot->left);
if( thatroot->right != NULL )
pwned->right = copyHelper(thatroot->right);
}
return pwned;
}
void CalcTree::destHelper( CTNode *thisroot )
{
if(thisroot==NULL) return;
else if(( thisroot->left != NULL ) || ( thisroot->right != NULL ))
{
if( thisroot->left != NULL )
{destHelper(thisroot->left);
thisroot->left=NULL;}
if( thisroot->right != NULL )
{destHelper(thisroot->right);
thisroot->right=NULL;}
}
delete thisroot;
}
void CalcTree::setRoot(NodeType type_, int operand_)
{
destHelper (root);
CTNode *temp = NULL;
temp= new CTNode;
temp-> type=type_;
temp-> operand=operand_;
temp->left=NULL;
temp->right=NULL;
root=temp;
delete temp;
}
void CalcTree::setLeft(CalcTree &otherTree)
{
// delete the old branch first
destHelper(root->left);
// copy the new tree as left branch
root->left= copyHelper(otherTree.root);
}
void CalcTree::setRight(CalcTree &otherTree)
{
// delete the old branch first
destHelper(root->right);
// copy the new tree as left branch
root->right= copyHelper(otherTree.root);
}
void CalcTree::printIN()
{
printINhelper(root);
}
void CalcTree::printINhelper(CTNode* thisroot)
{
if (thisroot == NULL)
return;
else if (thisroot->type == OPR)
cout << thisroot->operand;
else {
cout << "( ";
printINhelper (thisroot->left);
switch (thisroot->type){
case ADD: cout << " + ";
break;
case SUB: cout << " - ";
break;
case MUT: cout << " * ";
break;
case DIV: cout << " / ";
break;
}
printINhelper (thisroot->right);
cout << " )";
}
}
void CalcTree::printPOST( )
{
cout << "(";
printPOSThelper(root);
cout << ")";
}
void CalcTree::printPOSThelper(CTNode *thisroot){
if (thisroot->type == OPR)
cout << thisroot->operand;
else {
printPOSThelper (thisroot->left);
printPOSThelper (thisroot->right);
switch (thisroot->type){
case ADD: cout << " + ";
break;
case SUB: cout << " - ";
break;
case MUT: cout << " * ";
break;
case DIV: cout << " / ";
break;
}
}
}
float CalcTree::evaluate( ){
return evalHelper(root);
}
float CalcTree::evalHelper( CTNode *thisroot ){
if (thisroot == NULL)
return 0;
if (thisroot->type == OPR)
return (float (thisroot->operand));
else {
switch (thisroot->type){
case ADD: return (float (evalHelper(thisroot->left) + evalHelper(thisroot->right)));
case SUB: return (float (evalHelper(thisroot->left)- evalHelper(thisroot->right)));
case MUT: return (float (evalHelper(thisroot->left) * evalHelper(thisroot->right)));
case DIV:return (float (evalHelper(thisroot->left)/ evalHelper(thisroot->right)));
default: return 0;
}
}
}
i was hoping to get this done by tonight.