#include #include #include "node.h" #include "globals.h" #include "unaryNode.h" #include "binaryNode.h" #include /* *Written by Jessica Smith from code adapted from code written by rcd. */ UnaryOperator::UnaryOperator() :myLHS(new Constant(0)), myPrecedence(UNARY_PRECEDENCE) { } UnaryOperator::UnaryOperator (Node * lhs) : myLHS(lhs), myPrecedence(UNARY_PRECEDENCE) {} UnaryOperator:: ~UnaryOperator () { delete myLHS; } void UnaryOperator::print (ostream & out) const { out << "("; myLHS->print(out); out << description(); out << ")"; } int UnaryOperator::precedence()const { return myPrecedence; } void UnaryOperator::Left(Node* lhNode) { myLHS=lhNode; } Negative::Negative() :UnaryOperator() { } Negative::Negative (Node * lhs) : UnaryOperator(lhs) { } double Negative::evaluate () const { return (myLHS->evaluate())*(-1); } Node * Negative::copy () const { return new Negative(); } string Negative::description () const { return " - "; } Node* Negative::takeDerivative(string & key) { //returns - of it return( new Negative(myLHS->takeDerivative(key))); } Factorial::Factorial() :UnaryOperator() { } Factorial::Factorial (Node * lhs) : UnaryOperator(lhs) { } double Factorial::evaluate () const { double temp =myLHS->evaluate(); if(temp<0) { temp=-1*temp; cout<<"Attempt to take factorial of negative number. Forced conversion to positive."<0;k--) { factorial*=k; } return factorial; } Node * Factorial::copy () const { return new Factorial(); } string Factorial::description () const { return " - "; } Sqrt::Sqrt() :UnaryOperator() { } Sqrt::Sqrt (Node * lhs) : UnaryOperator(lhs) { } double Sqrt::evaluate () const { if(myLHS->evaluate()>=0) { return sqrt(myLHS->evaluate()); } else { cout<<"Attempt to take sqrt of negative number. Forced change to positive."<evaluate())); } } Node * Sqrt::copy () const { return new Sqrt(); } string Sqrt::description () const { return " sqrt "; } Node* Sqrt::takeDerivative(string & key) { //returns deriv of square root return(new Divide(new Times(new Constant(.5), myLHS->takeDerivative(key)),new Sqrt(myLHS))); } Abs::Abs() :UnaryOperator() { } Abs::Abs (Node * lhs) : UnaryOperator(lhs) { } double Abs::evaluate () const { if(myLHS->evaluate()>=0) return myLHS->evaluate(); return (myLHS->evaluate()*(-1)); } Node * Abs::copy () const { return new Abs(); } string Abs::description () const { return " abs "; } Sin::Sin () : UnaryOperator() { } Sin::Sin (Node * lhs) : UnaryOperator(lhs) { } double Sin::evaluate() const { // sin of a negative number is possible return sin(myLHS->evaluate()); } Node * Sin::copy() const { return new Sin(); } string Sin::description() const { return " sin "; } Node* Sin::takeDerivative(string & key) { //take deriv of sin return( new Times(new Cos(myLHS),myLHS->takeDerivative(key))); } Cos::Cos () : UnaryOperator() { } Cos::Cos (Node * lhs) : UnaryOperator(lhs) { } double Cos::evaluate() const { // cos of a negative number is possible return cos(myLHS->evaluate()); } Node * Cos::copy() const { return new Cos(); } string Cos::description() const { return " cos "; } Node* Cos:: takeDerivative(string & key) { //deriv of sin return( new Negative(new Times(new Sin(myLHS),myLHS->takeDerivative(key)))); } Tan::Tan () : UnaryOperator() { } Tan::Tan (Node * lhs) : UnaryOperator(lhs) { } double Tan::evaluate() const { // tan of a negative number is possible return tan(myLHS->evaluate()); } Node * Tan::copy() const { return new Tan(); } string Tan::description() const { return " tan "; } Node* Tan::takeDerivative(string & key) { //deriv of tan return( new Times(new Plus(new Constant(1),new Pow(new Tan(myLHS),new Constant(2))),myLHS->takeDerivative(key))); } LN::LN () : UnaryOperator() { } LN::LN (Node * lhs) : UnaryOperator(lhs) { } double LN::evaluate() const { // ln return log(myLHS->evaluate()); } Node * LN::copy() const { return new LN(); } string LN::description() const { return " log base e"; } Node* LN::takeDerivative(string & key) { //deriv of ln return( new Divide(myLHS->takeDerivative(key),myLHS)); } Exp::Exp () : UnaryOperator() { } Exp::Exp (Node * lhs) : UnaryOperator(lhs) { } double Exp::evaluate() const { // ln return exp(myLHS->evaluate()); } Node * Exp::copy() const { return new Exp(); } string Exp::description() const { return " exp"; } Node* Exp::takeDerivative(string & key) { //deriv of ln return( new Times(new Exp(myLHS),myLHS->takeDerivative(key))); }