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
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#
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
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)));
575 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
586 PHP ssh2_scp_send fails to send files as intended
930 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
578 Who developed the "avformat-59.dll" that comes with FFmpeg?
624 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.