Utilisando dos clases y Archivos

class Matriz
{
    int[,] mat;
    int ren, col;


    //Constructor
    public Matriz(int r, int c)
    {
        ren = r;
        col = c;
        mat = new int[r, c];
    }


    public Matriz(DataGridView dg)
    {


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


    //Indexador
    public int this[int r, int c]
    {
        get { return mat[r, c]; }
        set { mat[r, c] = value; }
    }


    //Metodos
    public double DiagonalPrinc()//int[,] m)
    {
        if (ren != col)
            return 0.0;
        double promedio = 0.0;
        for (int r = 0; r < mat.GetLength(0); r++)
            for (int c = 0; c < mat.GetLength(1); c++)
                if (r == c)
                    promedio += mat[r, c];
        return promedio / mat.GetLength(0);
    }


    public double DiagonalInv()//int[,] m)
    {
        if (ren != col)
            return 0.0;
        double prom = 0.0;
        int _col = col - 1;
        for (int i = 0; i < ren; i++)
            prom += mat[i, _col--];
        return prom / col;
    }


    //Propiedades
    public int Renglones
    {
        get { return mat.GetLength(0); }
    }


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


    //Multiplicar matrices
    static public Matriz operator *(Matriz a, Matriz b)
    {
        Matriz aux = new Matriz(a.ren, b.col);
        for (int i = 0; i < a.ren; i++)
        {
            for (int j = 0; j < aux.col; j++)
            {
                aux[i, j] = 0;
                for (int k = 0; k < aux.ren; k++)
                    aux[i, j] = aux[i, j] + a[i, k] * b[k, j];
            }
        }
        return aux;
    }
}
//-----------------------------------------------------

class Vector
{
    int[] vec;


    public Vector(int dim)
    {
        try
        {
            if (dim > 0)
                vec = new int[dim];
            else
                vec = new int[5];
        }
        catch (FormatException ex)
        {
            Console.Write("Solo se aceptan numeros enteros positivos" + ex);
        }
    }


    public int Mayor()
    {
        int mayor = vec[0];
        //Inicializamos la variable i en 1 porque ya tenemos el dato en la posicion 0
        for (int i = 1; i < vec.Length; i++)
            if (mayor < vec[i])
                mayor = vec[i];
        return mayor;
    }


    public int Menor()
    {
        int menor = vec[0];
        for (int i = 0; i < vec.Length; i++)
            if (menor > vec[i])
                menor = vec[i];
        return menor;
    }


    public void Insertar()
    {
        Random ran = new Random();
        for (int i = 0; i < vec.Length; i++)
            vec[i] = ran.Next(1000, 10001);
    }


    public void Asignar(int val, int pos)
    {
        if (pos >= 0 && pos < vec.Length)
            vec[pos] = val;
    }


    public int Leer(int pos)
    {
        return vec[pos];
    }


    //El uso del indexador suple los 2 metodos anteriores
    public int this[int pos]
    {
        get { return vec[pos]; }
        set { vec[pos] = value; }
    }


    public int Tamaño
    {
        get { return vec.Length; }
    }


    public void Leer()
    {
        for (int i = 0; i < vec.Length; i++)
            Console.WriteLine(vec[i]);
    }
}

//-----------------------------------------------------------------
  public partial class Form1 : Form
    {
        int[,] aux ={{0,2,0,3,7},
                    {1,0,0,4,9},
                    {8,0,2,5,1},
                    {9,0,8,0,0},
                    {0,0,0,0,1}};
        Matriz mat;

        public Form1()
        {
            InitializeComponent();            
        }

        static private void LeerArchivo(string nomarch, int ren, int col, Matriz mat)
        {
            BinaryReader br = null;
            try
            {
                if (File.Exists(nomarch))
                {
                    br = new BinaryReader(new FileStream(nomarch, FileMode.Open, FileAccess.Read));
                    
                    for (int r = 0; r < ren; r++)
                        for (int c = 0; c < col; c++)
                        {
                            int dato = br.ReadInt32();
                            mat[r, c] = dato;
                        }
                }
                else
                    MessageBox.Show("No existe el archivo");
            }
            catch (EndOfStreamException)
            {
                MessageBox.Show("Fin de archivo");
            }
            finally
            {
                if (br != null)
                    br.Close();
            }
        }
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            dataGridView1.RowCount = dataGridView2.RowCount = Convert.ToInt32(numericUpDown1.Value);
        }

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

        private void button1_Click(object sender, EventArgs e)
        {
            int ren = Convert.ToInt32(numericUpDown1.Value);
            int col = Convert.ToInt32(numericUpDown2.Value);
            if ((ren >= 3 && ren <= 5) && (col >= 3 && col <= 5))
            {
                mat = new Matriz(ren, col);
                Vector vec = new Vector(ren);
                for (int r = 0; r < ren; r++)
                {
                    int cont = 0;
                    for (int c = 0; c < col; c++)
                    {
                        mat[r, c] = aux[r, c];
                        dataGridView1.Rows[r].Cells[c].Value = mat[r, c];
                        if (mat[r, c] == 0)
                            cont++;
                        vec[r] = cont;
                        dataGridView2[0, r].Value = vec[r];
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            int ren = Convert.ToInt32(numericUpDown1.Value);
            int col = Convert.ToInt32(numericUpDown2.Value);
            if ((ren >= 3 && ren <= 5) && (col >= 3 && col <= 5))
            {
                mat = new Matriz(ren, col);
                Vector vec = new Vector(ren);
                // aqui le damos la direccion de un archivo
                LeerArchivo("G:\\Examen1_Parte1\\jose.av", ren, col, mat);
                for (int r = 0; r < ren; r++)
                {
                    int cont = 0;
                    for (int c = 0; c < col; c++)
                    {
                        dataGridView1[c, r].Value = mat[r, c];
                        if (mat[r, c] == 0)
                            cont++;
                        vec[r] = cont;
                        dataGridView2[0, r].Value = vec[r];
                    }
                }
            }
        }
    }
}



No hay comentarios:

Publicar un comentario