diff --git a/main.cpp b/main.cpp index d4fe9b7..b588766 100644 --- a/main.cpp +++ b/main.cpp @@ -64,31 +64,31 @@ int main() else if(p1=="SIN") { cin>>p2; - gra.push(new Sin(name,gra[p2])); + gra.push(new Opn(name,gra[p2])); continue; } else if(p1=="EXP") { cin>>p2; - gra.push(new Exp(name,gra[p2])); + gra.push(new Opn(name,gra[p2])); continue; } else if(p1=="LOG") { cin>>p2; - gra.push(new Log(name,gra[p2])); + gra.push(new Opn(name,gra[p2])); continue; } else if(p1=="TANH") { cin>>p2; - gra.push(new Tanh(name,gra[p2])); + gra.push(new Opn(name,gra[p2])); continue; } else if(p1=="SIGMOID") { cin>>p2; - gra.push(new Sigmoid(name,gra[p2])); + gra.push(new Opn(name,gra[p2])); continue; } else @@ -97,39 +97,39 @@ int main() cin>>p2; if(type=="+") { - gra.push(new sum(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="-") { - gra.push(new subtraction(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="*") { - gra.push(new multiply(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="/") { - gra.push(new division(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type==">") { - gra.push(new GTR(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="<") { - gra.push(new LSS(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type==">=") { - gra.push(new GEQ(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="<=") { - gra.push(new LEQ(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else if(type=="==") { - gra.push(new EQU(name,gra[p1],gra[p2])); + gra.push(new Tpn(name,gra[p1],gra[p2])); } else assert(0); } diff --git a/node.cpp b/node.cpp index ea7a80f..a6472d3 100644 --- a/node.cpp +++ b/node.cpp @@ -1,84 +1,84 @@ -#include "node.h" -float Node::geteval() -{ -} -void Node::setvalue(float a) -{ -} -void Node::reset() -{ - calculated=0; -} -float ZeroParentsNode::geteval() -{ - return 1; -} -int ZeroParentsNode::getParentsNum() -{ - return 0; -} -float OneParentsNode::geteval() -{ - if(calculated)return tempeval; - else - { - calculated=1; - tempeval=func(p1->geteval()); - return tempeval; - } -} -int OneParentsNode::getParentsNum() -{ - return 1; -} -float OneParentsNode::func(float x){} -OneParentsNode::OneParentsNode(std::string a,Node* parent1) -{ - name=a; - p1=parent1; -} -float TwoParentsNode::func(float x,float y) -{ -} -float TwoParentsNode::geteval() -{ - if(calculated)return tempeval; - else - { - calculated=1; - tempeval=func(p1->geteval(),p2->geteval()); - return tempeval; - } -} -int TwoParentsNode::getParentsNum() -{ - return 2; -} -TwoParentsNode::TwoParentsNode(std::string a,Node* parent1,Node* parent2) -{ - name=a; - p1=parent1; - p2=parent2; -} -float ThreeParentsNode::func(float x,float y,float z){} -float ThreeParentsNode::geteval() -{ - if(calculated)return tempeval; - else - { - calculated=1; - tempeval=func(p1->geteval(),p2->geteval(),p3->geteval()); - return tempeval; - } -} -int ThreeParentsNode::getParentsNum() -{ - return 3; -} -ThreeParentsNode::ThreeParentsNode(std::string a,Node* parent1,Node* parent2,Node* parent3) -{ - name=a; - p1=parent1; - p2=parent2; - p3=parent3; +#include "node.h" +float Node::geteval() +{ +} +void Node::setvalue(float a) +{ +} +void Node::reset() +{ + calculated=0; +} +float ZeroParentsNode::geteval() +{ + return 1; +} +int ZeroParentsNode::getParentsNum() +{ + return 0; +} +float OneParentsNode::geteval() +{ + if(calculated)return tempeval; + else + { + calculated=1; + tempeval=func(p1->geteval()); + return tempeval; + } +} +int OneParentsNode::getParentsNum() +{ + return 1; +} +float OneParentsNode::func(float x){} +OneParentsNode::OneParentsNode(std::string a,Node* parent1) +{ + name=a; + p1=parent1; +} +float TwoParentsNode::func(float x,float y) +{ +} +float TwoParentsNode::geteval() +{ + if(calculated)return tempeval; + else + { + calculated=1; + tempeval=func(p1->geteval(),p2->geteval()); + return tempeval; + } +} +int TwoParentsNode::getParentsNum() +{ + return 2; +} +TwoParentsNode::TwoParentsNode(std::string a,Node* parent1,Node* parent2) +{ + name=a; + p1=parent1; + p2=parent2; +} +float ThreeParentsNode::func(float x,float y,float z){} +float ThreeParentsNode::geteval() +{ + if(calculated)return tempeval; + else + { + calculated=1; + tempeval=func(p1->geteval(),p2->geteval(),p3->geteval()); + return tempeval; + } +} +int ThreeParentsNode::getParentsNum() +{ + return 3; +} +ThreeParentsNode::ThreeParentsNode(std::string a,Node* parent1,Node* parent2,Node* parent3) +{ + name=a; + p1=parent1; + p2=parent2; + p3=parent3; } \ No newline at end of file diff --git a/node.h b/node.h index 4222a24..efbf985 100644 --- a/node.h +++ b/node.h @@ -1,50 +1,102 @@ -#pragma once -#include -#include -#include -const float NaN=0.0/0.0; -class Node -{ - public: - std::string name; - virtual float geteval(); - virtual void setvalue(float a); - float tempeval; - bool calculated=0; - bool printable=0; - virtual void reset(); -}; -class ZeroParentsNode:public Node -{ - public: - float geteval(); - int getParentsNum(); -}; - -class OneParentsNode:public Node -{ - public: - Node *p1;//parents - OneParentsNode(std::string a,Node* parent1); - virtual float func(float x); - float geteval(); - int getParentsNum(); -}; -class TwoParentsNode:public Node -{ - public: - Node *p1,*p2;//parents - virtual float func(float x,float y); - float geteval(); - int getParentsNum(); - TwoParentsNode(std::string a,Node* parent1,Node* parent2); -}; -class ThreeParentsNode:public Node -{ - public: - Node *p1,*p2,*p3;//parents - virtual float func(float x,float y,float z); - float geteval(); - int getParentsNum(); - ThreeParentsNode(std::string a,Node* parent1,Node* parent2,Node* parent3); -}; \ No newline at end of file +#pragma once +#include +#include +#include +const float NaN=0.0/0.0; +class Node +{ + public: + std::string name; + virtual float geteval(); + virtual void setvalue(float a); + float tempeval; + bool calculated=0; + bool printable=0; + virtual void reset(); +}; +class ZeroParentsNode:public Node +{ + public: + float geteval(); + int getParentsNum(); +}; + +class OneParentsNode:public Node +{ + public: + Node *p1;//parents + OneParentsNode(std::string a,Node* parent1); + virtual float func(float x); + float geteval(); + int getParentsNum(); +}; +template +class Opn:public Node +{ + public: + Node *p1;//parents + Opn(std::string a,Node* parent1) + { + name=a; + p1=parent1; + } + float geteval() + { + if(calculated)return tempeval; + else + { + calculated=1; + tempeval=func(p1->geteval()); + return tempeval; + } + } + int getParentsNum() + { + return 1; + } +}; +template +class Tpn:public Node +{ + public: + Node *p1,*p2;//parents + Tpn(std::string a,Node* parent1,Node* parent2) + { + name=a; + p1=parent1; + p2=parent2; + } + float geteval() + { + if(calculated)return tempeval; + else + { + calculated=1; + tempeval=func(p1->geteval(),p2->geteval()); + return tempeval; + } + } + int getParentsNum() + { + return 2; + } + TwoParentsNode(std::string a,Node* parent1,Node* parent2); +}; +class TwoParentsNode:public Node +{ + public: + Node *p1,*p2;//parents + virtual float func(float x,float y); + float geteval(); + int getParentsNum(); + TwoParentsNode(std::string a,Node* parent1,Node* parent2); +}; +class ThreeParentsNode:public Node +{ + public: + Node *p1,*p2,*p3;//parents + virtual float func(float x,float y,float z); + float geteval(); + int getParentsNum(); + ThreeParentsNode(std::string a,Node* parent1,Node* parent2,Node* parent3); +}; diff --git a/onepn.cpp b/onepn.cpp index 74a0ba2..781a69d 100644 --- a/onepn.cpp +++ b/onepn.cpp @@ -1,28 +1,28 @@ -#include "onepn.h" -float Print::func(float x) -{ - std::cout<<"Print Operator: "<name<<"="<name<<"="< -#include -#include -#include "node.h" -class Print:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; -class Sin:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; -class Exp:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; -class Log:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; -class Tanh:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; -class Sigmoid:public OneParentsNode -{ - public: - using OneParentsNode::OneParentsNode; - float func(float x); -}; +#pragma once +#include +#include +#include +#include "node.h" +class Print:public OneParentsNode +{ + public: + using OneParentsNode::OneParentsNode; + float func(float x); +}; +float Sin(float x); +float Exp(float x); +float Log(float x); +float Tanh(float x); +float Sigmoid(float x); diff --git a/threepn.cpp b/threepn.cpp index d46c680..28394d5 100644 --- a/threepn.cpp +++ b/threepn.cpp @@ -1,9 +1,9 @@ -#include "threepn.h" -#include -float Cond::func(float x,float y,float z) -{ - if(x>0) - return y; - else - return z; +#include "threepn.h" +#include +float Cond::func(float x,float y,float z) +{ + if(x>0) + return y; + else + return z; } \ No newline at end of file diff --git a/threepn.h b/threepn.h index 89f6726..392944e 100644 --- a/threepn.h +++ b/threepn.h @@ -1,11 +1,11 @@ -#pragma once -#include -#include -#include -#include "node.h" -class Cond:public ThreeParentsNode -{ - public: - using ThreeParentsNode::ThreeParentsNode; - float func(float x,float y,float z); +#pragma once +#include +#include +#include +#include "node.h" +class Cond:public ThreeParentsNode +{ + public: + using ThreeParentsNode::ThreeParentsNode; + float func(float x,float y,float z); }; \ No newline at end of file diff --git a/twopn.cpp b/twopn.cpp index 5d61aeb..f92b391 100644 --- a/twopn.cpp +++ b/twopn.cpp @@ -1,43 +1,43 @@ -#include "twopn.h" -float sum::func(float x,float y) -{ - return x+y; -} -float subtraction::func(float x,float y) -{ - return x-y; -} -float multiply::func(float x,float y) -{ - return x*y; -} -float division::func(float x,float y) -{ - if(y==0) - { - std::cout<<"ERROR: Division by zero"; - return NaN; - } - else - return x/y; -} -float EQU::func(float x,float y) -{ - return float(x==y); -} -float GTR::func(float x,float y) -{ - return float(x>y); -} -float GEQ::func(float x,float y) -{ - return float(x>=y); -} -float LSS::func(float x,float y) -{ - return float(xy); +} +float GEQ(float x,float y) +{ + return float(x>=y); +} +float LSS(float x,float y) +{ + return float(x -#include -#include -#include "node.h" -class sum:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class subtraction:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class multiply:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class division:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class EQU:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class GTR:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class GEQ:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class LSS:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; -class LEQ:public TwoParentsNode -{ - public: - using TwoParentsNode::TwoParentsNode; - float func(float x,float y); -}; +#pragma once +#include +#include +#include +#include "node.h" +float sum(float x,float y); +float subtraction(float x,float y); +float multiply(float x,float y); +float division(float x,float y); +float EQU(float x,float y); +float GTR(float x,float y); +float GEQ(float x,float y); +float LSS(float x,float y); +float LEQ(float x,float y); diff --git a/zeropn.cpp b/zeropn.cpp index fd0aa70..eecdbce 100644 --- a/zeropn.cpp +++ b/zeropn.cpp @@ -1,46 +1,46 @@ -#include -#include -#include "zeropn.h" -void Placeholder::setvalue(float a) -{ - value=a; - calculated=1; -} -float Placeholder::geteval() -{ - if(calculated) - return value; - else - { - std::cout<<"ERROR: Placeholder missing"< +#include +#include "zeropn.h" +void Placeholder::setvalue(float a) +{ + value=a; + calculated=1; +} +float Placeholder::geteval() +{ + if(calculated) + return value; + else + { + std::cout<<"ERROR: Placeholder missing"< -#include -#include -#include "node.h" -using namespace std; -class Placeholder:public ZeroParentsNode -{ - public: - static float valuelist; - float value; - void setvalue(float a); - float geteval(); - Placeholder(string a); -}; -class Constant:public ZeroParentsNode -{ - float value; - public: - float geteval(); - virtual void reset(); - Constant(string a,float b); -}; -class Variable:public ZeroParentsNode -{ - public: - float value; - void setvalue(float a); - float geteval(); - virtual void reset(); - Variable(std::string a,float b); -}; +#pragma once +#include +#include +#include +#include "node.h" +using namespace std; +class Placeholder:public ZeroParentsNode +{ + public: + static float valuelist; + float value; + void setvalue(float a); + float geteval(); + Placeholder(string a); +}; +class Constant:public ZeroParentsNode +{ + float value; + public: + float geteval(); + virtual void reset(); + Constant(string a,float b); +}; +class Variable:public ZeroParentsNode +{ + public: + float value; + void setvalue(float a); + float geteval(); + virtual void reset(); + Variable(std::string a,float b); +};