#include #include #include "node.h" #include "globals.h" #include "binaryNode.h" #include "node.h" #include BinaryOperator::BinaryOperator() :myLHS(new Constant(0)), myRHS(new Constant(0)), myPrecedence(0) { } BinaryOperator::BinaryOperator (Node * lhs, Node * rhs) : myLHS(lhs), myRHS(rhs), myPrecedence(0) {} BinaryOperator::BinaryOperator (Node * lhs, Node * rhs, int precedence) :myLHS(lhs), myRHS(rhs), myPrecedence(precedence) { } BinaryOperator:: ~BinaryOperator () { delete myLHS; delete myRHS; } void BinaryOperator::print (ostream & out) const { out << "("; myLHS->print(out); out << description(); myRHS->print(out); out << ")"; } int BinaryOperator::precedence()const { return myPrecedence; } void BinaryOperator::Left(Node* lhNode) { myLHS=lhNode; } void BinaryOperator::Right(Node* rhNode) { myRHS=rhNode; } // define a subclass of Operator for each of +, -, *, / Plus::Plus() :BinaryOperator( new Constant(0),new Constant(0),ADD_PRECEDENCE) { } Plus::Plus (Node * lhs, Node * rhs) : BinaryOperator(lhs, rhs, ADD_PRECEDENCE) { } double Plus::evaluate () const { return myLHS->evaluate() + myRHS->evaluate(); } Node * Plus::copy () const { return new Plus(); } string Plus::description () const { return " + "; } Node* Plus::takeDerivative(string & key) { //Returns derivative of right plus derivative of left return(new Plus(myLHS->takeDerivative(key),myRHS->takeDerivative(key))); } Minus::Minus() :BinaryOperator(new Constant(0),new Constant(0),SUBTRACT_PRECEDENCE) { } Minus::Minus (Node * lhs, Node * rhs) : BinaryOperator(lhs, rhs, SUBTRACT_PRECEDENCE) { } double Minus::evaluate () const { return myLHS->evaluate() - myRHS->evaluate(); } Node * Minus::copy () const { return new Minus(); } string Minus::description () const { return " - "; } Node* Minus::takeDerivative(string & key) { //Returns derivative of left minus derivative of right return(new Minus(myLHS->takeDerivative(key),myRHS->takeDerivative(key))); } Times::Times() :BinaryOperator(new Constant(0),new Constant(0),TIMES_PRECEDENCE) { } Times::Times (Node * lhs, Node * rhs) : BinaryOperator(lhs, rhs,TIMES_PRECEDENCE) { } double Times::evaluate () const { return myLHS->evaluate() * myRHS->evaluate(); } Node * Times::copy () const { return new Times(); } string Times::description () const { return " * "; } Node*Times:: takeDerivative(string & key) { //product rule cout<evaluate()<<" "<description()<takeDerivative(key)),new Times(myLHS->takeDerivative(key),myRHS))); } Divide::Divide() :BinaryOperator(new Constant(0),new Constant(0),DIV_PRECEDENCE) { } Divide::Divide (Node * lhs, Node * rhs) : BinaryOperator(lhs, rhs,DIV_PRECEDENCE ) { } double Divide::evaluate () const { return myLHS->evaluate() / myRHS->evaluate(); } Node * Divide::copy () const { return new Divide(); } string Divide::description () const { return " / "; } Node* Divide::takeDerivative(string & key) { //Quotient rule return(new Divide(new Minus(new Times(myRHS,myLHS->takeDerivative(key)),new Times(myLHS,myRHS->takeDerivative(key))),new Pow(myRHS,new Constant(2)))); } Mod::Mod() :BinaryOperator(new Constant(0),new Constant(0),MOD_PRECEDENCE) { } Mod::Mod (Node * lhs, Node * rhs) : BinaryOperator(lhs, rhs,MOD_PRECEDENCE) { } double Mod::evaluate () const { int temp =myLHS->evaluate(); int temp2= myRHS->evaluate(); if(temp<0) { temp=temp*-1; cout<<"Attempt to mod with negative number, forced conversion to positive."<evaluate(); if(temp<0) { temp=temp*(-1); cout<<"Attempt to raise number to negative power. Forced conversion to positive."<evaluate(); } return result; } Node * Pow::copy () const { return new Pow(); } string Pow::description () const { return " ^ "; } Node* Pow::takeDerivative(string & key) { //Returns derivative of a power return(new Times(new Times(myRHS,new Pow(myLHS, new Constant(myRHS->evaluate()-1))),myLHS->takeDerivative(key))); }