Сдвинуть указанный столбец на n ячеек вверх и вниз
10.03.2016, 13:17. Показов 846. Ответов 0
Задание: сдвинуть указанный столбец на n ячеек вверх и вниз
colN - номер столбца
shiftStep - число сдвигов
Вот мои тщетные попытки (*cpp)
| C++ | 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
| //Подключаемые заголовочные файлы
#include "MyForm.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//Прототипы пользовательских функций
void FillArray(int arrayDimension, int randomMax, int** array);
void DeleteArray(int arrayDimension, int** array);
void nayhnic(int arrayDimension, int tbColN, int tbshiftStep, int randomMax, int** array);
double Ariph(int arrayDimension, int** array);
void sdvig(int arrayDimension, int** array, int tbColN, int tbshiftStep);
void stolbi(int arrayDimension, int stolb, int** array, System::Windows::Forms::DataGridView^TTS);
int stolb, colN, shiftStep, sampleArray, to, N;
//Событие формы Load, позволяет задавать начальные свойства объектов
void Проект4::MyForm::MyForm_Load(System::Object^ sender, System::EventArgs^ e)
{
tbArrayDimension->Text = System::Convert::ToString(3);
tbRndomMax->Text = System::Convert::ToString(19);
tbcolN->Text = System::Convert::ToString(1);
tbshiftStep->Text = System::Convert::ToString(2);
dgvArrayView->ColumnCount = 3;
dgvArrayView->RowCount = 3;
}
void Проект4::MyForm::bArr_Click(System::Object^ sender, System::EventArgs^ e)
{
int** sampleArray; // Описание динамического массива
int rndMax; // Максимальное число для генератора случайных чисел
int arrayDimension; // Размерность массива sampleArray
arrayDimension = System::Convert::ToInt16(tbArrayDimension->Text);
rndMax = System::Convert::ToInt16(tbRndomMax->Text);
colN = System::Convert::ToInt16(tbcolN->Text);
shiftStep = System::Convert::ToInt16(tbshiftStep->Text);
dgvArrayView->ColumnCount = arrayDimension;
dgvArrayView->RowCount = arrayDimension;
sampleArray = new int*[arrayDimension];
for (int i = 0; i < arrayDimension; i++)
{
sampleArray[i] = new int[arrayDimension];
}
FillArray(arrayDimension, rndMax, sampleArray);
stolbi(arrayDimension, stolb, sampleArray, dgvArrayView);
double progress = Ariph(arrayDimension, sampleArray);
sdvig (arrayDimension, sampleArray, colN, shiftStep);
tbprogress->Text = System::Convert::ToString(progress);
DeleteArray(arrayDimension, sampleArray);
}
void FillArray(int arrayDimension, int randomMax, int** array)
{
srand((unsigned)time(NULL));
for (int i = 0; i < arrayDimension; i++)
{
for (int j = 0; j < arrayDimension; j++) {
array[i][j] = rand() % 12;
}
}
}
//void Проект4::MyForm::one_Click(System::Object^ sender, System::EventArgs^ e)
//{
// int sampleArray;
// colN = System::Convert::ToInt16(tbcolN->Text);
// shiftStep = System::Convert::ToInt16(tbshiftStep->Text);
//}
void MoveColumn(int** array, int tbshiftStep, int tbColN, int arrayDimension)
{
}
void Проект4::MyForm::two_Click(System::Object^ sender, System::EventArgs^ e)
{
colN = System::Convert::ToInt16(tbcolN->Text);
shiftStep = System::Convert::ToInt16(tbshiftStep->Text);
}
void sdvig(int arrayDimension, int** array, int tbColN, int tbshiftStep, N)
{
int i, *temp;
if (tbshiftStep)
{
while (tbColN-- > 0) {
for (i = 0; i < arrayDimension - 1; i++) {
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
else {
while (tbColN-- > 0) {
for (i = arrayDimension - 1; i > 0; i--) {
temp = array[i];
array[i] = array[i - 1];
array[i - 1] = temp;
}
}
}
}
double Ariph(int arrayDimension, int** array)
{
int sum = 0;
double Result = 0.0;
for (int i = 0; i < arrayDimension; i++)
{
for (int j = 0; j < arrayDimension; j++)
{
sum += array[i][j];
}
}
return System::Convert::ToDouble(sum) / System::Convert::ToDouble(arrayDimension * arrayDimension);
}
void Проект4::MyForm::bExit_Click(System::Object^ sender, System::EventArgs^ e)
{
Application::Exit();
}
void stolbi(int arrayDimension, int stolb, int** array, System::Windows::Forms::DataGridView^TTS)
{
for (int i = 0; i < arrayDimension; i++)
{
for (int j = 0; j < arrayDimension; j++)
{
TTS->Rows[i]->Cells[j]->Value =
System::Convert::ToString(array[i][j]);
}
}
}
//void nayhnic(int arrayDimension, int** array)
//{
//for (int i = 0; i < arrayDimension; i++)
//{
// for (int j = 0; j < arrayDimension; j++)
//{
// DATA->Rows[i]->Cells[j]->Value =
// System::Convert::ToString(array[i][j]);
// }
//}
//}
void DeleteArray(int arrayDimension, int** array)
{
for (int i = 0; i < arrayDimension; i++)
{
delete[]array[i];
}
delete[] array;
} |
|
и файл (*h)
| C++ | 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
| #pragma once
namespace Проект4 {
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>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::DataGridView^ dgvArrayView;
protected:
private: System::Windows::Forms::TextBox^ tbRndomMax;
private: System::Windows::Forms::TextBox^ tbArrayDimension;
private: System::Windows::Forms::Button^ bExit;
private: System::Windows::Forms::Button^ bArr;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::TextBox^ tbprogress;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::TextBox^ tbcolN;
private: System::Windows::Forms::TextBox^ tbshiftStep;
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->dgvArrayView = (gcnew System::Windows::Forms::DataGridView());
this->tbRndomMax = (gcnew System::Windows::Forms::TextBox());
this->tbArrayDimension = (gcnew System::Windows::Forms::TextBox());
this->bArr = (gcnew System::Windows::Forms::Button());
this->bExit = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->tbprogress = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->tbcolN = (gcnew System::Windows::Forms::TextBox());
this->tbshiftStep = (gcnew System::Windows::Forms::TextBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dgvArrayView))->BeginInit();
this->SuspendLayout();
//
// dgvArrayView
//
this->dgvArrayView->ColumnHeadersHeightSizeMode = System::Windows::Forms::DataGridViewColumnHeadersHeightSizeMode::AutoSize;
this->dgvArrayView->Location = System::Drawing::Point(12, 12);
this->dgvArrayView->Name = L"dgvArrayView";
this->dgvArrayView->Size = System::Drawing::Size(328, 219);
this->dgvArrayView->TabIndex = 0;
//
// tbRndomMax
//
this->tbRndomMax->Location = System::Drawing::Point(349, 80);
this->tbRndomMax->Name = L"tbRndomMax";
this->tbRndomMax->Size = System::Drawing::Size(145, 20);
this->tbRndomMax->TabIndex = 1;
this->tbRndomMax->TextChanged += gcnew System::EventHandler(this, &MyForm::tbRndomMax_TextChanged);
//
// tbArrayDimension
//
this->tbArrayDimension->Location = System::Drawing::Point(349, 28);
this->tbArrayDimension->Name = L"tbArrayDimension";
this->tbArrayDimension->Size = System::Drawing::Size(145, 20);
this->tbArrayDimension->TabIndex = 11;
//
// bArr
//
this->bArr->Location = System::Drawing::Point(349, 179);
this->bArr->Name = L"bArr";
this->bArr->Size = System::Drawing::Size(145, 23);
this->bArr->TabIndex = 10;
this->bArr->Text = L"Создать массив";
this->bArr->UseVisualStyleBackColor = true;
this->bArr->Click += gcnew System::EventHandler(this, &MyForm::bArr_Click);
//
// bExit
//
this->bExit->Location = System::Drawing::Point(349, 208);
this->bExit->Name = L"bExit";
this->bExit->Size = System::Drawing::Size(145, 23);
this->bExit->TabIndex = 4;
this->bExit->Text = L"Выход";
this->bExit->UseVisualStyleBackColor = true;
this->bExit->Click += gcnew System::EventHandler(this, &MyForm::bExit_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(346, 12);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(122, 13);
this->label1->TabIndex = 5;
this->label1->Text = L"Размерность массива";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(346, 64);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(104, 13);
this->label2->TabIndex = 16;
this->label2->Text = L"Max число для ГСЧ";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(346, 120);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(139, 13);
this->label3->TabIndex = 17;
this->label3->Text = L"Среднее арифметическое";
//
// tbprogress
//
this->tbprogress->Location = System::Drawing::Point(349, 136);
this->tbprogress->Name = L"tbprogress";
this->tbprogress->Size = System::Drawing::Size(145, 20);
this->tbprogress->TabIndex = 18;
this->tbprogress->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(549, 133);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(145, 23);
this->button1->TabIndex = 19;
this->button1->Text = L"Up";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::one_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(549, 179);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(145, 23);
this->button2->TabIndex = 20;
this->button2->Text = L"Down";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &MyForm::two_Click);
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(557, 12);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(85, 13);
this->label4->TabIndex = 21;
this->label4->Text = L"Номер столбца";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(557, 64);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(83, 13);
this->label5->TabIndex = 22;
this->label5->Text = L"Число сдвигов";
//
// tbcolN
//
this->tbcolN->Location = System::Drawing::Point(549, 28);
this->tbcolN->Name = L"tbcolN";
this->tbcolN->Size = System::Drawing::Size(145, 20);
this->tbcolN->TabIndex = 23;
//
// tbshiftStep
//
this->tbshiftStep->Location = System::Drawing::Point(549, 80);
this->tbshiftStep->Name = L"tbshiftStep";
this->tbshiftStep->Size = System::Drawing::Size(145, 20);
this->tbshiftStep->TabIndex = 24;
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(739, 274);
this->Controls->Add(this->tbshiftStep);
this->Controls->Add(this->tbcolN);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Controls->Add(this->tbprogress);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->bExit);
this->Controls->Add(this->bArr);
this->Controls->Add(this->tbArrayDimension);
this->Controls->Add(this->tbRndomMax);
this->Controls->Add(this->dgvArrayView);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->dgvArrayView))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e);
private:System::Void bArr_Click(System::Object^ sender, System::EventArgs^ e);
private:System::Void bExit_Click(System::Object^ sender, System::EventArgs^ e);
private:System::Void one_Click(System::Object^ sender, System::EventArgs^ e);
private:System::Void two_Click(System::Object^ sender, System::EventArgs^ e);
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void tbRndomMax_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
};
} |
|
Добавлено через 1 минуту
Меня конкретно интересует функция sdvig
0
|