Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Trigonometric
- {
- public const double PI = 3.141592653589793;
- static void Main()
- {
- string symbol;
- string operation = "no_data";
- while (true)
- {
- while (operation != "sin" && operation != "cos" && operation != "tg" && operation != "ctg")
- {
- System.Console.Write("Введите название тригонометрической функции, значение которой необходимо посчитать (cos, sin, tg, ctg): ");
- operation = System.Console.ReadLine();
- if (operation != "sin" && operation != "cos" && operation != "tg" && operation != "ctg")
- {
- System.Console.Clear();
- System.Console.WriteLine("Ошибка. Введен неверный ответ. Попробуйте снова.");
- System.Threading.Thread.Sleep(1500);
- }
- System.Console.Clear();
- }
- double rad = To_rad();
- System.Console.WriteLine("{0}({1:0.###}) = {2:0.###}", operation, rad, Taylor_series(operation, rad));
- System.Console.Write("Чтобы закончить работу введите 0. Если хотите продолжить, введите любой символ: ");
- symbol = System.Console.ReadLine();
- if (symbol[0] == '0')
- {
- break;
- }
- operation = "no_data";
- System.Console.Clear();
- }
- }
- public static double Value_check()
- {
- bool was_point = false;
- string check = System.Console.ReadLine();
- for (int i = 0; i < check.Length; i++)
- {
- if (check[i] == '.' || check[i] == ',' && !was_point)
- {
- check = check.Remove(i, 1);
- check = check.Insert(i, ",");
- was_point = true;
- }
- else if (check[i] == '.' || check[i] == ',' && was_point)
- {
- check = check.Remove(i, 1);
- i--;
- }
- if ((check[i] < 48 && check[i] != 43 && check[i] != 44 && check[i] != 45 && check[i] != 0) || check[i] > 57)
- {
- check = check.Remove(i, 1);
- i--;
- }
- }
- if (check.Length == 0)
- {
- return 0;
- }
- return System.Convert.ToDouble(check);
- }
- public static double To_rad()
- {
- int angle = 0;
- double rad = 0;
- string name_operation = "no_data";
- while (name_operation != "angle" && name_operation != "rad")
- {
- System.Console.Write("Введите название величины, которая будет передана параметром в функцию (angle, rad): ");
- name_operation = System.Console.ReadLine();
- if (name_operation != "angle" && name_operation != "rad")
- {
- System.Console.Clear();
- System.Console.WriteLine("Ошибка. Введен неверный ответ. Попробуйте снова.");
- System.Threading.Thread.Sleep(1500);
- }
- System.Console.Clear();
- }
- if (name_operation == "angle")
- {
- System.Console.Write("Введите величину угла: ");
- angle = System.Convert.ToInt32(Value_check());
- rad = (double)angle / (double)(180 / PI);
- }
- else
- {
- System.Console.Write("Введите радианы: ");
- rad = Value_check();
- }
- while (rad > 2*PI || rad < 0)
- {
- if (rad > 2 * PI)
- {
- rad -= (2*PI);
- }
- else
- {
- rad += (2*PI);
- }
- }
- System.Console.Clear();
- return rad;
- }
- public static long Factorial(int value)
- {
- long temp_value = value;
- if (value == 0)
- {
- return 1;
- }
- else if (value > 1)
- {
- for (int i = 1; i < value; i++)
- {
- temp_value *= (value - i);
- }
- }
- return temp_value;
- }
- public static double Exponentiation(double value, int exp)
- {
- double temp_value = value;
- if (exp == 0)
- {
- return 1;
- }
- else if (exp > 1)
- {
- for (int i = 1; i < exp; i++)
- {
- temp_value *= value;
- }
- }
- return temp_value;
- }
- public static double Taylor_series(string tr_fun, double rad)
- {
- double value_cos = 0, value_sin = 0, value_result = 0;
- bool was_minus = true;
- if (tr_fun == "cos" || tr_fun == "tg" || tr_fun == "ctg")
- {
- for (int i = 0; i < 25; i++)
- {
- if (i % 2 == 0 && was_minus)
- {
- value_cos += (Exponentiation(rad, i) / Factorial(i));
- was_minus = false;
- }
- else if (i % 2 == 0 && !was_minus)
- {
- value_cos -= (Exponentiation(rad, i) / Factorial(i));
- was_minus = true;
- }
- }
- was_minus = true;
- value_result = value_cos;
- }
- if (tr_fun == "sin" || tr_fun == "tg" || tr_fun == "ctg")
- {
- for (int i = 0; i < 25; i++)
- {
- if (i % 2 != 0 && was_minus)
- {
- value_sin += Exponentiation(rad, i) / Factorial(i);
- was_minus = false;
- }
- else if (i % 2 != 0 && !was_minus)
- {
- value_sin -= Exponentiation(rad, i) / Factorial(i);
- was_minus = true;
- }
- }
- value_result = value_sin;
- }
- if (tr_fun == "tg")
- {
- value_result = value_sin / value_cos;
- }
- else if (tr_fun == "ctg")
- {
- value_result = value_cos / value_sin;
- }
- return value_result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment