Can't we use Delegate in c# to extract this function a little bit more neatly?

Asked 2 years ago, Updated 2 years ago, 40 views

public static void TrigFunctions_Example(string pathNfilename)

    {
        FileStream fsWrite = new FileStream(pathNfilename, FileMode.Create, FileAccess.Write);
        StreamWriter sWriter = new StreamWriter(fsWrite);
        for (double angleDEG = 0.0; angleDEG <= 360.0; angleDEG += 45.0)
        {
            double angleRAD = ConvertDegreesToRadians(angleDEG);
            Console.WriteLine("Angle = {0}\xb0", angleDEG);
            sWriter.WriteLine("Angle = {0}\xb0", angleDEG);
            Console.WriteLine("Cos({0}\xb0)={1}", angleDEG, Math.Cos(angleRAD));
            sWriter.WriteLine("Cos({0}\xb0)={1}", angleDEG, Math.Cos(angleRAD));
            Console.WriteLine("Sin({0}\xb0)={1}", angleDEG, Math.Sin(angleRAD));
            sWriter.WriteLine("Sin({0}\xb0)={1}", angleDEG, Math.Sin(angleRAD));
            Console.WriteLine("Tan({0}\xb0)={1}", angleDEG, Math.Tan(angleRAD));
            sWriter.WriteLine("Tan({0}\xb0)={1}", angleDEG, Math.Tan(angleRAD));
            Console.WriteLine("Sec({0}\xb0)={1}", angleDEG, Sec(angleRAD));
            sWriter.WriteLine("Sec({0}\xb0)={1}", angleDEG, Sec(angleRAD));
            Console.WriteLine("Csc({0}\xb0)={1}", angleDEG, Csc(angleRAD));
            sWriter.WriteLine("Csc({0}\xb0)={1}", angleDEG, Csc(angleRAD));
            Console.WriteLine("Cot({0}\xb0)={1}", angleDEG, Cot(angleRAD));
            sWriter.WriteLine("Cot({0}\xb0)={1}", angleDEG, Cot(angleRAD));
        }
        sWriter.Close();
    }

As you can see, it's a very dirty-looking guy.

I'm trying to simplify them as much as I can, and friends like Cot and Sec can make string arrangements and bind them together, and Math.Cot (angleRAD) is Math.String array I can't touch it in the same way as element number . Cot, Sec, Csc, which I have defined separately, of course not.

    public static string[] trigarr = {"Cos","Sin","Tan","Sec","Csc","Cot"};
    public static string[] hyperarr = { "Cosh", "Sinh", "Tanh", "Sech", "Csch", "Coth" };
    public static void CalcNWrite(string pathNfilename, string s)
    {
        FileStream fsWrite = new FileStream(pathNfilename, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sWriter = new StreamWriter(fsWrite);
        for (double angleDEG = 0.0; angleDEG <= 360.0; angleDEG += 45.0)
        {
            double angleRAD = ConvertDegreesToRadians(angleDEG);
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine(trigarr[i] + "({0}\xb0)={1}", angleDEG, trigarr[i](angleRAD))); //This part does not work
                sWriter.WriteLine(trigarr[i] + "({0}\xb0)={1}", angleDEG, trigarr[i](angleRAD))); //Like..
            }
        }
        sWriter.Close();
    }

How can we cleanly handle the actual output function?

The hint is, use the deligate. That's all you're saying <

c#

2022-09-21 23:00

2 Answers

I think you can do it with this example.

private delegate double Cal(double arg);

private static double ConvertDegreesToRadians(double angle)
{
    return Math.PI * angle / 180.0;
}

void Main()
{
    Func<string, double, Cal, string> f = (string name, double x, Cal cal) => string.Format("{0}({1}\xb0)={2}", name, x, cal(ConvertDegreesToRadians(x)));
    string line = f("Sin", 30, Math.Sin);
    Console.WriteLine(line);
}

Sin(30°)=0.5


2022-09-21 23:00

You can delete private register double Cal(double arg); and do it as follows.

Func<string, double, Func<double, double>, string> f = (string name, double x, Func<double, double> cal) => string.Format("{0}({1}\xb0)={2}", name, x, cal(ConvertDegreesToRadians(x)));


2022-09-21 23:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.