Перевод программы из консоли в приложение с формой
11.11.2017, 09:53. Показов 1084. Ответов 5
Доброго времени суток.
Попытался перевести проект в VCL. Получаю ошибки о выходе за диапазон массива. Помогите пожалуйста разобраться в том, что я не так делаю.
Вот оригинальный проект:
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
| using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BraunRobinson
{
class Program
{
static void Main(string[] args)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Black;
double[,] A = { { 7, 5, 2 }, { 2, 3, 11 } };
Console.WriteLine("Braun's method for matrix A:");
printMatr(A);
Braun(A, 0.001);
double[,] B = { { 1, 6 }, { 5, 4 }, { 0, 7 }, { 3, 5} };
Console.WriteLine("\n\nBraun's method for matrix B:");
printMatr(B);
Braun(B, 0.001);
Console.ReadKey();
}
private static void printMatr(double[,] m)
{
for (int i = 0; i < m.GetLength(0); i++)
{
for (int j = 0; j < m.GetLength(1); j++)
Console.Write(Math.Round(m[i, j], 3) + " ");
Console.WriteLine();
}
}
private static void Braun(double[,] matrix, double eps)
{
int m = matrix.GetLength(0);
int n = matrix.GetLength(1);
List<double> B = new List<double>();
List<double> cB = new List<double>();
List<double> A = new List<double>();
List<double> cA = new List<double>();
for (int i = 0; i < n; i++) { B.Add(0.0); cB.Add(0.0); }
for (int j = 0; j < m; j++) { A.Add(0.0); cA.Add(0.0); }
Random rand = new Random();
int f = rand.Next(m);
int min = 0;
double Vn = 0.0, Vv = 0.0, Vs = 0.0, Vold = 0.0;
int count=0;
int width=6;
Console.Write("┌─────┬───┬"); for (int i = 0; i < B.Count; i++) Console.Write("──────┬"); Console.Write("───┬"); for (int i = 0; i < A.Count + 2; i++) Console.Write("──────┬"); Console.Write("──────┐");
Console.WriteLine();
Console.Write("│K".PadRight(width) + "│" + "i │"); for (int i = 0; i < B.Count; i++) Console.Write("B:"+i.ToString().PadRight(width-2) + "│");
Console.Write("j │"); for (int i = 0; i < A.Count; i++) Console.Write("A:"+i.ToString().PadRight(width-2) + "│");
Console.Write("Vn".PadRight(width)+"│"+"Vv".PadRight(width)+"│"+"Vs".PadRight(width)+"│");
Console.WriteLine();
while (true)
{
Console.Write("├─────┼───┼"); for (int i = 0; i < B.Count; i++) Console.Write("──────┼"); Console.Write("───┼"); for (int i = 0; i < A.Count + 2; i++) Console.Write("──────┼"); Console.Write("──────┤");
Console.WriteLine();
count++;
cA[f]++;
for (int i = 0; i < B.Count; i++) B[i] += matrix[f, i];
min = B.IndexOf(B.Min());
cB[min]++;
for (int j = 0; j < A.Count; j++) A[j] += matrix[j, min];
Console.Write("│" + count.ToString().PadRight(width-1) + "│" + (f + 1).ToString().PadRight(3) + "│");
for (int i = 0; i < B.Count; i++) Console.Write(Math.Round(B[i], 3).ToString().PadRight(width) + "│");
Console.Write((min + 1).ToString().PadRight(3) + "│");
for (int j = 0; j < A.Count; j++) Console.Write(Math.Round(A[j], 3).ToString().PadRight(width) + "│");
Console.Write(Math.Round(Vn,3).ToString().PadRight(width)+"│"+Math.Round(Vv,3).ToString().PadRight(width)+"│"+Math.Round(Vs,3).ToString().PadRight(width)+"│");
Console.WriteLine();
Vn = B.Min() / count;
Vv = A.Max() / count;
Vs = (Vv + Vn)/2;
if ((Math.Abs(Vs - Vv)) < eps) break;
//if ((count%20)==0) Console.ReadLine();
Vold = Vs;
f = A.IndexOf(A.Max());
}
Console.Write("└─────┴───┴"); for (int i = 0; i < B.Count; i++) Console.Write("──────┴"); Console.Write("───┴"); for (int i = 0; i < A.Count + 2; i++) Console.Write("──────┴"); Console.Write("──────┘");
Console.WriteLine();
Console.WriteLine("V*= " + Math.Round(Vs, 3));
double[] p = new double[A.Count];
for (int i = 0; i < p.Length; i++) p[i] = cA[i] / count;
Console.Write("p*= ( "); for (int i = 0; i < p.Length; i++) Console.Write(Math.Round(p[i],3)+"; "); Console.WriteLine(")");
double[] q = new double[B.Count];
for (int i = 0; i < q.Length; i++) q[i] = cB[i] / count;
Console.Write("q*= ( "); for (int i = 0; i < q.Length; i++) Console.Write(Math.Round(q[i],3)+"; "); Console.WriteLine(")");
}
}
} |
|
Вот мой проект
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace S_TR
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int k, l;
private int[,] array_cell;
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Enabled = false;
button1.Enabled = false;
maskedTextBox1.Enabled = false;
maskedTextBox2.Enabled = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (maskedTextBox1.Text != "")
{
k = Convert.ToInt32(maskedTextBox1.Text);
dataGridView1.RowCount = k + 1;
maskedTextBox2.Enabled = true;
}
}
private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (maskedTextBox2.Text != "")
{
dataGridView1.Rows.Clear();
l = Convert.ToInt32(maskedTextBox2.Text);
dataGridView1.RowCount = k+1;
dataGridView1.ColumnCount =l;
for (int i = 1; i < l+1; i++)
{
dataGridView1.Columns[i - 1].Name = "B" + i.ToString();
dataGridView1.Columns[i - 1].Width = 70;
}
for (int i = 1; i <k+1; i++)
{
dataGridView1.Rows[i - 1].HeaderCell.Value = "A" + i.ToString();
}
dataGridView1.RowHeadersWidth = 135;
button1.Enabled = true;
}
}
private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
string cellValue = e.FormattedValue.ToString();
try
{
if ((Convert.ToInt32(cellValue) <= 0))
{
MessageBox.Show("Значение может быть только числовым!");
e.Cancel = true;
}
}
catch { }
}
private void выходToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void режимПодсчётаToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void справкаToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void выходToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bmp = new Bitmap(dataGridView1.Size.Width + 10, dataGridView1.Size.Height + 10);
dataGridView1.DrawToBitmap(bmp, dataGridView1.Bounds);
e.Graphics.DrawImage(bmp, 0, 0);
}
private void справкаToolStripMenuItem1_Click(object sender, EventArgs e)
{
dataGridView1.Enabled = true;
button1.Enabled = false;
maskedTextBox1.Enabled = true;
maskedTextBox2.Enabled = false;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void maskedTextBox2_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
}
private void maskedTextBox1_MaskInputRejected_1(object sender, MaskInputRejectedEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
}
private void dataGridView1_KeyPress_1(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar <= 47 || e.KeyChar >= 58) && e.KeyChar != 8)
e.Handled = true;
}
private void button1_Click(object sender, EventArgs e)
{
double eps = 0.001;
dataGridView1.Enabled = false;
button1.Enabled = false;
maskedTextBox1.Enabled = false;
maskedTextBox2.Enabled = false;
array_cell = new int[k, l];
for (int i = 0; i < k; i++)
for (int j = 0; j < l; j++)
{
array_cell[i, j] = Convert.ToInt32(dataGridView1.Rows[i].Cells[j].Value);
}
int m = array_cell.GetLength(0);
int n = array_cell.GetLength(1);
List<double> B = new List<double>();
List<double> cB = new List<double>();
List<double> A = new List<double>();
List<double> cA = new List<double>();
for (int i = 0; i < n; i++) { B.Add(0.0); cB.Add(0.0); }
for (int j = 0; j < m; j++) { A.Add(0.0); cA.Add(0.0); }
Random rand = new Random();
int f = rand.Next(m);
int min = 0;
double Vn = 0.0, Vv = 0.0, Vs = 0.0, Vold = 0.0;
int count = 0;
dataGridView2.ColumnCount = 2+k+l+4;
dataGridView2.Columns[0].Name = "K"; dataGridView2.Columns[0].Width = 70;
dataGridView2.Columns[1].Name = "i"; dataGridView2.Columns[1].Width = 70;
int inc = 0, inc1 = 0;
for (int i=2;i< k+2;i++)
{
inc1 = inc1 + 1;
dataGridView2.Columns[i].Name = "B"+inc1.ToString(); dataGridView2.Columns[i].Width = 70;
inc = i + 1;
}
inc1 = 0;
dataGridView2.Columns[inc].Name = "j";
for (int j = inc+1; j < l+k+3; j++)
{
inc1 = inc1 + 1;
dataGridView2.Columns[j].Name = "A" + inc1.ToString(); dataGridView2.Columns[j].Width = 70;
inc = inc + 1 ;
}
inc = inc + 1;
dataGridView2.Columns[inc].Name = "Vn";
inc = inc + 1;
dataGridView2.Columns[inc].Name = "Vv";
inc = inc + 1;
dataGridView2.Columns[inc].Name = "Vs";
while (true)
{
count++;
dataGridView2.Rows.Add();
cA[f]++;
for (int i = 0; i < B.Count; i++) B[i] += array_cell[f, i];
min = B.IndexOf(B.Min());
cB[min]++;
for (int j = 0; j < A.Count; j++) A[j] += array_cell[j, min];
dataGridView1.Rows[count].Cells[0].Value = count.ToString();
dataGridView1.Rows[count].Cells[1].Value = (f + 1).ToString();
inc1 = 0;
for (int i = 2; i < k + 2; i++)
{
dataGridView1.Rows[count].Cells[i].Value = Math.Round(B[inc1], 3).ToString();
inc1 = inc1 + 1;
}
dataGridView1.Rows[count].Cells[inc1].Value = min + 1.ToString();
inc = 0;
for (int j = inc1 + 1; j < l + k + 3; j++)
{
dataGridView1.Rows[count].Cells[j].Value = Math.Round(A[inc], 3).ToString();
inc1 = inc1 + 1;
inc = inc + 1;
}
dataGridView1.Rows[count].Cells[inc1].Value = Math.Round(Vn, 3).ToString();
inc1 = inc1 + 1;
dataGridView1.Rows[count].Cells[inc1].Value = Math.Round(Vv, 3).ToString();
inc1 = inc1 + 1;
dataGridView1.Rows[count].Cells[inc1].Value = Math.Round(Vs, 3).ToString();
Vn = B.Min() / count;
Vv = A.Max() / count;
Vs = (Vv + Vn) / 2;
if ((Math.Abs(Vs - Vv)) < eps) break;
Vold = Vs;
f = A.IndexOf(A.Max());
}
listBox1.Items.Add("V*= " + Math.Round(Vs, 3).ToString());
double[] p = new double[A.Count];
for (int i = 0; i < p.Length; i++) p[i] = cA[i] / count;
string str,str1;
str= ("p*= ( ");
for (int i = 0; i < p.Length; i++) str=str+(Math.Round(p[i], 3) + "; "+")");
listBox1.Items.Add(str);
double[] q = new double[B.Count];
for (int i = 0; i < q.Length; i++) q[i] = cB[i] / count;
str1=("q*= ( ");
for (int i = 0; i < q.Length; i++)
str1=str1+(Math.Round(q[i], 3) + "; ");
listBox1.Items.Add(str1);
}
}
} |
|
Ошибку показывает в этой строке
C# | 1
| dataGridView1.Rows[count].Cells[i].Value = Math.Round(B[inc1], 3).ToString(); |
|
0
|