private void button4_Click_1(object sender, EventArgs e) { string rname = ""; string lname = ""; string oper = ""; string resultname = ""; if (!CheckLeft()) { return; } rname = ComboRightMatrix.SelectedItem.ToString().Split(' ')[0]; lname = ComboLeftMatrix.SelectedItem.ToString().Split(' ')[0]; oper = ComboOperation.SelectedItem.ToString(); resultname = EditOperNewMatrix.Text; OperProgress prbar = new OperProgress(); if (matrix_collection.Count == 0) { MessageBox.Show("Ни одной матрицы не создано", "Ошибка"); return; } if (EditOperNewMatrix.Text == "" || EditOperNewMatrix.Text == null) { MessageBox.Show("Не задано имя результирующей матрицы"); return; } if (EditOperNewMatrix.Text.Contains(' ')) { MessageBox.Show("Название матрицы не должно содержать пробелов", "Ошибка"); return; } foreach (var el in matrix_collection) { if (el.name == EditOperNewMatrix.Text) { MessageBox.Show("Матрица с таким названием уже существует", "Ошибка"); return; } } Matrix left = null, right = null, result = null; foreach (var matrix in matrix_collection) { if (matrix.name == lname) { left = matrix; } } switch (oper) { case "*": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } ReadLeft(left); ReadRight(right); result = new Matrix(right.sizex, left.sizey, resultname); prbar.Text = "Умножение, " + left.name + " * " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizey; prbar.Show(); Parms par = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.MultMatrix)); threads.Last().Start(par); break; case "+": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizex || left.sizey != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } ReadLeft(left); ReadRight(right); result = new Matrix(right.sizex, left.sizey, resultname); prbar.Text = "Сложение матриц, " + left.name + " + " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizex; prbar.Show(); Parms par2 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.AddMatrix)); threads.Last().Start(par2); break; case "-": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizex || left.sizey != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } ReadLeft(left); ReadRight(right); result = new Matrix(right.sizex, left.sizey, resultname); prbar.Text = "Вычитание матриц, " + left.name + " - " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizex; prbar.Show(); Parms par3 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.SubMatrix)); threads.Last().Start(par3); break; case "нахождение обратной": if (left.sizex != left.sizey) { MessageBox.Show("Обратную матрицу возможно найти только для квадратной"); return; } left.count_readers++; ReadLeft(left); result = new Matrix(left.sizex, left.sizey, resultname); prbar.Text = "Обратная матрица, от " + left.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizey * result.sizex; prbar.Show(); Parms par4 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.ReverseMatrix)); threads.Last().Start(par4); while (par4.isDetermNull == 2) { Thread.Sleep(100); } if (par4.isDetermNull == 1) { //MessageBox.Show("Определитель матрицы равен нулю, невозможно найти обратную", "Ошибка"); return; } break; } matrix_collection.Add(result); names_collection.Add(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)"); ComboRefresh(); }
public static void MultMatrix(Object para) { Parms par = (Parms)para; Matrix mat1 = par.left, mat2 = par.right, result = par.result; OperProgress prbar = par.prbar; int count = 0; while (!prbar.IsHandleCreated) { Thread.Sleep(10); } for (int i = 0; i < result.sizey; ++i) { if (!prbar.IsDisposed) { prbar.InvokeUI(() => { prbar.progressBar1.Value = count; }); } count++; for (int j = 0; j < result.sizex; ++j) { int sum = 0; Link temp = mat2.columns[j].next; Link current_pointer = temp; while (temp != null) { sum += mat2.Exist(j, temp.line_number, ref current_pointer) * mat1.Exist(temp.line_number, i); //sum += mat2.Exist(j, temp.line_number) * mat1.Exist(temp.line_number, i); temp = temp.next; } if (sum != 0) { result.columns[j].Insert(i, sum); } } } Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")"; if (mat1.name == mat2.name) { mat1.count_readers--; } else { mat1.count_readers--; mat2.count_readers--; } if (mat1.count_readers == 0) { if (mat1.name != result.name)//для сброса наименования и возможности дальнейшей работы с матрицами { try { Fparent.names_collection[Fparent.names_collection.IndexOf(mat1.name + " (" + mat1.sizey.ToString() + "x" + mat1.sizex.ToString() + ") (чтение)")] = mat1.name + " (" + mat1.sizey.ToString() + "x" + mat1.sizex.ToString() + ")"; } catch { Fparent.names_collection[Fparent.names_collection.IndexOf(mat1.name + " (" + mat1.sizey.ToString() + "x" + mat1.sizex.ToString() + ") (запись)")] = mat1.name + " (" + mat1.sizey.ToString() + "x" + mat1.sizex.ToString() + ")"; } } } if (mat2.count_readers == 0) { if (mat2.name != result.name && mat2.name != mat1.name) { try { Fparent.names_collection[Fparent.names_collection.IndexOf(mat2.name + " (" + mat2.sizey.ToString() + "x" + mat2.sizex.ToString() + ") (чтение)")] = mat2.name + " (" + mat2.sizey.ToString() + "x" + mat2.sizex.ToString() + ")"; } catch { Fparent.names_collection[Fparent.names_collection.IndexOf(mat2.name + " (" + mat2.sizey.ToString() + "x" + mat2.sizex.ToString() + ") (запись)")] = mat2.name + " (" + mat2.sizey.ToString() + "x" + mat2.sizex.ToString() + ")"; } } } Program.fr2.InvokeUI(() => { Program.fr2.ComboRefresh(); }); if (MessageBox.Show("Матрица \"" + result.name + "\" успешно добавлена, вывести результат?", "Успех", MessageBoxButtons.YesNo) == DialogResult.Yes) { Program.fr2.InvokeUI(() => { prbar.progressBar1.Maximum = result.sizex * result.sizey; }); Matrix.Outparams forout = new Matrix.Outparams(new ForOutputMatrix(result.name, "matrix"), prbar); // result.count_readers++; Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (чтение)"; Program.fr2.InvokeUI(() => { Program.fr2.ComboRefresh(); }); result.count_readers++; Fparent.threads.Add(new Thread(result.OutputMatrix)); Fparent.threads.Last().Start(forout); } else if (!prbar.IsDisposed) { prbar.InvokeUI(() => { prbar.Close(); }); } }
private void button4_Click(object sender, EventArgs e) { if (!CheckLeft()) { return; } if (!CheckResult()) { return; } string rname = ""; string lname = ""; string oper = ""; string resultname = ""; rname = ComboRightMatrix.SelectedItem.ToString().Split(' ')[0]; lname = ComboLeftMatrix.SelectedItem.ToString().Split(' ')[0]; oper = ComboOperation.SelectedItem.ToString(); resultname = ComboResult.SelectedItem.ToString().Split(' ')[0]; OperProgress prbar = new OperProgress(); if (matrix_collection.Count == 0) { MessageBox.Show("Ни одной матрицы не создано", "Ошибка"); return; } if (MessageBox.Show("Перезаписать матрицу \"" + resultname + "\"?", "Предупреждение", MessageBoxButtons.YesNo) == DialogResult.No) { return; } Matrix left = null, right = null, result = null; foreach (var matrix in matrix_collection) { if (matrix.name == lname) { left = matrix; } } switch (oper) { case "*": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } result = new Matrix(right.sizex, left.sizey, resultname); WriteResult(result); ReadLeft(left); ReadRight(right); prbar.Text = "Умножение, " + left.name + " * " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizey; prbar.Show(); Parms par = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.MultMatrix)); threads.Last().Start(par); break; case "+": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizex || left.sizey != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } result = new Matrix(right.sizex, left.sizey, resultname); WriteResult(result); ReadLeft(left); ReadRight(right); prbar.Text = "Сложение матриц, " + left.name + " + " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizex; prbar.Show(); Parms par2 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.AddMatrix)); threads.Last().Start(par2); break; case "-": if (!CheckRight()) { return; } foreach (var matrix in matrix_collection) { if (matrix.name == rname) { right = matrix; } } if (left.sizex != right.sizex || left.sizey != right.sizey) { MessageBox.Show("Неверные размерности входных матриц", "Ошибка"); return; } if (left.name == right.name) { left.count_readers++; } else { left.count_readers++; right.count_readers++; } result = new Matrix(right.sizex, left.sizey, resultname); WriteResult(result); ReadLeft(left); ReadRight(right); prbar.Text = "Вычитание матриц, " + left.name + " - " + right.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizex; prbar.Show(); Parms par3 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.SubMatrix)); threads.Last().Start(par3); break; case "нахождение обратной": if (left.sizex != left.sizey) { MessageBox.Show("Обратную матрицу возможно найти только для квадратной"); return; } left.count_readers++; result = new Matrix(left.sizex, left.sizey, resultname); WriteResult(result); ReadLeft(left); prbar.Text = "Обратная матрица, от " + left.name + " = " + result.name; prbar.progressBar1.Maximum = result.sizey * result.sizex; prbar.Show(); Parms par4 = new Parms(left, right, result, prbar); threads.Add(new Thread(Matrix.ReverseMatrix)); threads.Last().Start(par4); while (par4.isDetermNull == 2) { Thread.Sleep(100); } if (par4.isDetermNull == 1) { //MessageBox.Show("Определитель матрицы равен нулю, невозможно найти обратную", "Ошибка"); return; } break; } string combo_line = resultname; Matrix temp = null; foreach (var el in matrix_collection) { if (el.name == combo_line) { temp = el; break; } } matrix_collection.Remove(temp); matrix_collection.Add(result); ComboRefresh(); }
public static void ReverseMatrix(Object para) { Parms par = (Parms)para; Matrix mat = par.left, result = par.result; OperProgress prbar = par.prbar; bool[] cols = new bool[mat.sizex]; bool[] rows = new bool[mat.sizex]; for (int i = 0; i < cols.Length; ++i) { cols[i] = rows[i] = true; } int det = mat.DetMatrix(rows, cols, mat.sizex); if (det == 0) { try { Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")"; } catch { Thread.Sleep(200); try { Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")"; } catch { } } --mat.count_readers; if (mat.count_readers == 0) { if (mat.name != result.name) { try { Fparent.names_collection[Fparent.names_collection.IndexOf(mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ") (чтение)")] = mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ")"; } catch { Fparent.names_collection[Fparent.names_collection.IndexOf(mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ") (запись)")] = mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ")"; } } } Program.fr2.InvokeUI(() => { Program.fr2.ComboRefresh(); }); if (!prbar.IsDisposed) { prbar.InvokeUI(() => { prbar.Close(); }); } par.isDetermNull = 1; MessageBox.Show("Определитель матрицы равен нулю, невозможно найти обратную", "Ошибка"); return; } else { par.isDetermNull = 0; } int k = 0; int count = 0; while (!prbar.IsHandleCreated) { Thread.Sleep(10); } for (int i = 0; i < result.sizey; ++i) { for (int j = 0; j < result.sizex; ++j) { if (!prbar.IsDisposed) { prbar.InvokeUI(() => { prbar.progressBar1.Value = count; }); } count++; result.columns[i].Insert(j, Convert.ToInt32((mat.Minor(i, j, (bool[])rows.Clone(), (bool[])cols.Clone(), mat.sizex)) / det)); Console.WriteLine(++k); } } try { Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")"; } catch { Thread.Sleep(200); Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (запись)")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")"; } --mat.count_readers; if (mat.count_readers == 0) { if (mat.name != result.name) { try { Fparent.names_collection[Fparent.names_collection.IndexOf(mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ") (чтение)")] = mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ")"; } catch { Fparent.names_collection[Fparent.names_collection.IndexOf(mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ") (запись)")] = mat.name + " (" + mat.sizey.ToString() + "x" + mat.sizex.ToString() + ")"; } } } Program.fr2.InvokeUI(() => { Program.fr2.ComboRefresh(); }); if (MessageBox.Show("Матрица \"" + result.name + "\" успешно добавлена, вывести результат?", "Успех", MessageBoxButtons.YesNo) == DialogResult.Yes) { Program.fr2.InvokeUI(() => { prbar.progressBar1.Maximum = result.sizex * result.sizey; }); Matrix.Outparams forout = new Matrix.Outparams(new ForOutputMatrix(result.name, "matrix"), prbar); // result.count_readers++; Fparent.names_collection[Fparent.names_collection.IndexOf(result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ")")] = result.name + " (" + result.sizey.ToString() + "x" + result.sizex.ToString() + ") (чтение)"; Program.fr2.InvokeUI(() => { Program.fr2.ComboRefresh(); }); result.count_readers++; Fparent.threads.Add(new Thread(result.OutputMatrix)); Fparent.threads.Last().Start(forout); } else if (!prbar.IsDisposed) { prbar.InvokeUI(() => { prbar.Close(); }); } }