Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9. class Massiv
  10. { /// <summary>
  11. /// Поле с элементами массива
  12. /// </summary>
  13. private double[] _a;
  14. /// <summary>
  15. /// Поле с числами элементов массива
  16. /// </summary>
  17. private int _n;
  18. /// <summary>
  19. /// Свойство с полем размера массива
  20. /// </summary>
  21. public int N
  22. {
  23. get { return _n; }
  24. set { _n = value; }
  25. }
  26. /// <summary>
  27. /// Метод ввода массива
  28. /// </summary>
  29. public void Input()
  30. {
  31. _a = new double[_n]; //выделили память для массива
  32. for (int i = 0; i < _n; i++)
  33. {
  34. Console.Write("Введите " + (i + 1) + " элемент массива: ");
  35. _a[i] = DoubleControl();
  36. }
  37. }
  38. /// <summary>
  39. /// Метод вывода массива на экран
  40. /// </summary>
  41. public void Print()
  42. {
  43. for (int i = 0; i < _n; i++)
  44. {
  45. Console.Write(_a[i] + "\t");
  46. }
  47. }
  48. /// <summary>
  49. /// Метод нахождения суммы элементов массива с нечётными номерами
  50. /// </summary>
  51. /// <returns></returns>
  52. public double Sum()
  53. {
  54. double sum = 0;
  55. for (int i = 0; i < _n; i++)
  56. {
  57. if (i % 2 == 0) sum += _a[i];
  58. }
  59. return sum;
  60. }
  61. /// <summary>
  62. /// Метод сворачивания
  63. /// </summary>
  64. public void Squeeze()
  65. {
  66. for (int i = 0; i < _n; i++)
  67. if (Math.Abs(_a[i]) < 1) //всем элементам, которые по модулю меньше 1 присвоили 0
  68. _a[i] = 0;
  69. double temp;
  70. for (int k = 0; k < _n - 1; k++)
  71. for (int i = 0; i < _n - 1 - k; i++)
  72. if (_a[i] == 0)
  73. {
  74. temp = _a[i];
  75. _a[i] = _a[i + 1];
  76. _a[i + 1] = temp;
  77. }
  78. //for (int i = 0; i < _n - 1; i++)
  79. // if (_a[i] == 0)
  80. // {
  81. // _a[i] = _a[i + 1];
  82. // _a[i + 1] = 0;
  83. // }
  84. }
  85.  
  86. public int IntControl()
  87. {
  88. string str = Console.ReadLine();
  89. int x;
  90. while (!int.TryParse(str, out x))
  91. {
  92. Console.WriteLine("Повторите код");
  93. str = Console.ReadLine();
  94. }
  95. return x;
  96. }
  97.  
  98.  
  99. public double DoubleControl()
  100. {
  101. string str = Console.ReadLine();
  102. double x;
  103. while (!double.TryParse(str, out x))
  104. {
  105. Console.WriteLine("Повторите ввод");
  106. str = Console.ReadLine();
  107. }
  108. return x;
  109. }
  110. private bool TestFirstNegative()
  111. {
  112. int i = 0;
  113. while (i < _n && _a[i] >= 0)
  114. i++;
  115. if (i == _n)
  116. return false;
  117. else
  118. return true;
  119. }
  120. private bool TestLastNegative()
  121. {
  122. int i = _n - 1;
  123. while (i >= 0 && _a[i] >= 0)
  124. i--;
  125. if (i == -1) //если вышел за границу, то отриц элемента нет
  126. return false;
  127. else
  128. return true;
  129. }
  130. private int SearchFirstNegative()
  131. {
  132. int i = 0;
  133. while (i < _n && _a[i] >= 0)
  134. i++;
  135. return i;
  136. }
  137. private double SearchLastNegative()
  138. {
  139. int i = _n - 1;
  140. while (i >= 0 && _a[i] >= 0)
  141. i--;
  142. return i;
  143. }
  144.  
  145. public void SumBetween()
  146. {
  147. double sum = 0;
  148. if (TestFirstNegative() && TestLastNegative())
  149. {
  150. for (int i = SearchFirstNegative() + 1; i < SearchLastNegative(); i++)
  151. sum += _a[i];
  152. Console.WriteLine("\nСумма элементов массива, расположенных между первым и последним \nотрицательным элементом равна:" + sum);
  153. }
  154. else
  155. Console.WriteLine("Ошибка, отсутствует либо первый, либо последний отрицательный элементы");
  156. }
  157. }
  158. }
  159.  
  160.  
  161. __________________________________________
  162. ___________________________________________
  163.  
  164.  
  165.  
  166. using System;
  167. using System.Collections.Generic;
  168. using System.Linq;
  169. using System.Text;
  170. using System.Threading.Tasks;
  171.  
  172. namespace ConsoleApp1
  173. {
  174. class Program
  175. {
  176. static void Main(string[] args)
  177. {
  178. Massiv Mass = new Massiv();
  179. try
  180. {
  181. Console.Write("Введите число элементов массива: ");
  182. Mass.N = Mass.IntControl();
  183. Mass.Input();
  184. Mass.Print();
  185. if (Mass.N != 0)
  186. {
  187. Console.WriteLine("\n\nСумма элементов массива с нечетными номерами равна: " + Mass.Sum());
  188. Mass.SumBetween();
  189. Console.WriteLine();
  190. Mass.Squeeze();
  191. Mass.Print();
  192. Console.WriteLine();
  193. }
  194. else Console.WriteLine("Число элементов массива должно быть больше 0, попробуйте еще раз.");
  195. }
  196. catch
  197. {
  198. Console.WriteLine("Ошибка!");
  199. }
  200. Console.WriteLine("Программа завершена. Нажмите любую клавишу...");
  201. Console.ReadKey();
  202. }
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement