Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using static System.Math;
  2.  
  3. namespace lab2
  4. {
  5.  
  6.     class Program
  7.     {
  8.         static int fact(int i)
  9.         {
  10.             return i == 1 || i == 0 ? 1 : fact(i - 1) * i;
  11.         }
  12.         static double[] takeY(double x)
  13.         {
  14.             int n = 0;
  15.             const double exp = 1e-6;
  16.             double element, nextElement;
  17.             while (true)
  18.             {
  19.                 element = (Pow(-1, n) * Pow(x, 2 * n)) / fact(n);
  20.                 nextElement = (Pow(-1, n + 1) * Pow(x, 2 * (n + 1))) / fact(n + 1);
  21.                 n++;
  22.                 if (Abs(nextElement - element) < exp) break;
  23.             }
  24.             return new double[]{ Round(element, 8), n };
  25.         }
  26.         static bool isTarget(double x, double y, double R1, double R2)
  27.         {
  28.             return
  29.             (
  30.             ((x * x + y * y) < (R1 * R1) && x < 0 && y > 0)
  31.             ||
  32.             ((x * x + y * y) > (R1 * R1) && (x * x + y * y) < (R2 * R2) && x > 0 && y < 0)
  33.             ?
  34.             true
  35.             :
  36.             false
  37.             );
  38.         }
  39.         static string center<T> (T value, int width)
  40.         {
  41.             string val = value.ToString();
  42.             int l = (width - val.Length);
  43.             return (new string(' ', (l + 1)/2)) + val + (new string(' ', l/2));
  44.            
  45.         }
  46.         static double takeFunctionValue(double x, double R)
  47.         {
  48.             double y;
  49.  
  50.             if (x <= -3) y = (x + 3) / 2.0;
  51.             else if (x <= 0) y = System.Math.Sqrt(System.Math.Pow(R, 2) - System.Math.Pow(x, 2));
  52.             else if (x <= 6)
  53.                 y = x/6.0 * R;
  54.             else y = (x - 6) / 2.0;
  55.  
  56.             return y;
  57.         }
  58.         static void Main(string[] args)
  59.         {
  60.             System.Console.Write("Task 3.1\nEnter xs, xe, dx and R: ");
  61.             string[] splitedStr = System.Console.ReadLine().Split(" ");    
  62.             double xs = double.Parse(splitedStr[0]),
  63.                    xe = double.Parse(splitedStr[1]),
  64.                    dx = double.Parse(splitedStr[2]),
  65.                     R = double.Parse(splitedStr[3]);
  66.             System.Console.WriteLine("|" + center<string>("F(x)", 10) + "|" + center<string>("x", 10) + "|");
  67.             while (xs <= xe){
  68.                 double y = System.Math.Round(takeFunctionValue(xs, R), 2);
  69.                 System.Console.WriteLine("|" + center<double>(y, 10) + "|" + center<double>(xs, 10) + "|");
  70.                 xs += dx;    
  71.             }
  72.             /*
  73.             System.Console.Write("Task 3.2\n");
  74.             System.Console.Write("Enter R1 and R2: ");
  75.             splitedStr = System.Console.ReadLine().Split(" ");
  76.             double R1 = double.Parse(splitedStr[0]),
  77.                    R2 = double.Parse(splitedStr[1]);
  78.  
  79.             for (uint i = 0; i < 5; i++){
  80.                 System.Console.Write("Enter x, y: ");
  81.                 splitedStr = System.Console.ReadLine().Split(" ");
  82.                 double  x = double.Parse(splitedStr[0]),
  83.                         y = double.Parse(splitedStr[1]);
  84.                 System.Console.WriteLine(isTarget(x, y, R1, R2) ? "True" : "False");
  85.             }
  86.             */
  87.             System.Console.Write("Task 3.2\n");
  88.             System.Console.Write("Enter xs, xe and dx: ");
  89.             splitedStr = System.Console.ReadLine().Split(" ");
  90.             xs = double.Parse(splitedStr[0]);
  91.             xe = double.Parse(splitedStr[1]);
  92.             dx = double.Parse(splitedStr[2]);
  93.  
  94.             System.Console.WriteLine("|" + center<string>("F(x)", 20)
  95.                                          + "|" + center<string>("x", 10)
  96.                                          + "|" + center<string>("n", 10) + "|");
  97.            
  98.             while (xs <= xe) {
  99.                 double[] y = takeY(xs);
  100.                 System.Console.WriteLine("|" + center<double>(y[0], 20)
  101.                                              + "|"
  102.                                              + center<double>(xs, 10)
  103.                                              + "|" + center<double>(y[1], 10) + "|");
  104.                 xs += dx;
  105.  
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement