diff --git a/src/example_8/.gitignore b/src/example_8/.gitignore new file mode 100644 index 0000000..a10294f --- /dev/null +++ b/src/example_8/.gitignore @@ -0,0 +1,15 @@ +.dub +docs.json +__dummy.html +docs/ +/example_8 +example_8.so +example_8.dylib +example_8.dll +example_8.a +example_8.lib +example_8-test-* +*.exe +*.o +*.obj +*.lst diff --git a/src/example_8/dub.json b/src/example_8/dub.json new file mode 100644 index 0000000..754845a --- /dev/null +++ b/src/example_8/dub.json @@ -0,0 +1,9 @@ +{ + "authors": [ + "Alexander" + ], + "description": "Интерфейсы и классы", + "license": "proprietary", + "name": "example_8", + "targetPath": "bin" +} diff --git a/src/example_8/source/app.d b/src/example_8/source/app.d new file mode 100644 index 0000000..f268a94 --- /dev/null +++ b/src/example_8/source/app.d @@ -0,0 +1,26 @@ +import std.stdio : writeln, stdin; +import std.exception : enforce; +import min_, stat_; + +void main(string[] args) +{ + Stat[] stats; + foreach (arg; args[1 .. $]) + { + auto newStat = cast(Stat) Object.factory("min_." ~ arg); + enforce(newStat, "Invalid statistics function: " ~ arg); + stats ~= newStat; + } + for (double x; stdin.readf(" %s ", &x) == 1;) + { + foreach (s; stats) + { + s.accumulate(x); + } + } + foreach (s; stats) + { + s.postprocess(); + writeln(s.result()); + } +} diff --git a/src/example_8/source/min_.d b/src/example_8/source/min_.d new file mode 100644 index 0000000..fdb1f9b --- /dev/null +++ b/src/example_8/source/min_.d @@ -0,0 +1,23 @@ +module min_; + +import stat_; + +class Min : Stat +{ + private double min = double.max; + + void accumulate(double x) + { + if (x < min) + { + min = x; + } + } + + void postprocess() {} + + double result() + { + return min; + } +} \ No newline at end of file diff --git a/src/example_8/source/stat_.d b/src/example_8/source/stat_.d new file mode 100644 index 0000000..9fc5668 --- /dev/null +++ b/src/example_8/source/stat_.d @@ -0,0 +1,8 @@ +module stat_; + +interface Stat +{ + void accumulate(double x); + void postprocess(); + double result(); +}