using System;
using System.Collections;
using System.Collections.Generic;
// ElementaryExtensions API:
using ElementaryExtensions;
namespace MiscellaneousDemo {
//**************************************************************************************************
static partial class Program {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
///
/// Usage of class.
///
static bool ArithAlgDemo()
{
int nDialogItemIdx=
ЄConsole.NumberedListDialog
(
"Arithmetic algorithm over collection (array):",
new string[]
{
"Average value of double precision numbers (built-in type)",
"Average value of complex numbers (single or double precision)",
},
"Cancel"
);
if (nDialogItemIdx<0)
return false;
ЄConsole.Print();
switch (nDialogItemIdx)
{
case 0:
double[] adNumbers=new double[]
{
15.34, -15e-1, 19.67e2, 47.1354, 3157.0009
};
PrintNumbers(adNumbers);
PrintCalcResult(Avg(adNumbers));
break;
case 1:
// Explicit conversion to Real type is used, because Complex type may be based
// either on Single (float) or on Double (double).
Complex[] acNumbers=new Complex[]
{
new Complex( (Real) 4.37 , (Real) 37.2108 ),
new Complex( (Real)(-5.08), (Real) 21.07 ),
new Complex( (Real) 8.623, (Real)(-0.3457) )
};
PrintNumbers(acNumbers);
PrintCalcResult(Avg(acNumbers));
break;
default:
throw new ЄDeadCodeBranchException();
}
return true;
}
///
/// Average value calculation -- algorithm over collection of values of indeterminate type.
///
///
/// This method is identical to static method (one of its overloads).
///
public static T Avg(IEnumerable collection) where T: struct
{
T sum=default(T);
int nCount=0;
foreach (T elem in collection)
{
sum=ЄArith.Oper_Addition(sum,elem);
nCount++;
}
if (nCount==0)
throw new InvalidOperationException(
"Attempt to calculate average of empty set of values." );
return ЄArith.Oper_Division( sum,
ЄArith.GenerateValueFrom(nCount) );
}
///
/// Helper function for arithmetic algorithm demo.
///
static void PrintNumbers(IEnumerable numCollection)
{
ЄConsole.TypeInformation("Numbers");
ЄConsole.Type(": ");
bool bNextElement=false;
foreach (object obNumber in numCollection)
{
if (bNextElement)
ЄConsole.Type(", ");
ЄConsole.TypeIntensive(obNumber);
bNextElement=true;
}
ЄConsole.Print();
}
///
/// Helper function for arithmetic algorithm demo.
///
static void PrintCalcResult(object obSum)
{
ЄConsole.TypeInformation("Average");
ЄConsole.Type(": ");
ЄConsole.PrintExclamation(obSum);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Program
//**************************************************************************************************
} // MiscellaneousDemo