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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace Matrix
{
public partial class Form1 : Form
{
public int n;
public int m;
public TextBox[,] a1;
public TextBox[,] a2;
public TextBox[,] a3;
public class matrix
{
public int stroka;
public int stolbets;
public TextBox[,] a1;
public TextBox[,] a2;
public TextBox[,] a3;
public matrix(int stroka, int stolbets)
{
this.stroka = stroka;
this.stolbets = stolbets;
}
public int Stroka
{
get { return stroka; }
set { stroka = value; }
}
public int Stolbets
{
get { return stolbets; }
set { stolbets = value; }
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//створення матриці
{
try
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
if (n >= 5)
{ MessageBox.Show("Перевищили кількість рядків в матриці!", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Error); }
if (m >= 5)
{ MessageBox.Show("Перевищили кількість стовпців в матриці!", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Error); }
else
a1 = new TextBox[n, m];
a2 = new TextBox[n, m];
int k = 30;
int l = 90;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a1[i, j] = new TextBox();
a1[i, j].Width = 25;
a1[i, j].Location = new Point(50 + k, 50 + l);
Controls.Add(a1[i, j]);
k += 30;
}
k = 30;
l += 30;
}
int k1 = 30;
int l1 = 240;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a2[i, j] = new TextBox();
a2[i, j].Width = 25;
a2[i, j].Location = new Point(50 + k1, 50 + l1);
Controls.Add(a2[i, j]);
k1 += 30;
}
k1 = 30;
l1 += 30;
}
groupBox1.Enabled = true;
fileToolStripMenuItem.Enabled = true;
newMatrixToolStripMenuItem.Enabled = true;
button1.Enabled = false;
button2.Enabled = true;
textBox1.Enabled = false;
textBox2.Enabled = false;
}
catch { MessageBox.Show("Введіть числа в діапазоні від 1 до 4", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
}
private void button2_Click(object sender, EventArgs e)//Random
{
Random x = new Random();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a1[i, j].Text = Convert.ToString(x.Next(-49, 499));
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a2[i, j].Text = Convert.ToString(x.Next(-49, 499));
}
}
button2.Enabled = false;
}
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i >= 100)
{ MessageBox.Show("Уводьте тільки числа!", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Error); }
if (j >= 100)
{ MessageBox.Show("Уводьте тільки числа!", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
}
if (radioButton1.Checked == true)//Додавання матриць
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
a3 = new TextBox[n, m];
int k2 = 200;
int l2 = 240;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a3[i, j] = new TextBox();
a3[i, j].Width = 25;
a3[i, j].Location = new Point(50 + k2, 50 + l2);
a3[i, j].Text = Convert.ToString(Convert.ToInt32(a1[i, j].Text) + Convert.ToInt32(a2[i, j].Text));
Controls.Add(a3[i, j]);
k2 += 30;
}
k2 = 200;
l2 += 30;
}
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
if (radioButton2.Checked == true)//віднімання матриць
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
a3 = new TextBox[n, m];
int k2 = 200;
int l2 = 240;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a3[i, j] = new TextBox();
a3[i, j].Width = 25;
a3[i, j].Location = new Point(50 + k2, 50 + l2);
a3[i, j].Text = Convert.ToString(Convert.ToInt32(a1[i, j].Text) - Convert.ToInt32(a2[i, j].Text));
Controls.Add(a3[i, j]);
k2 += 30;
}
k2 = 200;
l2 += 30;
}
radioButton1.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
if (radioButton3.Checked == true)//Помноження матриць
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
a3 = new TextBox[n, m];
int k2 = 200;
int l2 = 240;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a3[i, j] = new TextBox();
a3[i, j].Width = 25;
a3[i, j].Location = new Point(50 + k2, 50 + l2);
a3[i, j].Text += Convert.ToString(Convert.ToInt32(a1[j, i].Text) * Convert.ToInt32(a2[i, j].Text));
Controls.Add(a3[i, j]);
k2 += 30;
}
k2 = 200;
l2 += 30;
}
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton4.Enabled = false;
}
if (radioButton4.Checked)//транспонування матриць
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
a3 = new TextBox[n, m];
int k2 = 200;
int l2 = 240;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
a3[i, j] = new TextBox();
a3[i, j].Width = 25;
a3[i, j].Location = new Point(50 + k2, 50 + l2);
a3[i, j].Text = Convert.ToString(Convert.ToInt32(a1[j, i].Text));
Controls.Add(a3[i, j]);
k2 += 30;
}
k2 = 200;
l2 += 30;
}
radioButton1.Enabled = false;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
}
if (radioButton5.Checked == true)//обчислення детермінанта
{
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
int d = 1;
int t = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (i == j) d *= Convert.ToInt32(a1[i, j].Text);
if (i == j) t *= Convert.ToInt32(a2[i, j].Text);
}
}
MessageBox.Show(string.Format("Детермінант 1 матриці = " + d.ToString()), "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show(string.Format("Детермінант 2 матриці = " + t.ToString()), "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (radioButton7.Checked == true)//пошук елемента матриці
{
int z;
int w;
n = Convert.ToInt32(textBox1.Text);
m = Convert.ToInt32(textBox2.Text);
z = Convert.ToInt32(numericUpDown1.Text);
w = Convert.ToInt32(numericUpDown2.Text);
int p = -1;
int r = -1;
z = z - 1;
w = w - 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (z == i & w == j)
p = Convert.ToInt32(a1[i, j].Text);
if (z == i & w == j)
r = Convert.ToInt32(a2[i, j].Text);
}
}
if (p == -1) MessageBox.Show("Елемент з таким індексом не існує", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Error);
else MessageBox.Show(string.Format("Елемент 1 матриці = " + p.ToString()), "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (r != -1) MessageBox.Show(string.Format("Елемент 2 матриці = " + r.ToString()), "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
button2.Enabled = false;
saveToolStripMenuItem.Enabled = true;
}
catch { MessageBox.Show("Вводьте тільки числа!", "Увага", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)//вихід
{
Application.Exit();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)//відкрити файл
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
StreamReader sr = new StreamReader(ofd.FileName);
for (int i = 0; i < a3.GetLength(0); i++)
{
for (int j = 0; j < a3.GetLength(1); j++)
{
a3[i, j].Text = sr.ReadLine();
}
}
sr.Close();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)//зберегти файл
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "txt files (*.txt)|*.txt";
sfd.ShowDialog();
StreamWriter sw = new StreamWriter(sfd.FileName);
for (int i = 0; i < a3.GetLength(0); i++)
{
for (int j = 0; j < a3.GetLength(1); j++)
{
sw.WriteLine(a3[i, j].Text + " ");
}
}
sw.Close();
openToolStripMenuItem.Enabled = true;
}
private void helpToolStripMenuItem_Click_1(object sender, EventArgs e)//вивати справку справки
{
try
{
Process SysInfo = new Process();
SysInfo.StartInfo.ErrorDialog = true;
SysInfo.StartInfo.FileName = "help.docx";
SysInfo.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void aboutToolStripMenuItem_Click_1(object sender, EventArgs e)//Інформація про виробника
{
try
{
Process SysInfo = new Process();
SysInfo.StartInfo.ErrorDialog = true;
SysInfo.StartInfo.FileName = "about_company.hta";
SysInfo.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void newMatrixToolStripMenuItem_Click(object sender, EventArgs e)//очистити
{
Application.Restart();
}
}
} |