1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
| #pragma once
namespace LR1_T12 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Label^ label1;
protected:
private: System::Windows::Forms::TextBox^ txtMLen;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::TextBox^ txtMMin;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ txtMMax;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Button^ btnCreate;
private: System::Windows::Forms::Button^ btnDoIt;
private: System::Windows::Forms::Button^ btnClear;
private: System::Windows::Forms::DataGridView^ dataGridView1;
private: System::Windows::Forms::DataGridView^ dataGridView2;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->txtMLen = (gcnew System::Windows::Forms::TextBox());
this->label2 = (gcnew System::Windows::Forms::Label());
this->txtMMin = (gcnew System::Windows::Forms::TextBox());
this->label3 = (gcnew System::Windows::Forms::Label());
this->txtMMax = (gcnew System::Windows::Forms::TextBox());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->btnCreate = (gcnew System::Windows::Forms::Button());
this->btnDoIt = (gcnew System::Windows::Forms::Button());
this->btnClear = (gcnew System::Windows::Forms::Button());
this->dataGridView1 = (gcnew System::Windows::Forms::DataGridView());
this->dataGridView2 = (gcnew System::Windows::Forms::DataGridView());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView2))->BeginInit();
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 13);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(199, 17);
this->label1->TabIndex = 0;
this->label1->Text = L"Длина одномерного массива";
//
// txtMLen
//
this->txtMLen->Location = System::Drawing::Point(219, 13);
this->txtMLen->Name = L"txtMLen";
this->txtMLen->Size = System::Drawing::Size(59, 23);
this->txtMLen->TabIndex = 1;
this->txtMLen->Text = L"10";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(16, 47);
this->label2->Name = L"label2";
this->label2->RightToLeft = System::Windows::Forms::RightToLeft::No;
this->label2->Size = System::Drawing::Size(216, 17);
this->label2->TabIndex = 2;
this->label2->Text = L"Интервал значений массива от";
//
// txtMMin
//
this->txtMMin->Location = System::Drawing::Point(239, 47);
this->txtMMin->Name = L"txtMMin";
this->txtMMin->Size = System::Drawing::Size(56, 23);
this->txtMMin->TabIndex = 3;
this->txtMMin->Text = L"-99";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(302, 47);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(24, 17);
this->label3->TabIndex = 4;
this->label3->Text = L"до";
//
// txtMMax
//
this->txtMMax->Location = System::Drawing::Point(333, 47);
this->txtMMax->Name = L"txtMMax";
this->txtMMax->Size = System::Drawing::Size(51, 23);
this->txtMMax->TabIndex = 5;
this->txtMMax->Text = L"99";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(19, 87);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(127, 17);
this->label4->TabIndex = 6;
this->label4->Text = L"Исходный массив:";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(361, 87);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(175, 17);
this->label5->TabIndex = 7;
this->label5->Text = L"Результирующий массив:";
//
// btnCreate
//
this->btnCreate->Location = System::Drawing::Point(184, 132);
this->btnCreate->Name = L"btnCreate";
this->btnCreate->Size = System::Drawing::Size(162, 32);
this->btnCreate->TabIndex = 8;
this->btnCreate->Text = L"Создать массив";
this->btnCreate->UseVisualStyleBackColor = true;
this->btnCreate->Click += gcnew System::EventHandler(this, &Form1::btnCreate_Click);
//
// btnDoIt
//
this->btnDoIt->Location = System::Drawing::Point(184, 180);
this->btnDoIt->Name = L"btnDoIt";
this->btnDoIt->Size = System::Drawing::Size(162, 32);
this->btnDoIt->TabIndex = 9;
this->btnDoIt->Text = L"Обработать массив";
this->btnDoIt->UseVisualStyleBackColor = true;
this->btnDoIt->Click += gcnew System::EventHandler(this, &Form1::btnDoIt_Click);
//
// btnClear
//
this->btnClear->Location = System::Drawing::Point(184, 231);
this->btnClear->Name = L"btnClear";
this->btnClear->Size = System::Drawing::Size(162, 32);
this->btnClear->TabIndex = 10;
this->btnClear->Text = L"Очистить массив";
this->btnClear->UseVisualStyleBackColor = true;
this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click);
//
// dataGridView1
//
this->dataGridView1->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView1->Location = System::Drawing::Point(22, 113);
this->dataGridView1->Name = L"dataGridView1";
this->dataGridView1->Size = System::Drawing::Size(141, 293);
this->dataGridView1->TabIndex = 11;
//
// dataGridView2
//
this->dataGridView2->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dataGridView2->Location = System::Drawing::Point(364, 113);
this->dataGridView2->Name = L"dataGridView2";
this->dataGridView2->Size = System::Drawing::Size(164, 293);
this->dataGridView2->TabIndex = 12;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(553, 418);
this->Controls->Add(this->dataGridView2);
this->Controls->Add(this->dataGridView1);
this->Controls->Add(this->btnClear);
this->Controls->Add(this->btnDoIt);
this->Controls->Add(this->btnCreate);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->txtMMax);
this->Controls->Add(this->label3);
this->Controls->Add(this->txtMMin);
this->Controls->Add(this->label2);
this->Controls->Add(this->txtMLen);
this->Controls->Add(this->label1);
this->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->Margin = System::Windows::Forms::Padding(4);
this->Name = L"Form1";
this->Text = L"Контрольная работа №1 - Задание 1.2 (вариант 10)";
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->dataGridView2))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//Объявляем переменные
private: int ArrayLength; //Переменная для хранения длинны одномерного массива
private: double minValue; //Переменная для хранения нижней границы значений массива
private: double maxValue; //Переменная для хранения верхней границы значений массива
private: int *mainArray; //Переменная для хранения динамического одномерного массива
private: System::Void btnCreate_Click(System::Object^ sender, System::EventArgs^ e) {
// нажатие кнопки СОЗДАТЬ МАССИВ
//Получаем длинну массива задаваемую пользователем
ArrayLength = Convert::ToInt32(txtMLen->Text);
//Получаем границы интервала
minValue = Convert::ToInt32(txtMMin->Text);
maxValue = Convert::ToInt32(txtMMax->Text);
//Создаем массив
mainArray = new int[ArrayLength];
//Наполняем массив значениями
Random^ r = gcnew Random; //Объявляем генератор случайных чисел
for (int i = 0; i < ArrayLength; i++) {
mainArray[i] = Convert::ToInt32(minValue + Convert::ToDouble(r->Next(1, 100)) / 100.0 * Convert::ToDouble(System::Math::Abs(minValue - maxValue)));
}
//Выводим значения в таблицу
dataGridView1->Columns->Add("Values", "Значения");
for (int j = 0; j < ArrayLength; j++) {
dataGridView1->Rows->Add();
dataGridView1->Rows[j]->Cells[0]->Value = mainArray[j];
}
}
private: System::Void btnDoIt_Click(System::Object^ sender, System::EventArgs^ e) {
// нажатие кнопки ОБРАБОТАТЬ МАССИВ
//Передаем массив на сортировку
MergeSort(mainArray, 0, ArrayLength);
//Выводим массив в таблицу "результирующий массив"
dataGridView2->Columns->Add("Values", "Значения");
for (int j = 0; j < ArrayLength; j++) {
dataGridView2->Rows->Add();
dataGridView2->Rows[j]->Cells[0]->Value = mainArray[j];
}
}
//Функция осуществляет сортировку методом перемещивания
private:System::Void Merge(int* A, int first, int last)
{
int middle, start, final, j;
int* mas = new int[100];
middle = (first + last) / 2; //вычисление среднего элемента
start = first; //начало левой части
final = middle + 1; //начало правой части
for (j = first; j <= last; j++) //выполнять от начала до конца
if ((start <= middle) && ((final > last) || (A[start] < A[final])))
{
mas[j] = A[start];
start++;
}
else
{
mas[j] = A[final];
final++;
}
//возвращение результата в список
for (j = first; j <= last; j++) A[j] = mas[j];
delete[]mas;
};
//рекурсивная процедура сортировки
void MergeSort(int* A, int first, int last)
{
{
if (first < last)
{
MergeSort(A, first, (first + last) / 2); //сортировка левой части
MergeSort(A, (first + last) / 2 + 1, last); //сортировка правой части
Merge(A, first, last); //слияние двух частей
}
}
};
private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e) {
// нажатие кнопки ОЧИСТИТЬ МАССИВ
//Очищаем таблицы
dataGridView1->Columns->Clear();
dataGridView2->Columns->Clear();
}
};
} |