Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp1
  4. {
  5. delegate double MyFunc(double x);
  6.  
  7. delegate double MyMethod(double f);
  8.  
  9. class Test
  10. {
  11. public static double f1(double x)
  12. {
  13. return x;
  14. }
  15.  
  16. public static double f2(double x)
  17. {
  18. return x * x;
  19. }
  20.  
  21. public static double f3(double x)
  22. {
  23. return (double) Math.Sqrt(x);
  24. }
  25. static double divCut(MyFunc f, int t)
  26. {
  27. //метод деления отрезка попалам
  28. double a, b, e;
  29. a = double.Parse(Console.ReadLine());
  30. b = double.Parse(Console.ReadLine());
  31. e = double.Parse(Console.ReadLine());
  32. double length = b - a;
  33. double middle = (b - a) / 2;
  34. switch (t)
  35. {
  36. case 1:
  37. if (f(a) == 0)
  38. {
  39.  
  40. return a;
  41. }
  42. else if (f(b) == 0)
  43. {
  44. return b;
  45. }
  46. else
  47. {
  48. while (length / 2 > e)
  49. {
  50. length /= 2;
  51. if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
  52. {
  53. a += length;
  54. }
  55. else
  56. {
  57. b -= length;
  58. }
  59. }
  60.  
  61. return a + length;
  62. }
  63. case 2:
  64. if (f(a) == 0)
  65. {
  66. return a;
  67. }
  68. else if (f(b) == 0)
  69. {
  70. return b;
  71. }
  72. else
  73. {
  74. while (length / 2 > e)
  75. {
  76. length /= 2;
  77. if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
  78. {
  79. a += length;
  80. }
  81. else
  82. {
  83. b -= length;
  84. }
  85. }
  86.  
  87. return a + length;
  88. }
  89. case 3:
  90. if (f(a) == 0)
  91. {
  92. return a;
  93. }
  94. else if (f(b) == 0)
  95. {
  96. return b;
  97. }
  98. else
  99. {
  100. while (length / 2 > e)
  101. {
  102. length /= 2;
  103. if (Math.Sign(f(a)) == Math.Sign(f(a + length)))
  104. {
  105. a += length;
  106. }
  107. else
  108. {
  109. b -= length;
  110. }
  111. }
  112.  
  113. return a + length;
  114. }
  115. }
  116.  
  117. return a + length;
  118. }
  119. static double hord()
  120. {
  121. return 1;
  122. }
  123. static double kas()
  124. {
  125. return 1;
  126. }
  127. static void Main()
  128. {
  129. MyFunc f = new MyFunc(f1);
  130. int a;
  131. Console.WriteLine("1 - x, 2 - x^2, 3 - sqrt(x)");
  132. a = int.Parse(Console.ReadLine());
  133. switch (a)
  134. {
  135. case 1:
  136. MyMethod g = new MyMethod(f1);
  137. break;
  138. case 2:
  139. g = new MyMethod(f2);
  140. break;
  141. case 3:
  142. g = new MyMethod(f3);
  143. break;
  144. }
  145.  
  146. double x = 4;
  147. Console.WriteLine("f({0}) = {1}", x, g(x));
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement