Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using std::cin;
  5. using std::cout;
  6. using std::cerr;
  7. using std::ifstream;
  8. const char* ERROR_CIN = "Ошибка ввода";
  9. const char* ERROR_FILE = "Ошибка открытия файла";
  10. const char* ERROR_FILE_FIRST_LINE = "Ошибка на первой строке файла должно быть число";
  11. const char* ERROR_FILE_LINE = "Ошибка в строке файла должно быть число";
  12. const char* ERROR_LEN = "Неверное кол-во элементов";
  13. void intArray(int* Array, const int nArray)
  14. {
  15. for (int i = 0; i < nArray; i++)
  16. {
  17. cin >> Array[i];
  18. if (!cin)
  19. {
  20. throw ERROR_CIN;
  21. }
  22. }
  23.  
  24. }
  25. void outArray(int* Array, const int nArray)
  26. {
  27. for (int i = 0; i < nArray; i++)
  28. {
  29. cout << Array[i] << "\n";
  30. }
  31. }
  32. bool isOrderedArray(const int* Array, int nElem)
  33. {
  34. if (sizeof(Array) / sizeof(*Array) < nElem)
  35. {
  36. throw ERROR_LEN;
  37. }
  38. bool temp = true;
  39. for (int i = 1; i < nElem; i++)
  40. {
  41. if (Array[i - 1] > Array[i])
  42. {
  43. bool temp = false;
  44. }
  45. }
  46. return temp;
  47. }
  48. //void permutationArray(int* Array, const int nArray) {
  49. // if (nArray % 2 == 0)
  50. // {
  51. // for (int i = 0; i < nArray; i++)
  52. // {
  53. //
  54. // }
  55. // }
  56. //}
  57.  
  58. //инд задание
  59. int maxArray(int* Array, const int nArray)
  60. {
  61. int i = 0;
  62. int min = std::numeric_limits<int>::max();
  63. int count = 0;
  64. int pos = 0;
  65. if (nArray < 2)
  66. {
  67. throw ERROR_LEN;
  68. }
  69. for (int i = 0; i < nArray; i++)
  70. {
  71. if (min >= Array[i]) {
  72. min = Array[i];
  73. pos = i;
  74. }
  75. }
  76. for (int i = pos; i > 0; i--)
  77. {
  78. if (Array[i] > 0)
  79. {
  80. count++;
  81. }
  82. }
  83. return count;
  84. }
  85.  
  86. int main()
  87. {
  88. setlocale(LC_ALL, "");
  89.  
  90. try
  91. {
  92. //фикс массив
  93. const int nMas = 10;
  94. int mas[nMas]{0};
  95. cout << "Введите 10 чисел в сатический массив \n";
  96. for (int i = 0; i < nMas; i++)
  97. {
  98. if (!cin)
  99. {
  100. throw ERROR_CIN;
  101. }
  102. cin >> mas[i];
  103. }
  104. cout << maxArray(mas, nMas);
  105. //динамический массив
  106. cout << "\nВведите длину динамического массива и его значения";
  107. int nMasDyn = 0;
  108. cin >> nMasDyn;
  109. int* masDyn = new int[nMasDyn];
  110. for (int i = 0; i < nMasDyn; i++)
  111. {
  112. if (!cin)
  113. {
  114. throw ERROR_CIN;
  115. }
  116. cin >> masDyn[i];
  117. }
  118. cout << maxArray(masDyn, nMasDyn);
  119. //массивы из файла
  120. cout << "\nВывод массива из файла";
  121. ifstream in;
  122. in.open("text.txt");
  123. if (!in)
  124. throw ERROR_FILE;
  125. int nMasCount = 0;
  126. in >> nMasCount;
  127. if (!in)
  128. throw ERROR_FILE_FIRST_LINE;
  129. int nMasFile = 0;
  130. for (int i = 0; i < nMasCount; i++)
  131. {
  132. in >> nMasFile;
  133.  
  134. }
  135.  
  136. }
  137. catch(const char* error)
  138. {
  139. cerr << error << "\n";
  140. return -1;
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement