Vous êtes sur la page 1sur 13

OPERACIONES CON MATRICES

Jos David Uc Salas

Definicin de una Matriz


Un conjunto de elementos (nmeros) ordenados en filas y columnas.

Si el nmero de filas es igual al nmero de columnas => matriz cuadrada.

Operaciones con matrices

cij=aijbij

Operaciones con matrices

cij=ai0*b0j + ai1*b1j + + ain-1*bn-1j

Crear un nuevo proyecto

Disear el Formulario

Cambiar sus dimensiones


Size tendr un dimensin de: 878; 277 Agregar los siguientes controles

1 MenuStrip 3 DataGridView 1 Label 1 Button

Agregar la clase InputBox


public static string Show(string title, string prompt, FormStartPosition posicion) using System.Windows.Forms; { using System; f = new Form(); public static class Inputbox f.Text = title; { f.ShowIcon = false; static Form f; f.Icon = null; static Label l; f.KeyPreview = true; static TextBox t; // Elementos necesarios f.ShowInTaskbar = false; static Button b1; f.MaximizeBox = false; static Button b2; f.MinimizeBox = false; static string Valor; f.Width = 200; /// <summary> f.FormBorderStyle = FormBorderStyle.FixedDialog; /// Objeto Esttico que muestra un pequeo dilogo para introducir datos f.Height = 120; /// </summary> f.StartPosition = posicion; /// <param name="title">Ttulo del dilogo</param> f.KeyPress += new KeyPressEventHandler(f_KeyPress); /// <param name="prompt">Texto de informacin</param> l = new Label(); /// <param name="posicion">Posicin de inicio</param> l.AutoSize = true; /// <returns>Devuelve la escrito en la caja de texto como string</returns> l.Text = prompt; t = new TextBox(); t.Width = 182; t.Top = 40; b1 = new Button(); b1.Text = "Aceptar"; b1.Click += new EventHandler(b1_Click);

Agregar la clase InputBox


b2 = new Button(); b2.Text = "Cancelar"; b2.Click += new EventHandler(b2_Click); f.Controls.Add(l); f.Controls.Add(t); f.Controls.Add(b1); f.Controls.Add(b2); l.Top = 10; t.Left = 5; t.Top = 30; b1.Left = 5; b1.Top = 60; b2.Left = 112; b2.Top = 60; f.ShowDialog(); return (Valor); } static void f_KeyPress(object sender, KeyPressEventArgs e) { switch (Convert.ToChar(e.KeyChar)) { case ('\r'): Acepta(); break; ; case ('_'): Cancela(); break; ; } } static void b2_Click(object sender, EventArgs e) { Cancela(); } static void b1_Click(object sender, EventArgs e) { Acepta(); } private static string Val { get { return (Valor); } set { Valor = value; } } private static void Acepta() { Val = t.Text; f.Dispose(); } private static void Cancela() { Val = null; f.Dispose(); } }

Agregar cdigo al men Sumar


private void sumaToolStripMenuItem_Click(object sender, EventArgs e) { string strSize; strSize = Inputbox.Show("Matrices en C#", "Tamao de la Matriz: ", FormStartPosition.CenterScreen); MessageBox.Show(strSize); dataGridView1.Columns.Clear(); dataGridView2.Columns.Clear(); dataGridView3.Columns.Clear(); lbpOperacion.Text = "+"; tam = int.Parse(strSize); // tam se declara dentro de la clase de Formulario y sirve para el Tamao de las matrices int i = 0; while (i < tam) { DataGridViewColumn columna = new DataGridViewColumn(new DataGridViewTextBoxCell()); columna.Name = i.ToString(); columna.HeaderText = i.ToString(); columna.Width = 25; this.dataGridView1.Columns.Add(columna);

Agregar cdigo al men Sumar


DataGridViewColumn columna2 = new DataGridViewColumn(new DataGridViewTextBoxCell()); columna2.Name = i.ToString(); columna2.HeaderText = i.ToString(); columna2.Width = 25; this.dataGridView2.Columns.Add(columna2); DataGridViewColumn columna3 = new DataGridViewColumn(new DataGridViewTextBoxCell()); columna3.Name = i.ToString(); columna3.HeaderText = i.ToString(); columna3.Width = 25; this.dataGridView3.Columns.Add(columna3); i++; } dataGridView1.Rows.Add(tam); dataGridView2.Rows.Add(tam); dataGridView3.Rows.Add(tam); // No permite que el usuario agregue nuevas filas dataGridView1.AllowUserToAddRows = false; dataGridView2.AllowUserToAddRows = false; dataGridView3.AllowUserToAddRows = false; }

Agregar cdigo al botn =


private void cmdIgual_Click(object sender, EventArgs e) { int[,] matriz1 = new int[tam, tam]; int[,] matriz2 = new int[tam, tam]; int[,] matriz3 = new int[tam, tam]; for (int f = 0; f < tam; f++) { for (int c = 0; c < tam; c++) { matriz1[f, c] = int.Parse(dataGridView1[f, c].Value.ToString()); matriz2[f, c] = int.Parse(dataGridView2[f, c].Value.ToString()); matriz3[f, c] = _matriz1[f, c] + matriz2[f, c]; dataGridView3.CurrentCell = dataGridView3[f, c]; dataGridView3.CurrentCell.Value = matriz3[f, c]; } } }

Ejercicio clase
Concluir con el desarrollo de las operaciones Restar y Multiplicar.

Ejercicio Tarea
Investigar como realizar las siguientes operaciones con matrices:
INVERSA DE UNA MATRZ TRANSPUESTA DE UNA MATRZ DIVISIN DE MATRICES

Vous aimerez peut-être aussi