いろいろなテクニック.Nucleo と DISCO-F746 用.

Dependencies:   Array_Matrix mbed

Revision:
0:bb939e0bc6e2
Child:
1:bbb9f0c3e523
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FunctionObject.hpp	Sun Oct 15 11:41:48 2017 +0000
@@ -0,0 +1,43 @@
+
+class Func
+{
+public:
+    virtual ~Func() {}
+    virtual float operator()(float) = 0;
+};
+
+float Integrate(Func &f, float a, float b)
+{
+    const int N = 1000;
+    float step = (b - a)/N;
+    float s = 0.0f;
+    while(a < b)
+    {
+        s += f(a)*step;
+        a += step;
+    }
+    return s;
+}
+
+float aFunc1(float x) { return x; }
+float aFunc2(float x) { return x*x; }
+
+class DerivFunc: public Func
+{
+public:
+    DerivFunc(float (*f)(float)) : f_(f) {}
+    virtual float operator()(float d) { return f_(d); }
+private:
+    float (*f_)(float);
+};
+
+void MyFunctionObject()
+{
+    DerivFunc fx1(aFunc1);
+    float area = Integrate(fx1, 0.0, 1.0);
+    printf("\r\nArea = %f\r\n", area);
+
+    DerivFunc fx2(aFunc2);
+    area = Integrate(fx2, 0.0, 1.0);
+    printf("Area = %f\r\n", area);
+}