private void btnSubtrair_Click(object sender, EventArgs e) { if (Matriz1 == null || Matriz2 == null) { MessageBox.Show("Matriz nula!", "Error - Matriz"); return; } float[,] tempMatriz1 = new float[Matriz1.GetLength(0), Matriz1.GetLength(1)]; float[,] tempMatriz2 = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; if (tempMatriz1.GetLength(0) != tempMatriz2.GetLength(0) || tempMatriz1.GetLength(1) != tempMatriz2.GetLength(1)) { MessageBox.Show("Atenção!\n\nSó é possível realizar a subtração de matrizes de mesma ordem!", "Erro - Subtração Entre Matrizes"); return; } for (int x = 0; x < Matriz1.GetLength(0); x++) { for (int y = 0; y < Matriz1.GetLength(1); y++) { float n = 0; float.TryParse(Matriz1[x, y].Text, out n); tempMatriz1[x, y] = n; } } for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempMatriz2[x, y] = n; //tempMatriz2[x, y] = Convert.ToInt32(Matriz2[x, y].Text); } } float[,] tempMatrizResultante = CalculosMatrizes.SubtrairMatrizes(tempMatriz1, tempMatriz2); MatrizResultante = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; int TamanhoText = groupBoxMatrizResultante.Width / MatrizResultante.GetLength(1); groupBoxMatrizResultante.Controls.Clear(); for (int x = 0; x < MatrizResultante.GetLength(0); x++) { for (int y = 0; y < MatrizResultante.GetLength(1); y++) { MatrizResultante[x, y] = new TextBox(); MatrizResultante[x, y].Text = tempMatrizResultante[x, y].ToString(); MatrizResultante[x, y].Top = (x * MatrizResultante[x, y].Height) + 20; MatrizResultante[x, y].Left = y * TamanhoText + 1; MatrizResultante[x, y].Width = TamanhoText; groupBoxMatrizResultante.Controls.Add(MatrizResultante[x, y]); } } }
private void MatrizInversa2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula!", "Error - Matriz"); return; } if (Matriz2.GetLength(0) != Matriz2.GetLength(1)) { MessageBox.Show("Atenção!\n\nSó é possível calcular o determinante de matrizes quadradas", "Error - Matriz Inversa"); return; } float[,] tempMatriz2 = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempMatriz2[x, y] = n; } } if ((CalculosMatrizes.Determinante(tempMatriz2)) == 0) { MessageBox.Show("Atenção!\n\nSó é possível calcular a inversa de matrizes\ncujo o determinante é diferente de zero.", "Error - Matriz Inversa"); return; } float[,] tempMatrizResultante = CalculosMatrizes.Inversa(tempMatriz2); int TamanhoText = groupBoxMatriz2.Width / Matriz2.GetLength(0); Matriz2 = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; groupBoxMatriz2.Controls.Clear(); for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { Matriz2[x, y] = new TextBox(); Matriz2[x, y].Text = tempMatrizResultante[x, y].ToString(); Matriz2[x, y].Top = (x * Matriz2[x, y].Height) + 20; Matriz2[x, y].Left = y * TamanhoText + 1; Matriz2[x, y].Width = TamanhoText; groupBoxMatriz2.Controls.Add(Matriz2[x, y]); } } }
private void bntPotenciaMatriz2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula!", "Error - Matriz"); return; } if (Matriz2.GetLength(0) != Matriz2.GetLength(1)) { MessageBox.Show("Atenção!\n\nSó é possível realizar potência de matrizes quadradas", "Error - Potência da Matriz"); return; } float[,] tempMatriz2 = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempMatriz2[x, y] = n; } } int m = 0; int.TryParse(textBoxPotenciaMatriz2.Text, out m); int potencia = m; float[,] tempMatrizResultante = CalculosMatrizes.PotenciaMatrizes(tempMatriz2, potencia); int TamanhoText = groupBoxMatriz2.Width / Matriz2.GetLength(0); Matriz2 = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; groupBoxMatriz2.Controls.Clear(); for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { Matriz2[x, y] = new TextBox(); Matriz2[x, y].Text = tempMatrizResultante[x, y].ToString(); Matriz2[x, y].Top = (x * Matriz2[x, y].Height) + 20; Matriz2[x, y].Left = y * TamanhoText + 1; Matriz2[x, y].Width = TamanhoText; groupBoxMatriz2.Controls.Add(Matriz2[x, y]); } } }
private void btnMultiplicarEscalarMatriz2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula !", "Error - Matriz"); return; } float[,] tempMatriz2 = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float m = 0; float.TryParse(Matriz2[x, y].Text, out m); tempMatriz2[x, y] = m; } } float n = 0; float.TryParse(textBoxEscalarMatriz2.Text, out n); float constante = n; float[,] tempMatrizResultante = CalculosMatrizes.MultiplicarMatrizPorEscalar(tempMatriz2, constante); Matriz2 = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; int TamanhoText = groupBoxMatriz2.Width / Matriz2.GetLength(1); groupBoxMatriz2.Controls.Clear(); for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { Matriz2[x, y] = new TextBox(); Matriz2[x, y].Text = tempMatrizResultante[x, y].ToString(); Matriz2[x, y].Top = (x * Matriz2[x, y].Height) + 20; Matriz2[x, y].Left = y * TamanhoText + 1; Matriz2[x, y].Width = TamanhoText; groupBoxMatriz2.Controls.Add(Matriz2[x, y]); } } }
private void DeterminanteMatriz2_Click(object sender, EventArgs e) { if (Matriz2 == null) { MessageBox.Show("Matriz nula!", "Error - Matriz"); return; } if (Matriz2.GetLength(0) != Matriz2.GetLength(1)) { MessageBox.Show("Atenção!\n\nSó é possível calcular o determinante de matrizes quadradas", "Error - Determinante da Matriz"); return; } float[,] tempMatriz2 = new float[Matriz2.GetLength(0), Matriz2.GetLength(1)]; for (int x = 0; x < Matriz2.GetLength(0); x++) { for (int y = 0; y < Matriz2.GetLength(1); y++) { float n = 0; float.TryParse(Matriz2[x, y].Text, out n); tempMatriz2[x, y] = n; } } float determinante = -1111111; if (tempMatriz2.GetLength(0) == 1) { determinante = tempMatriz2[0, 0]; } else { determinante = CalculosMatrizes.Determinante(tempMatriz2); } Determinante2.Text = "det = " + determinante.ToString(); }
private void btnTranspostaMatriz1_Click(object sender, EventArgs e) { if (Matriz1 == null) { MessageBox.Show("Matriz nula !", "Error - Matriz"); return; } float[,] tempResultante = new float[Matriz1.GetLength(0), Matriz1.GetLength(1)]; for (int x = 0; x < Matriz1.GetLength(0); x++) { for (int y = 0; y < Matriz1.GetLength(1); y++) { float n = 0; float.TryParse(Matriz1[x, y].Text, out n); tempResultante[x, y] = n; } } float[,] tempMatrizResultante = CalculosMatrizes.GerarTransposta(tempResultante); int TamanhoText = groupBoxMatriz1.Width / Matriz1.GetLength(0); Matriz1 = new TextBox[tempMatrizResultante.GetLength(0), tempMatrizResultante.GetLength(1)]; groupBoxMatriz1.Controls.Clear(); for (int x = 0; x < Matriz1.GetLength(0); x++) { for (int y = 0; y < Matriz1.GetLength(1); y++) { Matriz1[x, y] = new TextBox(); Matriz1[x, y].Text = tempMatrizResultante[x, y].ToString(); Matriz1[x, y].Top = (x * Matriz1[x, y].Height) + 20; Matriz1[x, y].Left = y * TamanhoText + 1; Matriz1[x, y].Width = TamanhoText; groupBoxMatriz1.Controls.Add(Matriz1[x, y]); } } }