Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. // ConsoleApplication2.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
  2. //
  3.  
  4. #include <iostream>
  5. #include <clocale>
  6. #include <stdlib.h>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. class mas
  12. {
  13. protected:
  14. int* a;
  15. int n;
  16. public:
  17.  
  18. mas() // Конструктор по умолчанию
  19. {
  20. }
  21.  
  22. mas(int count)
  23. {
  24. n = count;
  25. a = new int[n];
  26. }
  27.  
  28. mas(const mas& t) // Конструктор копирования
  29. {
  30. n = t.n;
  31. a = new int[t.n];
  32. for (int i = 0; i < n; i++)
  33. {
  34. a[i] = t.a[i];
  35. }
  36. }
  37.  
  38. ~mas() { delete[] a; }; // Деструктор массива
  39.  
  40. void input_rand()
  41. {
  42. for (int i = 0; i < n; i++)
  43. {
  44. a[i] = rand() % 20 ;
  45. }
  46. }
  47. void input()
  48. {
  49. cout << "Введите массив" << endl;
  50. for (int i = 0; i < n; i++)
  51. {
  52. cin >> a[i];
  53. }
  54. }
  55.  
  56. void output() // Метод вывода массива
  57. {
  58. for (int i = 0; i < n; i++)
  59. {
  60. cout << a[i] << " ";
  61. }
  62. cout << endl;
  63. }
  64.  
  65. };
  66.  
  67. class mass2 : public mas {
  68.  
  69. protected:
  70.  
  71. public:
  72. int m, n;//строки. столбцы
  73.  
  74. mass2() // Конструктор по умолчанию
  75. {
  76. cout << "введите размер (строки. столбцы): ";
  77. cin >> m;
  78. cin >> n;
  79. a = new int[m * n];
  80.  
  81. for (int i = 0; i < m * n; i++)
  82. {
  83. a[i] = rand() % 20 - 10;
  84. }
  85. }
  86.  
  87. mass2(int f, int q) // Конструктор задания матрицы
  88. {
  89. m = f;
  90. n = q;
  91. a = new int[m * n];
  92. }
  93.  
  94. ~mass2() // Деструктор
  95. {
  96.  
  97. }
  98.  
  99. void in()
  100. {
  101. int mas_counter = 0;
  102. cout << "Введите матрицу: " << endl;
  103. for (int i = 0; i < m; i++)
  104. {
  105. for (int j = 0; j < n; j++)
  106. {
  107. cin >> a[mas_counter];
  108. mas_counter++;
  109. }
  110. }
  111. }
  112.  
  113. void out() // Метод вывода
  114. {
  115. int mas_counter = 0;
  116. for (int i = 0; i < m; i++)
  117. {
  118. for (int j = 0; j < n; j++)
  119. {
  120. cout << a[mas_counter] << "\t";
  121. mas_counter++;
  122. }
  123. cout << endl;
  124. }
  125. }
  126.  
  127. };
  128.  
  129. int main()
  130. {
  131. setlocale(LC_ALL, "rus");
  132. srand(time(0));
  133. int key;
  134. int size;
  135. cout << "Введите номер задания(1-одномерный. 2-двумерный): "; cin >> key;
  136. if (key == 1) {
  137. key = 0;
  138. cout << "1-rand; 2-ввести с клавиатуры: ";
  139. cin >> key;
  140. if(key==1){
  141. cout << "Ввeдите размер массива: ";
  142. cin >> size;
  143. mas mass1(size);
  144. mass1.input_rand();
  145. mass1.output();
  146. }
  147.  
  148. if (key == 2) {
  149. cout << "Ввeдите размер массива: ";
  150. cin >> size;
  151. mas mass1(size);
  152. mass1.input();
  153. mass1.output();
  154. }
  155.  
  156. }
  157.  
  158. if (key == 2) {
  159. key = 0;
  160. cout << "1-rand; 2-ввести с клавиатуры: ";
  161. cin >> key;
  162. if (key == 1) {
  163. mass2 mas1;
  164. mas1.out();
  165. }
  166.  
  167. if (key == 2) {
  168. int f, q;
  169. cout << "введите кол-во строк: "; cin >> f;
  170. cout << "введите кол-во stolbcov: "; cin >> q;
  171. mass2 mas2(f, q);
  172. mas2.in();
  173. mas2.out();
  174. }
  175. }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement