From 71dda63867c018da58df2f0bb26f728db859f17e Mon Sep 17 00:00:00 2001 From: HangZhiYang <49983713+HangZhiYang@users.noreply.github.com> Date: Sun, 28 Apr 2019 10:41:29 +0800 Subject: [PATCH 1/3] template nodes --- node.cpp | 168 ++++++++++++++++++++++++++-------------------------- node.h | 151 ++++++++++++++++++++++++++++++---------------- onepn.cpp | 56 +++++++++--------- onepn.h | 57 +++++------------- threepn.cpp | 16 ++--- threepn.h | 20 +++---- twopn.cpp | 86 +++++++++++++-------------- twopn.h | 73 +++++------------------ zeropn.cpp | 90 ++++++++++++++-------------- zeropn.h | 64 ++++++++++---------- 10 files changed, 381 insertions(+), 400 deletions(-) diff --git a/node.cpp b/node.cpp index ea7a80f..3f91b5f 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; -} \ No newline at end of file +#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; +} diff --git a/node.h b/node.h index 4222a24..bc5f627 100644 --- a/node.h +++ b/node.h @@ -1,50 +1,101 @@ -#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; + } +}; +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):Node(name),p1,p2{} +}; +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); +}; From 76a0050be65bb96ee2b19f78e8fa2571920e4758 Mon Sep 17 00:00:00 2001 From: HangZhiYang <49983713+HangZhiYang@users.noreply.github.com> Date: Sun, 28 Apr 2019 10:57:57 +0800 Subject: [PATCH 2/3] templates functions(fix bug) --- node.cpp | 14 +++++++------- node.h | 25 +++++++++++++------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/node.cpp b/node.cpp index 3f91b5f..a6472d3 100644 --- a/node.cpp +++ b/node.cpp @@ -23,7 +23,7 @@ float OneParentsNode::geteval() else { calculated=1; - tempeval=func(p1.geteval()); + tempeval=func(p1->geteval()); return tempeval; } } @@ -32,7 +32,7 @@ int OneParentsNode::getParentsNum() return 1; } float OneParentsNode::func(float x){} -OneParentsNode::OneParentsNode(std::string a,Node& parent1) +OneParentsNode::OneParentsNode(std::string a,Node* parent1) { name=a; p1=parent1; @@ -46,7 +46,7 @@ float TwoParentsNode::geteval() else { calculated=1; - tempeval=func(p1.geteval(),p2.geteval()); + tempeval=func(p1->geteval(),p2->geteval()); return tempeval; } } @@ -54,7 +54,7 @@ int TwoParentsNode::getParentsNum() { return 2; } -TwoParentsNode::TwoParentsNode(std::string a,Node& parent1,Node& parent2) +TwoParentsNode::TwoParentsNode(std::string a,Node* parent1,Node* parent2) { name=a; p1=parent1; @@ -67,7 +67,7 @@ float ThreeParentsNode::geteval() else { calculated=1; - tempeval=func(p1.geteval(),p2.geteval(),p3.geteval()); + tempeval=func(p1->geteval(),p2->geteval(),p3->geteval()); return tempeval; } } @@ -75,10 +75,10 @@ int ThreeParentsNode::getParentsNum() { return 3; } -ThreeParentsNode::ThreeParentsNode(std::string a,Node& parent1,Node& parent2,Node& parent3) +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 bc5f627..efbf985 100644 --- a/node.h +++ b/node.h @@ -24,8 +24,8 @@ class ZeroParentsNode:public Node class OneParentsNode:public Node { public: - Node& p1;//parents - OneParentsNode(std::string a,Node& parent1); + Node *p1;//parents + OneParentsNode(std::string a,Node* parent1); virtual float func(float x); float geteval(); int getParentsNum(); @@ -34,8 +34,8 @@ template class Opn:public Node { public: - Node& p1;//parents - Opn(std::string a,Node& parent1) + Node *p1;//parents + Opn(std::string a,Node* parent1) { name=a; p1=parent1; @@ -46,7 +46,7 @@ class Opn:public Node else { calculated=1; - tempeval=func(p1.geteval()); + tempeval=func(p1->geteval()); return tempeval; } } @@ -59,8 +59,8 @@ template class Tpn:public Node { public: - Node& p1,p2;//parents - Tpn(std::string a,Node& parent1,Node& parent2) + Node *p1,*p2;//parents + Tpn(std::string a,Node* parent1,Node* parent2) { name=a; p1=parent1; @@ -72,7 +72,7 @@ class Tpn:public Node else { calculated=1; - tempeval=func(p1.geteval(),p2.geteval()); + tempeval=func(p1->geteval(),p2->geteval()); return tempeval; } } @@ -80,22 +80,23 @@ class Tpn:public Node { return 2; } + TwoParentsNode(std::string a,Node* parent1,Node* parent2); }; class TwoParentsNode:public Node { public: - Node& p1,p2;//parents + Node *p1,*p2;//parents virtual float func(float x,float y); float geteval(); int getParentsNum(); - TwoParentsNode(std::string a,Node& parent1,Node& parent2):Node(name),p1,p2{} + TwoParentsNode(std::string a,Node* parent1,Node* parent2); }; class ThreeParentsNode:public Node { public: - Node& p1,p2,p3;//parents + 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); + ThreeParentsNode(std::string a,Node* parent1,Node* parent2,Node* parent3); }; From 0644f138948a5804cb9f8e224fbff89ace4b69ec Mon Sep 17 00:00:00 2001 From: HangZhiYang <49983713+HangZhiYang@users.noreply.github.com> Date: Sun, 28 Apr 2019 11:02:35 +0800 Subject: [PATCH 3/3] Template functions --- main.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) 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); }