Compare commits

...
This repository has been archived on 2021-11-27. You can view files and clone it, but cannot push or open issues or pull requests.

1 Commits

Author SHA1 Message Date
Alexander Zhirov 56483989cb example 9 2021-11-21 15:11:59 +03:00
4 changed files with 127 additions and 0 deletions

15
src/example_9/.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/example_9
example_9.so
example_9.dylib
example_9.dll
example_9.a
example_9.lib
example_9-test-*
*.exe
*.o
*.obj
*.lst

9
src/example_9/dub.json Normal file
View File

@ -0,0 +1,9 @@
{
"authors": [
"Alexander"
],
"description": "Больше статистики. Наследование.",
"license": "proprietary",
"name": "example_9",
"targetPath": "bin"
}

View File

@ -0,0 +1,26 @@
import std.stdio : writeln, stdin;
import std.exception : enforce;
import stat_;
void main(string[] args)
{
Stat[] stats;
foreach (arg; args[1 .. $])
{
auto newStat = cast(Stat) Object.factory("stat_." ~ 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());
}
}

View File

@ -0,0 +1,77 @@
module stat_;
interface Stat
{
void accumulate(double x);
void postprocess();
double result();
}
class IncrementalStat : Stat
{
protected double _result;
abstract void accumulate(double x);
void postprocess()
{
}
double result()
{
return _result;
}
}
class Min : IncrementalStat
{
this()
{
_result = double.max;
}
override void accumulate(double x)
{
if (x < _result)
{
_result = x;
}
}
}
class Max : IncrementalStat
{
this()
{
_result = double.min_normal;
}
override void accumulate(double x)
{
if (x > _result)
{
_result = x;
}
}
}
class Average : IncrementalStat
{
private uint items = 0;
this()
{
_result = 0;
}
override void accumulate(double x)
{
_result += x;
++items;
}
override void postprocess()
{
if (items)
{
_result /= items;
}
}
}