diff options
author | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-19 16:21:26 +1000 |
---|---|---|
committer | Carlo Zancanaro <carlo@carlo-laptop> | 2012-04-19 16:21:26 +1000 |
commit | 2ee29b3426d2d79872fba35adbeb8d768983ffec (patch) | |
tree | c1c516816b7fa8d59bea445abd4505860be376bc /impl/main.cpp | |
parent | 5d7252681da3b26845fc4e5dcf0b0e94ed9fabb1 (diff) |
Add presentation; start a different implementation
Diffstat (limited to 'impl/main.cpp')
-rw-r--r-- | impl/main.cpp | 111 |
1 files changed, 85 insertions, 26 deletions
diff --git a/impl/main.cpp b/impl/main.cpp index 814a3ee..dc6cf45 100644 --- a/impl/main.cpp +++ b/impl/main.cpp @@ -1,36 +1,95 @@ -#include "Constant.h" -#include "Variable.h" -#include "Min.h" -#include "Max.h" -#include "Sum.h" -#include "Equation.h" -#include "EquationSystem.h" - #include <iostream> +#include <vector> +#include <math.h> +#include "Expression.h" +#include "Operator.h" using namespace std; -int main() { - std::map<std::string, int> mappings; - mappings["x"] = 10; +struct Variable : public Operator<float> { + Variable(const std::string& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 0); + map<string, float>::const_iterator it = m.find(_value); + return (it == m.end() ? -INFINITY : it->second); + } + private: + std::string _value; +}; + +struct Const : public Operator<float> { + Const(const float& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 0); + return _value; + } + private: + float _value; +}; + +struct Mult : public Operator<float> { + Mult(const float& x) + : _value(x) { } + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + return _value * v[0].minvalue(m); + } + private: + float _value; +}; - Equation<int> e(Variable<int>("x"), - new Max<int>( - new Min<int>( - new Constant<int>(100), - new Sum<int>( - new Variable<int>("x"), - new Constant<int>(100))), - new Constant<int>(20))); +struct Maximum : public Operator<float> { + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + float value = -INFINITY; + for (vector<Expression<float> >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? result : value); + } + return value; + } +}; - Equation<int> e2(Variable<int>("x"), - new Constant<int>(200)); +struct Minimum : public Operator<float> { + const float eval(const vector<Expression<float> >& v, const map<string, float>& m) const { + //assert(v.size() == 1); + float value = INFINITY; + for (vector<Expression<float> >::const_iterator it = v.begin(); + it != v.end(); + ++it) { + const float result = it->maxvalue(m); + value = (value < result ? value : result); + } + return value; + } +}; - EquationSystem<int> es; - es.add(e); - es.add(e2); - std::map<std::string, int> result = es.solve(mappings); - cout << result["x"] << endl; +int main () { + typedef Expression<float> E; + typedef vector<E> Args; + map<string, float> m; + //m["x"] = -10; + Args empty; + { + Args a; + { + Args b; + b.push_back(E(new Const(10), empty)); + b.push_back(E(new Const(20), empty)); + b.push_back(E(new Variable("x"), empty)); + a.push_back(E(new Minimum(), b)); + cout << E(new Minimum(), b).minvalue(m) << endl; + } + } + /*map<string, float> m; + vector<Expression<float> > empty; + vector<Expression<float> > args; + args.push_back(Expression<float>(new Const(10), empty)); + cout << Expression<float>(new Mult(3), args).minvalue(m) << endl;*/ return 0; } |