Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using static System.Math;
- namespace lab2
- {
- class Program
- {
- static int fact(int i)
- {
- return i == 1 || i == 0 ? 1 : fact(i - 1) * i;
- }
- static double[] takeY(double x)
- {
- int n = 0;
- const double exp = 1e-6;
- double element, nextElement;
- while (true)
- {
- element = (Pow(-1, n) * Pow(x, 2 * n)) / fact(n);
- nextElement = (Pow(-1, n + 1) * Pow(x, 2 * (n + 1))) / fact(n + 1);
- n++;
- if (Abs(nextElement - element) < exp) break;
- }
- return new double[]{ Round(element, 8), n };
- }
- static bool isTarget(double x, double y, double R1, double R2)
- {
- return
- (
- ((x * x + y * y) < (R1 * R1) && x < 0 && y > 0)
- ||
- ((x * x + y * y) > (R1 * R1) && (x * x + y * y) < (R2 * R2) && x > 0 && y < 0)
- ?
- true
- :
- false
- );
- }
- static string center<T> (T value, int width)
- {
- string val = value.ToString();
- int l = (width - val.Length);
- return (new string(' ', (l + 1)/2)) + val + (new string(' ', l/2));
- }
- static double takeFunctionValue(double x, double R)
- {
- double y;
- if (x <= -3) y = (x + 3) / 2.0;
- else if (x <= 0) y = System.Math.Sqrt(System.Math.Pow(R, 2) - System.Math.Pow(x, 2));
- else if (x <= 6)
- y = x/6.0 * R;
- else y = (x - 6) / 2.0;
- return y;
- }
- static void Main(string[] args)
- {
- System.Console.Write("Task 3.1\nEnter xs, xe, dx and R: ");
- string[] splitedStr = System.Console.ReadLine().Split(" ");
- double xs = double.Parse(splitedStr[0]),
- xe = double.Parse(splitedStr[1]),
- dx = double.Parse(splitedStr[2]),
- R = double.Parse(splitedStr[3]);
- System.Console.WriteLine("|" + center<string>("F(x)", 10) + "|" + center<string>("x", 10) + "|");
- while (xs <= xe){
- double y = System.Math.Round(takeFunctionValue(xs, R), 2);
- System.Console.WriteLine("|" + center<double>(y, 10) + "|" + center<double>(xs, 10) + "|");
- xs += dx;
- }
- /*
- System.Console.Write("Task 3.2\n");
- System.Console.Write("Enter R1 and R2: ");
- splitedStr = System.Console.ReadLine().Split(" ");
- double R1 = double.Parse(splitedStr[0]),
- R2 = double.Parse(splitedStr[1]);
- for (uint i = 0; i < 5; i++){
- System.Console.Write("Enter x, y: ");
- splitedStr = System.Console.ReadLine().Split(" ");
- double x = double.Parse(splitedStr[0]),
- y = double.Parse(splitedStr[1]);
- System.Console.WriteLine(isTarget(x, y, R1, R2) ? "True" : "False");
- }
- */
- System.Console.Write("Task 3.2\n");
- System.Console.Write("Enter xs, xe and dx: ");
- splitedStr = System.Console.ReadLine().Split(" ");
- xs = double.Parse(splitedStr[0]);
- xe = double.Parse(splitedStr[1]);
- dx = double.Parse(splitedStr[2]);
- System.Console.WriteLine("|" + center<string>("F(x)", 20)
- + "|" + center<string>("x", 10)
- + "|" + center<string>("n", 10) + "|");
- while (xs <= xe) {
- double[] y = takeY(xs);
- System.Console.WriteLine("|" + center<double>(y[0], 20)
- + "|"
- + center<double>(xs, 10)
- + "|" + center<double>(y[1], 10) + "|");
- xs += dx;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement