Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. class Program
  10. {
  11. static double xmin = -1, xmax = 1, dx = 0.0001;
  12. static bool good = false;
  13. static void ReadFrom() //Функция считывает входные данные
  14. {
  15. good = false; //Переменная для проверки на правильный ввод
  16. Console.WriteLine("Please write x minimal:\t");
  17. while (good == false)
  18. {
  19. try // Дебажим на предмет некорректного ввода
  20. {
  21. xmin = Convert.ToDouble(Console.ReadLine());
  22. if (xmin < -6)
  23. good = true;
  24. else
  25. Console.WriteLine("Please write x mmaximal again. It must be >= -6 minimal:\t");
  26.  
  27.  
  28. }
  29. catch
  30. {
  31. Console.WriteLine("Error!\tTry again please:\t");
  32. }
  33. }
  34. good = false;
  35. Console.WriteLine("Please write x maximal:\t");
  36. while (good == false)
  37. {
  38. try // Дебажим на предмет некорректного ввода
  39. {
  40. xmax = Convert.ToDouble(Console.ReadLine());
  41. if (xmax > xmin && xmax > 6)
  42. good = true;
  43. else
  44. Console.WriteLine("Please write x mmaximal again. It must be < x minimal:\t");
  45. }
  46. catch
  47. {
  48. Console.WriteLine("Error!\tTry again please:\t");
  49. }
  50. }
  51. good = false;
  52. Console.WriteLine("Please write step dx:\t");
  53. while (good == false)
  54. {
  55. try // Дебажим на предмет некорректного ввода
  56. {
  57. dx = Convert.ToDouble(Console.ReadLine());
  58. if (dx > 0 && dx < xmax - xmin && isInRange(xmin, xmax, dx))
  59. good = true;
  60. else
  61. Console.WriteLine("Please write dx again. It must be positive and < (x maximal - x minimal):\t");
  62. }
  63. catch
  64. {
  65. Console.WriteLine("Error!\tTry again please:\t");
  66. }
  67. }
  68. }
  69. static void Main(string[] args)
  70. {
  71. ReadFrom();
  72. for (double x = xmin; x < xmax; x += dx)
  73. {
  74. string y = "Функции не существует.";
  75. if (x < -10)
  76. y = "Функции не существует.";
  77. else if ((-10 <= x) && (x < -6))
  78. y = (Math.Sqrt(Math.Pow(2, 2) - Math.Pow(x + 8, 2)) - 2).ToString();
  79. else if ((-6 <= x) && (x <= 2))
  80. y = (0.5 * x + 1).ToString();
  81. else if ((2 < x) && (x < 6))
  82. y = "Функции не существует.";
  83. else if ((6 <= x) && (x <= 8))
  84. y = (Math.Pow(x - 6, 2)).ToString();
  85. else if (8 < x)
  86. y = "Функции не существует.";
  87.  
  88. Console.WriteLine("X = " + x + "; Y = " + y);
  89. }
  90. Console.ReadKey();
  91. }
  92.  
  93. private static bool isInRange(double xmin, double xmax, double dx)
  94. {
  95. bool first = false;
  96. bool second = false;
  97. bool third = false;
  98.  
  99. for (double x = xmin; x < xmax; x += dx)
  100. {
  101.  
  102. if ((-10 <= x) && (x < -6))
  103. first = true;
  104. else if ((-6 <= x) && (x <= 2))
  105. second = true;
  106. else if ((6 <= x) && (x <= 8))
  107. third = true;
  108. }
  109. if (first && second && third)
  110. return true;
  111.  
  112. return false;
  113. }
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement