tranerius

Тригонометрические функции

Feb 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.31 KB | None | 0 0
  1. class Trigonometric
  2. {
  3.     public const double PI = 3.141592653589793;
  4.     static void Main()
  5.     {
  6.         string symbol;
  7.         string operation = "no_data";
  8.         while (true)
  9.         {
  10.             while (operation != "sin" && operation != "cos" && operation != "tg" && operation != "ctg")
  11.             {
  12.                 System.Console.Write("Введите название тригонометрической функции, значение которой необходимо посчитать (cos, sin, tg, ctg): ");
  13.                 operation = System.Console.ReadLine();
  14.                 if (operation != "sin" && operation != "cos" && operation != "tg" && operation != "ctg")
  15.                 {
  16.                     System.Console.Clear();
  17.                     System.Console.WriteLine("Ошибка. Введен неверный ответ. Попробуйте снова.");
  18.                     System.Threading.Thread.Sleep(1500);
  19.                 }
  20.                 System.Console.Clear();
  21.             }
  22.             double rad = To_rad();
  23.             System.Console.WriteLine("{0}({1:0.###}) = {2:0.###}", operation, rad, Taylor_series(operation, rad));
  24.             System.Console.Write("Чтобы закончить работу введите 0. Если хотите продолжить, введите любой символ: ");
  25.             symbol = System.Console.ReadLine();
  26.             if (symbol[0] == '0')
  27.             {
  28.                 break;
  29.             }
  30.             operation = "no_data";
  31.             System.Console.Clear();
  32.         }
  33.     }
  34.     public static double Value_check()
  35.     {
  36.         bool was_point = false;
  37.         string check = System.Console.ReadLine();
  38.         for (int i = 0; i < check.Length; i++)
  39.         {
  40.             if (check[i] == '.' || check[i] == ',' && !was_point)
  41.             {
  42.                 check = check.Remove(i, 1);
  43.                 check = check.Insert(i, ",");
  44.                 was_point = true;
  45.             }
  46.             else if (check[i] == '.' || check[i] == ',' && was_point)
  47.             {
  48.                 check = check.Remove(i, 1);
  49.                 i--;
  50.             }
  51.             if ((check[i] < 48 && check[i] != 43 && check[i] != 44 && check[i] != 45 && check[i] != 0) || check[i] > 57)
  52.             {
  53.                 check = check.Remove(i, 1);
  54.                 i--;
  55.             }
  56.         }
  57.         if (check.Length == 0)
  58.         {
  59.             return 0;
  60.         }
  61.         return System.Convert.ToDouble(check);
  62.     }
  63.     public static double To_rad()
  64.     {
  65.         int angle = 0;
  66.         double rad = 0;
  67.         string name_operation = "no_data";
  68.         while (name_operation != "angle" && name_operation != "rad")
  69.         {
  70.             System.Console.Write("Введите название величины, которая будет передана параметром в функцию (angle, rad): ");
  71.             name_operation = System.Console.ReadLine();
  72.             if (name_operation != "angle" && name_operation != "rad")
  73.             {
  74.                 System.Console.Clear();
  75.                 System.Console.WriteLine("Ошибка. Введен неверный ответ. Попробуйте снова.");
  76.                 System.Threading.Thread.Sleep(1500);
  77.             }
  78.             System.Console.Clear();
  79.         }
  80.         if (name_operation == "angle")
  81.         {
  82.             System.Console.Write("Введите величину угла: ");
  83.             angle = System.Convert.ToInt32(Value_check());
  84.             rad = (double)angle / (double)(180 / PI);
  85.         }
  86.         else
  87.         {
  88.             System.Console.Write("Введите радианы: ");
  89.             rad = Value_check();
  90.         }
  91.         while (rad > 2*PI || rad < 0)
  92.         {
  93.             if (rad > 2 * PI)
  94.             {
  95.                 rad -= (2*PI);
  96.             }
  97.             else
  98.             {
  99.                 rad += (2*PI);
  100.             }
  101.         }
  102.         System.Console.Clear();
  103.         return rad;
  104.     }
  105.     public static long Factorial(int value)
  106.     {
  107.         long temp_value = value;
  108.         if (value == 0)
  109.         {
  110.             return 1;
  111.         }
  112.         else if (value > 1)
  113.         {
  114.             for (int i = 1; i < value; i++)
  115.             {
  116.                 temp_value *= (value - i);
  117.             }
  118.         }
  119.         return temp_value;
  120.     }
  121.     public static double Exponentiation(double value, int exp)
  122.     {
  123.         double temp_value = value;
  124.         if (exp == 0)
  125.         {
  126.             return 1;
  127.         }
  128.         else if (exp > 1)
  129.         {
  130.             for (int i = 1; i < exp; i++)
  131.             {
  132.                 temp_value *= value;
  133.             }
  134.         }
  135.         return temp_value;
  136.     }
  137.     public static double Taylor_series(string tr_fun, double rad)
  138.     {
  139.         double value_cos = 0, value_sin = 0, value_result = 0;
  140.         bool was_minus = true;
  141.         if (tr_fun == "cos" || tr_fun == "tg" || tr_fun == "ctg")
  142.         {
  143.             for (int i = 0; i < 25; i++)
  144.             {
  145.                 if (i % 2 == 0 && was_minus)
  146.                 {
  147.                     value_cos += (Exponentiation(rad, i) / Factorial(i));
  148.                     was_minus = false;
  149.                 }
  150.                 else if (i % 2 == 0 && !was_minus)
  151.                 {
  152.                     value_cos -= (Exponentiation(rad, i) / Factorial(i));
  153.                     was_minus = true;
  154.                 }
  155.             }
  156.             was_minus = true;
  157.             value_result = value_cos;
  158.         }
  159.         if (tr_fun == "sin" || tr_fun == "tg" || tr_fun == "ctg")
  160.         {
  161.             for (int i = 0; i < 25; i++)
  162.             {
  163.                 if (i % 2 != 0 && was_minus)
  164.                 {
  165.                     value_sin += Exponentiation(rad, i) / Factorial(i);
  166.                     was_minus = false;
  167.                 }
  168.                 else if (i % 2 != 0 && !was_minus)
  169.                 {
  170.                     value_sin -= Exponentiation(rad, i) / Factorial(i);
  171.                     was_minus = true;
  172.                 }
  173.             }
  174.             value_result = value_sin;
  175.         }
  176.         if (tr_fun == "tg")
  177.         {
  178.             value_result = value_sin / value_cos;
  179.         }
  180.         else if (tr_fun == "ctg")
  181.         {
  182.             value_result = value_cos / value_sin;
  183.         }
  184.         return value_result;
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment