1

```
using System;
using System.Collections.Generic;
// DM 14/nov/2014
namespace TestFuncDelegate
{
class Program
{
static public Dictionary<string, Func<double, double, double>> dict = new Dictionary<string, Func<double, double, double>>();
static void Main(string[] args)
{
InitDictionary();
Console.WriteLine(" ++++ Simple RPN calculator ++++");
Console.WriteLine();
Console.WriteLine(" Approximation of pi: 355.0/113.0 = {0}", do_op("/", 355, 113));
Console.WriteLine(" Square root of 25 = {0}", do_op("**", 25, 0.5));
Console.WriteLine(" The addition of {0} and {1} equals {2}", 2.3, 8.18, do_op("+", 2.3, 8.18));
Console.WriteLine(" The sine of an angle of 30Â° = {0}", do_op("sin", Math.PI/6, 0));
Console.WriteLine(" The arcsine of a sine: {0}" , do_op("asin", do_op("sin", 0.7, 0), 0));
Console.WriteLine(" The hypothenuse of a right triangle with sides 3 and 4: {0}",do_op("hypo", 3, 4));
Console.ReadKey();
}
static public void InitDictionary()
{
// Other way, more verbose but perhaps clearer?
// Func<double, double, double> AddFunc = (x, y) => x + y;
// dict.Add("+", AddFunc);
// Shorter, let compiler worry about Func delegates.
dict.Add("+", (x, y) => x + y);
dict.Add("-", (x, y) => x - y);
dict.Add("*", (x, y) => x * y);
dict.Add("/", (x, y) => x / y);
dict.Add("//", (x, y) => Math.Floor(x / y));
dict.Add("**", (x, y) => Math.Pow(x,y));
dict.Add("sin", (x, y) => Math.Sin(x)); // y dummy
dict.Add("cos", (x, y) => Math.Cos(x)); // y dummy
dict.Add("tan", (x, y) => Math.Tan(x)); // y dummy
dict.Add("asin", (x, y) => Math.Asin(x)); // y dummy
dict.Add("acos", (x, y) => Math.Acos(x)); // y dummy
dict.Add("atan", (x, y) => Math.Atan(x)); // y dummy
dict.Add("hypo", (x, y) => Math.Sqrt(x * x + y * y)); // y dummy
}
static public double do_op(string oper,double x, double y)
{
try
{
double result;
Func<double, double, double> theFunc;
dict.TryGetValue(oper, out theFunc);
result = theFunc(x, y);
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return double.NaN;
}
}
}
}
```

The article starter is a financial contributor. Sponsored articles offer a bounty for quality replies.