Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.58 KB | None | 0 0
  1. int *chislo = new int[matrix.columns];
  2. for (int j = 0; j < matrix.columns; j++) {
  3. chislo[j] = 0;
  4. }
  5.  
  6. cout << "nnПроверка на подходимость числа:" << endl;
  7.  
  8. // Проверка на подходимость и занесение подходящих чисел в массив chislo
  9. for (int i = 0; i < matrix.rows; i++) {
  10. for (int j = 0; j < matrix.columns; j++) {
  11. if (matrix.ptr[i][j] % 2 == -1) {
  12. cout << " +"; // отладочный вывод
  13. chislo[j] += matrix.ptr[i][j];
  14. }
  15. else {
  16. cout << " -"; // отладочный вывод
  17. chislo[j] += 0;
  18. }
  19. }
  20. cout << endl;
  21. }
  22. cout << endl;
  23.  
  24. for (int i = 1; i < matrix.columns; ++i) {
  25. size_t smallest_stolb = matrix.columns - i;
  26. // находим самый маленькую характеристику столбов
  27. for (int j = 0; j < matrix.columns - i; j++)
  28. if (chislo[j] < chislo[smallest_stolb])
  29. smallest_stolb = j;
  30. // столбы меняем, и их характеристику
  31. for (int j = 0; j < matrix.rows; j++) {
  32. int tmp = matrix.ptr[j][matrix.columns - i];
  33. matrix.ptr[j][matrix.columns - i] = matrix.ptr[j][smallest_stolb];
  34. matrix.ptr[j][smallest_stolb] = tmp;
  35. }
  36. { int temp = chislo[matrix.columns - i];
  37. chislo[matrix.columns - i] = chislo[smallest_stolb];
  38. chislo[smallest_stolb] = temp; }
  39. }
  40.  
  41. cout << "Отсортированные характеристики:" << endl;
  42.  
  43. // Проверял такой массив:
  44. // -3 -5 1 0
  45. // 1 -3 -1 0
  46. // -2 3 0 0
  47.  
  48. // Проверка корректного занесения подходящих чисел со столбцов матрицы в одномерный массив chislo
  49. for (int j = 0; j < matrix.columns; j++) {
  50. cout << chislo[j] << " "; // отладочный вывод
  51. }
  52. cout << "n" << endl;
  53.  
  54. cout << "Отсортированная матрица:" << endl;
  55. outputMatrix(matrix);
  56.  
  57. #pragma once
  58. #include <math.h>
  59. #include <time.h>
  60. #include <iostream>
  61. #include <cstdlib>
  62.  
  63. struct Matrix {
  64. int rows;
  65. int columns;
  66. int** ptr;
  67. };
  68.  
  69. Matrix matrix;
  70.  
  71. #pragma once
  72. #include "Header.h"
  73.  
  74. namespace Engineering6 {
  75.  
  76. using namespace System;
  77. using namespace System::ComponentModel;
  78. using namespace System::Collections;
  79. using namespace System::Windows::Forms;
  80. using namespace System::Data;
  81. using namespace System::Drawing;
  82.  
  83. /// <summary>
  84. /// Сводка для MyForm
  85. /// </summary>
  86. public ref class MyForm : public System::Windows::Forms::Form
  87. {
  88. public:
  89. MyForm(void)
  90. {
  91. InitializeComponent();
  92. //
  93. //TODO: добавьте код конструктора
  94. //
  95. }
  96.  
  97. protected:
  98. /// <summary>
  99. /// Освободить все используемые ресурсы.
  100. /// </summary>
  101. ~MyForm()
  102. {
  103. if (components)
  104. {
  105. delete components;
  106. }
  107. }
  108.  
  109. private: System::Windows::Forms::DataGridView^ dataGridView1;
  110. private: System::Windows::Forms::TextBox^ textBox1;
  111. private: System::Windows::Forms::TextBox^ textBox2;
  112. private: System::Windows::Forms::Button^ button1;
  113. private: System::Windows::Forms::Label^ label1;
  114. private: System::Windows::Forms::Label^ label2;
  115. private: System::Windows::Forms::Label^ label3;
  116. private: System::Windows::Forms::Label^ label4;
  117. private: System::Windows::Forms::Label^ label5;
  118. private: System::Windows::Forms::Label^ label6;
  119. private: System::Windows::Forms::Button^ button2;
  120. private: System::Windows::Forms::DataGridView^ dataGridView2;
  121.  
  122. protected:
  123.  
  124. protected:
  125.  
  126. private:
  127. /// <summary>
  128. /// Обязательная переменная конструктора.
  129. /// </summary>
  130. System::ComponentModel::Container ^components;
  131.  
  132. #pragma region Windows Form Designer generated code
  133. /// <summary>
  134. /// Требуемый метод для поддержки конструктора — не изменяйте
  135. /// содержимое этого метода с помощью редактора кода.
  136. /// </summary>
  137. void InitializeComponent(void)
  138. {
  139. System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MyForm::typeid));
  140. this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
  141. this->textBox1 = (gcnew System::Windows::Forms::TextBox());
  142. this->textBox2 = (gcnew System::Windows::Forms::TextBox());
  143. this->button1 = (gcnew System::Windows::Forms::Button());
  144. this->label1 = (gcnew System::Windows::Forms::Label());
  145. this->label2 = (gcnew System::Windows::Forms::Label());
  146. this->label3 = (gcnew System::Windows::Forms::Label());
  147. this->label4 = (gcnew System::Windows::Forms::Label());
  148. this->label5 = (gcnew System::Windows::Forms::Label());
  149. this->label6 = (gcnew System::Windows::Forms::Label());
  150. this->button2 = (gcnew System::Windows::Forms::Button());
  151. this->dataGridView2 = (gcnew System::Windows::Forms::DataGridView());
  152. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->BeginInit();
  153. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView2))->BeginInit();
  154. this->SuspendLayout();
  155. //
  156. // dataGridView1
  157. //
  158. this->dataGridView1->AllowUserToAddRows = false;
  159. this->dataGridView1->AllowUserToDeleteRows = false;
  160. this->dataGridView1->AllowUserToResizeColumns = false;
  161. this->dataGridView1->AllowUserToResizeRows = false;
  162. this->dataGridView1->BackgroundColor = System::Drawing::SystemColors::Control;
  163. this->dataGridView1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
  164. this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  165. this->dataGridView1->ColumnHeadersVisible = false;
  166. this->dataGridView1->GridColor = System::Drawing::SystemColors::Control;
  167. this->dataGridView1->Location = System::Drawing::Point(73, 136);
  168. this->dataGridView1->Name = L"dataGridView1";
  169. this->dataGridView1->RowHeadersVisible = false;
  170. this->dataGridView1->RowHeadersWidth = 30;
  171. this->dataGridView1->RowTemplate->Height = 24;
  172. this->dataGridView1->Size = System::Drawing::Size(375, 392);
  173. this->dataGridView1->TabIndex = 1;
  174. this->dataGridView1->CellContentClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MyForm::dataGridView1_CellContentClick);
  175. //
  176. // textBox1
  177. //
  178. this->textBox1->BackColor = System::Drawing::SystemColors::InactiveBorder;
  179. this->textBox1->ForeColor = System::Drawing::SystemColors::InfoText;
  180. this->textBox1->Location = System::Drawing::Point(260, 46);
  181. this->textBox1->Name = L"textBox1";
  182. this->textBox1->Size = System::Drawing::Size(65, 22);
  183. this->textBox1->TabIndex = 2;
  184. this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
  185. this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox1_KeyPress);
  186. //
  187. // textBox2
  188. //
  189. this->textBox2->BackColor = System::Drawing::SystemColors::InactiveBorder;
  190. this->textBox2->ForeColor = System::Drawing::SystemColors::InfoText;
  191. this->textBox2->Location = System::Drawing::Point(260, 73);
  192. this->textBox2->Name = L"textBox2";
  193. this->textBox2->Size = System::Drawing::Size(65, 22);
  194. this->textBox2->TabIndex = 3;
  195. this->textBox2->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox2_TextChanged);
  196. this->textBox2->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox2_KeyPress);
  197. //
  198. // button1
  199. //
  200. this->button1->BackColor = System::Drawing::Color::Gainsboro;
  201. this->button1->BackgroundImageLayout = System::Windows::Forms::ImageLayout::None;
  202. this->button1->ForeColor = System::Drawing::SystemColors::ActiveCaptionText;
  203. this->button1->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button1.Image")));
  204. this->button1->Location = System::Drawing::Point(331, 34);
  205. this->button1->Name = L"button1";
  206. this->button1->Size = System::Drawing::Size(85, 73);
  207. this->button1->TabIndex = 4;
  208. this->button1->TabStop = false;
  209. this->button1->UseVisualStyleBackColor = false;
  210. this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
  211. //
  212. // label1
  213. //
  214. this->label1->AutoSize = true;
  215. this->label1->Location = System::Drawing::Point(80, 48);
  216. this->label1->Name = L"label1";
  217. this->label1->Size = System::Drawing::Size(179, 17);
  218. this->label1->TabIndex = 5;
  219. this->label1->Text = L"Введите кол-во столбцов:";
  220. //
  221. // label2
  222. //
  223. this->label2->AutoSize = true;
  224. this->label2->Location = System::Drawing::Point(80, 75);
  225. this->label2->Name = L"label2";
  226. this->label2->Size = System::Drawing::Size(155, 17);
  227. this->label2->TabIndex = 6;
  228. this->label2->Text = L"Введите кол-во строк:";
  229. //
  230. // label3
  231. //
  232. this->label3->AutoSize = true;
  233. this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7.8F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
  234. static_cast<System::Byte>(204)));
  235. this->label3->ForeColor = System::Drawing::Color::Black;
  236. this->label3->Location = System::Drawing::Point(80, 12);
  237. this->label3->Name = L"label3";
  238. this->label3->Size = System::Drawing::Size(458, 17);
  239. this->label3->TabIndex = 7;
  240. this->label3->Text = L"[INFO] Заполните поля и нажмите кнопку для построения матрицы.";
  241. //
  242. // label4
  243. //
  244. this->label4->AutoSize = true;
  245. this->label4->ForeColor = System::Drawing::SystemColors::ControlDark;
  246. this->label4->Location = System::Drawing::Point(853, 497);
  247. this->label4->Name = L"label4";
  248. this->label4->Size = System::Drawing::Size(62, 34);
  249. this->label4->TabIndex = 8;
  250. this->label4->Text = L"made in rnDIIT";
  251. this->label4->TextAlign = System::Drawing::ContentAlignment::TopCenter;
  252. //
  253. // label5
  254. //
  255. this->label5->AutoSize = true;
  256. this->label5->Location = System::Drawing::Point(80, 109);
  257. this->label5->Name = L"label5";
  258. this->label5->Size = System::Drawing::Size(391, 17);
  259. this->label5->TabIndex = 9;
  260. this->label5->Text = L"[INFO] После нажатия на кнопку заполните массив ниже. ";
  261. //
  262. // label6
  263. //
  264. this->label6->AutoSize = true;
  265. this->label6->Location = System::Drawing::Point(640, 11);
  266. this->label6->Name = L"label6";
  267. this->label6->Size = System::Drawing::Size(180, 34);
  268. this->label6->TabIndex = 10;
  269. this->label6->Text = L"Нажмите на эту кнопку rnдля выполнения задания.";
  270. this->label6->TextAlign = System::Drawing::ContentAlignment::TopCenter;
  271. //
  272. // button2
  273. //
  274. this->button2->Image = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"button2.Image")));
  275. this->button2->Location = System::Drawing::Point(685, 53);
  276. this->button2->Name = L"button2";
  277. this->button2->Size = System::Drawing::Size(85, 73);
  278. this->button2->TabIndex = 11;
  279. this->button2->UseVisualStyleBackColor = true;
  280. this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click_1);
  281. //
  282. // dataGridView2
  283. //
  284. this->dataGridView2->BackgroundColor = System::Drawing::SystemColors::Control;
  285. this->dataGridView2->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
  286. this->dataGridView2->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
  287. this->dataGridView2->GridColor = System::Drawing::SystemColors::Control;
  288. this->dataGridView2->Location = System::Drawing::Point(472, 136);
  289. this->dataGridView2->Name = L"dataGridView2";
  290. this->dataGridView2->RowTemplate->Height = 24;
  291. this->dataGridView2->Size = System::Drawing::Size(375, 392);
  292. this->dataGridView2->TabIndex = 12;
  293. this->dataGridView2->CellContentClick += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &MyForm::dataGridView2_CellContentClick);
  294. //
  295. // MyForm
  296. //
  297. this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
  298. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  299. this->BackColor = System::Drawing::Color::Gainsboro;
  300. this->ClientSize = System::Drawing::Size(921, 540);
  301. this->Controls->Add(this->dataGridView2);
  302. this->Controls->Add(this->button2);
  303. this->Controls->Add(this->label6);
  304. this->Controls->Add(this->label5);
  305. this->Controls->Add(this->label4);
  306. this->Controls->Add(this->label3);
  307. this->Controls->Add(this->label2);
  308. this->Controls->Add(this->label1);
  309. this->Controls->Add(this->button1);
  310. this->Controls->Add(this->textBox2);
  311. this->Controls->Add(this->textBox1);
  312. this->Controls->Add(this->dataGridView1);
  313. this->ForeColor = System::Drawing::SystemColors::ActiveCaptionText;
  314. this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
  315. this->MaximizeBox = false;
  316. this->Name = L"MyForm";
  317. this->StartPosition = System::Windows::Forms::FormStartPosition::CenterParent;
  318. this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
  319. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView1))->EndInit();
  320. (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dataGridView2))->EndInit();
  321. this->ResumeLayout(false);
  322. this->PerformLayout();
  323.  
  324. }
  325. #pragma endregion
  326. private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
  327. }
  328. private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
  329. }
  330.  
  331. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
  332. dataGridView1->Columns->Clear();
  333. dataGridView1->Rows->Clear();
  334. dataGridView2->Columns->Clear();
  335. dataGridView2->Rows->Clear();
  336.  
  337. if (textBox1->Text->Length != 0) {
  338. dataGridView1->ColumnCount = Convert::ToInt32(textBox1->Text);
  339. dataGridView1->RowCount = Convert::ToInt32(textBox2->Text);
  340.  
  341. matrix.rows = Convert::ToInt32(textBox2->Text);
  342. matrix.columns = Convert::ToInt32(textBox2->Text);
  343.  
  344. // выделение памяти
  345. matrix.ptr = new int*[matrix.rows];
  346. for (int i = 0; i < matrix.rows; i++)
  347. matrix.ptr[i] = new int[matrix.columns];
  348.  
  349. // заполнение матрицы
  350. for (int i = 0; i < matrix.rows; i++) {
  351. for (int j = 0; j < matrix.columns; j++)
  352. matrix.ptr[i][j] = rand() % 21 + (-10);
  353. }
  354.  
  355. for (int i = 0; i < matrix.rows; i++) {
  356. for (int j = 0; j < matrix.rows; j++) {
  357. dataGridView1->Rows[i]->Cells[j]->Value = matrix.ptr[i][j];
  358. }
  359. }
  360.  
  361. /*dataGridView1->Columns[0]->Width = 50;
  362. dataGridView1->Columns[1]->Width = 50;
  363. dataGridView1->Columns[2]->Width = 50;
  364. dataGridView1->Columns[3]->Width = 50;*/
  365. }
  366. }
  367. private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
  368. char number = e->KeyChar;
  369. if ((e->KeyChar <= 47 || e->KeyChar >= 58) && number != 8 && number != 44 && number != 46 && number != 45) //цифры, клавиша BackSpace и запятая а ASCII
  370. {
  371. e->Handled = true;
  372. MessageBox::Show("Вводить можно только цифры, а также символы . , - ");
  373. }
  374. }
  375. private: System::Void textBox2_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e) {
  376. char number = e->KeyChar;
  377. if ((e->KeyChar <= 47 || e->KeyChar >= 58) && number != 8 && number != 44 && number != 46 && number != 45) //цифры, клавиша BackSpace и запятая а ASCII
  378. {
  379. e->Handled = true;
  380. MessageBox::Show("Вводить можно только цифры, а также символы . , - ");
  381. }
  382. }
  383. private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
  384.  
  385. }
  386. private: System::Void button2_Click_1(System::Object^ sender, System::EventArgs^ e) {
  387. int *chislo = new int[matrix.columns];
  388. for (int j = 0; j < matrix.columns; j++) {
  389. chislo[j] = 0;
  390. }
  391.  
  392. // Проверка на подходимость и занесение подходящих чисел в массив chislo
  393. for (int i = 0; i < matrix.rows; i++) {
  394. for (int j = 0; j < matrix.columns; j++) {
  395. if (matrix.ptr[i][j] % 2 == -1) {
  396. chislo[j] += matrix.ptr[i][j];
  397. }
  398. else {
  399. chislo[j] += 0;
  400. }
  401. }
  402. }
  403.  
  404. for (int i = 1; i < matrix.columns; ++i) {
  405. size_t smallest_stolb = matrix.columns - i;
  406. // находим самый маленькую характеристику столбов
  407. for (int j = 0; j < matrix.columns - i; j++)
  408. if (chislo[j] < chislo[smallest_stolb])
  409. smallest_stolb = j;
  410. // столбы меняем, и их характеристику
  411. for (int j = 0; j < matrix.rows; j++) {
  412. int tmp = matrix.ptr[j][matrix.columns - i];
  413. matrix.ptr[j][matrix.columns - i] = matrix.ptr[j][smallest_stolb];
  414. matrix.ptr[j][smallest_stolb] = tmp;
  415. }
  416. { int temp = chislo[matrix.columns - i];
  417. chislo[matrix.columns - i] = chislo[smallest_stolb];
  418. chislo[smallest_stolb] = temp; }
  419. }
  420.  
  421. // Проверял такой массив:
  422. // -3 -5 1 0
  423. // 1 -3 -1 0
  424. // -2 3 0 0
  425.  
  426. for (int i = 0; i < matrix.rows; i++) {
  427. for (int j = 0; j < matrix.columns; j++) {
  428. dataGridView2[j, i]->Value = matrix.ptr[i][j];
  429. }
  430. }
  431.  
  432. }
  433.  
  434. private: System::Void dataGridView1_CellContentClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
  435. }
  436. private: System::Void dataGridView2_CellContentClick(System::Object^ sender, System::Windows::Forms::DataGridViewCellEventArgs^ e) {
  437. }
  438. private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
  439. }
  440. private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e) {
  441. }
  442. };
  443. }
  444.  
  445. #include "MyForm.h"
  446.  
  447. using namespace System;
  448. using namespace System::Windows::Forms;
  449.  
  450. [STAThreadAttribute]
  451. void Main(array<String^>^ args) {
  452. Application::EnableVisualStyles();
  453. Application::SetCompatibleTextRenderingDefault(false);
  454. Engineering6::MyForm form;
  455. Application::Run(%form);
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement