jueves, 13 de junio de 2013

CLASE MATRIZ

En esta aplicación visual se muestra una ventana en donde dimensionas el numero de renglones y de columnas en un datagridview con un NumericUpDown,  en el costado izquierdo hay 3 radiobuttons en donde le daremos vlores aleatorios o insertar manualmente, el objetivo de esta aplicación es encontrar los números nones y pares asi como los promedios de las diagonales principal y las diagonales invertidas.

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;//

namespace clase_matriz
{
    class Matriz
    {


        //ATRIBUTOS
        int[,] mat;


        //CONSTRUCTORES
        public Matriz()
        {
        }
       
       

        public Matriz(int numRen,int numCol)
        {
            mat = new int[numRen,numCol];
        }


        //CONSTRUCTOR QUE RECIBE UN DGV
        public Matriz(DataGridView dgv1)
        {
            mat = new int[dgv1.RowCount, dgv1.ColumnCount];

            for (int ren = 0; ren < dgv1.ColumnCount; ren++)
                for (int col = 0; col < dgv1.RowCount; col++)
                    mat[ren, col] = Convert.ToInt32(dgv1.Rows[ren].Cells[col].Value.ToString());
        }

        //PROPIEDADES
        public int Ren
        {
            get { return mat.GetLength(0); }
        }

        public int Col
        {
            get { return mat.GetLength(1); }
        }

        //INDIZADOR
        public int this[int ren, int col]
        {
            //if( ren == col )
            //{

            set { mat[ren, col] = value; }
            get { return mat[ren, col]; }
            //}
        }

        //numeros nones
        public static bool Es_Non(int n)
        {
            if (n % 2 != 0)
                return true;
            return false;
        }
        //METODOS
       // METODO QUE SACA LOS NUMEROS NONES DE UNA MATRIZ
        public string[] Nones(ref int num_non)
        {
            int n = 0;
            for (int ren = 0; ren < mat.GetLength(0); ren++)
                for (int col = 0; col < mat.GetLength(1); col++)
                    if (Es_Non(mat[ren, col]))
                        num_non++;

            string[] dat_non = new string[num_non];

            for (int i = 0; i < mat.GetLength(0); i++)
                for (int j = 0; j < mat.GetLength(1); j++)
                    if (Es_Non(mat[i, j]))
                    {
                        dat_non[n++] = mat[i, j].ToString();
                    }
            return dat_non;
        }

        //numeros pares
        public static bool Es_Par(int n)
        {
            if (n % 2 == 0)
                    return true;
            return false;
        }
        //METODO QUE SACA LOS NUMEROS PRIMOS DE UNA MATRIZ
        public string[] Pares(ref int num_par)
        {
            int n = 0;
            for (int i = 0; i < mat.GetLength(0); i++)
                for (int j = 0; j < mat.GetLength(1); j++)
                    if (Es_Par(mat[i, j]))
                        num_par++;

            string[] dat_prim = new string[num_par];

            for (int i = 0; i < mat.GetLength(0); i++)
                for (int j = 0; j < mat.GetLength(1); j++)
                    if (Es_Par(mat[i, j]))
                    {
                        dat_prim[n++] = mat[i, j].ToString();
                    }
            return dat_prim;
        }

        //METODO QUE SACA EL PROMEDIO DE LA DIAGONAL PRINCIPAL
        public string[] Diag_Prin(ref double prom)
        {
            double sum = 0.0;
            string[] dat_Diag_Princ = new string[mat.GetLength(0)];

            if (mat.GetLength(0) == mat.GetLength(1))
            {
                for (int i = 0; i < mat.GetLength(0); i++)
                {
                    sum += mat[i, i];
                    dat_Diag_Princ[i] = mat[i, i].ToString();
                }
                prom = sum / mat.GetLength(0);
                return dat_Diag_Princ;
            }
            else
            {
                MessageBox.Show("Para poder sacar el promedio de la diagonal principal la matriz debe ser cuadrada");
                dat_Diag_Princ[0] = " La matriz debe ser de igual numero de renglones y columnas ";
                prom = 0;
                return dat_Diag_Princ;
            }
        }

        //METODO QUE SACA EL PROMEDIO DE LA DIAGONAL INVERTIDA
        public string[] Diag_Inv(ref double prom)
        {
            double sum = 0.0;
            int c = mat.GetLength(0) - 1;
            int c2 = mat.GetLength(0) - 1;
            int c3 = mat.GetLength(0) - 1;
            string[] dat_Diag_Inv = new string[mat.GetLength(0)];

            if (mat.GetLength(0) == mat.GetLength(1))
            {
                for (int i = 0; i < mat.GetLength(0); i++)
                {
                    sum += mat[i, c--];
                }

                for (int i = 0; i < mat.GetLength(0); i++)
                {
                    dat_Diag_Inv[i] = mat[i, c2--].ToString();
                }
                prom = sum / mat.GetLength(0);
                return dat_Diag_Inv;
            }
            else
            {
                MessageBox.Show("Para poder sacar el promedio de la diagonal invertida la matriz debe ser cuadrada");
                dat_Diag_Inv[0] = " La matriz debe ser de igual numero de renglones y columnas ";
                prom = 0;
                return dat_Diag_Inv;
            }
        }
    }
}
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;

namespace clase_matriz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Matriz mat = new Matriz();


        //renglones
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            int renglones;
            renglones = Convert.ToInt32(numericUpDown1.Value);

            dataGridView1.RowCount = renglones;
            dgv1.RowCount = renglones;
        }

        //columnas
        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            int columnas;
            columnas = Convert.ToInt32(numericUpDown2.Value);
            dataGridView1.ColumnCount = columnas;
            dgv2.ColumnCount = columnas;
        }

        private void radioAleatorio_CheckedChanged(object sender, EventArgs e)
        {
            Random r = new Random();

            for (int ren = 0; ren < dataGridView1.RowCount; ren++)

                for (int col = 0; col < dataGridView1.ColumnCount; col++)
                    dataGridView1[col, ren].Value = r.Next(1, 100);
        }


        private void radioDefoul_CheckedChanged(object sender, EventArgs e)
        {
            //CREAMOS UN ARREGLO DONDE ESTAN GUARDADOS 100 NUMEROS ENTEROS
            int[,] aux = {{ 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,110}};

            //OBTENEMOS EL VALOR DE LOS numericUpDown
            int nr = Convert.ToInt32(numericUpDown1.Value);
            int nc = int.Parse(numericUpDown2.Value.ToString());

            //LLENAMOS LOS dGVS CON LOS DATOS DEL ARREGLO
            for (int ren = 0; ren < dataGridView1.RowCount; ren++)
                for (int col = 0; col < dataGridView1.ColumnCount; col++)
                {
                    dataGridView1.Rows[ren].Cells[col].Value = int.Parse(aux[ren, col].ToString());
                 
                }
        }


        private void radioCapturar_CheckedChanged(object sender, EventArgs e)
        {
            //LIMPIAMOS LOS dGVS
            for (int ren = 0; ren < dataGridView1.RowCount; ren++)
                for (int col = 0; col < dataGridView1.ColumnCount; col++)
                {
                    dataGridView1.Rows[ren].Cells[col].Value = " ";

                  
                }
            //HACEMOS QUE LOS dGVS NO SEAN DE SOLO LECTURA Y CONCENTRAMOS EL PUNTERO EN EL dataGridView1

            dataGridView1.ReadOnly = false;
            dataGridView1.Focus();
        }

        private void butDiagPrinc_Click(object sender, EventArgs e)
        {
            try
            {
                //CREAMOS UN OBJETO DE LA CLASE MATRIZ POR MEDIO DE SU CONSTRUCTOR QUE RECIBE UN dGV
                Matriz mat = new Matriz(dataGridView1);

                //DECLARAMOS UNA VARIABLE DONDE SE GUARDARA EL PROMEDIO
                double prom = 0;

                //DECLARAMOS UN ARREGLO DONDE SE GUARDARAN LAS POSICIONES DE LOS NUMEROS QUE CONFORMAN LA DIAGINAL PRINCIPAL
                string[] arr_prin = mat.Diag_Prin(ref prom);

                //DAMOS SALIDA A EL PROMEDIO
                textBox1.Text = prom.ToString();

                //DAMOS LA CANTIDAD DE RENGLONES AL dGV QUE MUESTRA LAS POSICIONES DE ACUERDO A LA LONGITUD DEL ARREGLO
                dgvPrincipal.RowCount = arr_prin.Length;


                //LLENAMOS EL dGV DONDE SE MUESTRAN LAS POSICIONES
                for (int i = 0; i < dgv1.RowCount; i++)
                    dgvPrincipal[0, i].Value = arr_prin[i];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butDiagInvert_Click(object sender, EventArgs e)
        {
            try
            {
                Matriz mat = new Matriz(dataGridView1);
                double prom = 0;
                string[] arr_inv = mat.Diag_Inv(ref prom);
                textBox2.Text = prom.ToString();
                dgvInvertida.RowCount = arr_inv.Length;
                textBox2.Visible = true;
                dgvInvertida.Visible = true;

                for (int i = 0; i < dgvInvertida.RowCount; i++)
                    dgvInvertida[0, i].Value = arr_inv[i];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butNones_Click(object sender, EventArgs e)
        {
            Matriz mat = new Matriz(dataGridView1);
            int num_non = 0;
            string[] arr_non = mat.Nones(ref num_non);
            dgv1.RowCount = arr_non.Length;

            for (int ren = 0; ren < dgv1.RowCount; ren++)
                for (int col = 0; col < dgv1.ColumnCount; col++)
                    dgv1.Rows[ren].Cells[col].Value = arr_non[ren];
        }

        private void butPares_Click(object sender, EventArgs e)
        {
            Matriz mat = new Matriz(dataGridView1);
            int num_Par = 0;
            string[] arr_Par = mat.Pares(ref num_Par);
            dgv2.RowCount = arr_Par.Length;

            for (int ren = 0; ren < dgv2.RowCount; ren++)
                for (int col = 0; col < dgv2.ColumnCount; col++)
                    dgv2.Rows[ren].Cells[col].Value = arr_Par[col];
        }
    }
}


No hay comentarios:

Publicar un comentario