Compare commits

..

No commits in common. "example_9" and "master" have entirely different histories.

4 changed files with 0 additions and 127 deletions

View File

@ -1,15 +0,0 @@
.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

View File

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

View File

@ -1,26 +0,0 @@
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

@ -1,77 +0,0 @@
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;
}
}
}