#include #include #include "node.h" #include "globals.h" #include "bFuncNode.h" #include BinaryFunction::BinaryFunction() :myLHS(new Constant(0)), myRHS(new Constant(0)), myPrecedence(BINARY_FUNCTION_PRECEDENCE) { } BinaryFunction::BinaryFunction (Node * lhs, Node * rhs) : myLHS(lhs), myRHS(rhs), myPrecedence(BINARY_FUNCTION_PRECEDENCE) {} BinaryFunction:: ~BinaryFunction () { delete myLHS; delete myRHS; } void BinaryFunction::print (ostream & out) const { out << "("; myLHS->print(out); out << description(); myRHS->print(out); out << ")"; } int BinaryFunction::precedence()const { return myPrecedence; } void BinaryFunction::Left(Node* lhNode) { myLHS=lhNode; } void BinaryFunction::Right(Node* rhNode) { myRHS=rhNode; } Log::Log() :BinaryFunction() { } Log::Log(Node * lhs, Node * rhs) :BinaryFunction(lhs, rhs) { } double Log::evaluate() const { double base = myRHS->evaluate(); if(base<0) { base=base*(-1); cout <<"Attempt to take log with respect to a negative base. Forced conversion to positive." << endl; } double constant = myLHS->evaluate(); double result; result = log(constant)/log(base); return result; } Node * Log::copy() const { return new Log(); } string Log::description() const { return " log "; } Derivative::Derivative() :BinaryFunction() { } Derivative::Derivative(Node * lhs, Node * rhs) :BinaryFunction(lhs, rhs) { } double Derivative::evaluate() const { return (myLHS->evaluate()); } Node * Derivative::copy() const { return new Derivative(); } string Derivative::description() const { return "d"; } Node * Derivative::takeDerivative(string & key) { key=myRHS->description(); return (myLHS->takeDerivative(key)); } Function::Function() :BinaryFunction() { } Function::Function(Node * lhs, Node * rhs) :BinaryFunction(lhs, rhs) { } double Function::evaluate() const { return (myLHS->evaluate()); } Node * Function::copy() const { return new Function(); } string Function::description() const { return "f"; }