sábado, 15 de junio de 2013

PROYECTO DE LISTAS ENLAZADAS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proyecto_Listas
{
    class Nodo
    {
        private Amigo datos;
        private Nodo sig;


        public Nodo()
        {
        }

        public Nodo(Amigo pdato, Nodo psig)
        {
            datos = pdato;
            sig = psig;
        }

        public Nodo(Amigo pdato)
        {
            datos = pdato;
            sig = null;
        }

        public Amigo DATOS
        {
            get { return datos; }
            set { datos = value; }
        }

        public Nodo SIG
        {
            get { return sig; }
            set { sig = value; }
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Proyecto_Listas
{
    class Amigo
    {
        private string Nombre;
        private string Apellido;
        private int edad;
        private char sexo;
        private string TelCasa;
        private string TelCel;
        private string FechaCumple;
        private string Mail;
        private string Facebook;
        private string Twitter;


        public Amigo()
        {
        }

        public Amigo(string Nombre,string Apellido, int edad, char sexo, string TelCasa, string TelCel, string FechaCumple, string Mail, string Facebook, string Twitter)
        {
            this.Nombre = Nombre;
            this.Apellido = Apellido;
            this.edad = edad;
            this.sexo = sexo;
            this.TelCasa = TelCasa;
            this.TelCel = TelCel;
            this.FechaCumple = FechaCumple;
            this.Mail = Mail;
            this.Facebook = Facebook;
            this.Twitter = Twitter;
        }

        public void MostrarArticulo(DataGridView dgv, int col)
        {
            dgv[col, 0].Value = Nombre;
            dgv[col, 1].Value = Apellido;
            dgv[col, 2].Value = edad;
            dgv[col, 3].Value = sexo;
            dgv[col, 4].Value = TelCasa;
            dgv[col, 5].Value = TelCel;
            dgv[col, 6].Value = FechaCumple;
            dgv[col, 7].Value = Mail;
            dgv[col, 8].Value = Facebook;
            dgv[col, 9].Value = Twitter;
        }

        public string NOMBRE
        {
            get { return Nombre; }
            set { Nombre = value; }
        }

        public string APELLIDO
        {
            get { return Apellido; }
            set { Apellido = value; }
        }

        public int EDAD
        {
            get { return edad; }
            set { edad = value; }
        }

        public char SEXO
        {
            get { return sexo; }
            set { sexo = value; }
        }

        public string TELCASA
        {
            get { return TelCasa; }
            set { TelCasa = value; }
        }

        public string TELCEL
        {
            get { return TelCel; }
            set { TelCel = value; }
        }

        public string FECHACUMPLE
        {
            get { return FechaCumple; }
            set { FechaCumple = value; }
        }

        public string MAIL
        {
            get { return Mail; }
            set { Mail = value; }
        }

        public string FACEBOOK
        {
            get { return Facebook; }
            set { Facebook = value; }
        }

        public string TWITTER
        {
            get { return Twitter; }
            set { Twitter = value; }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Proyecto_Listas
{
    class Lista
    {
        private Nodo pNodo;
        private Nodo uNodo;

        public Lista()
        {
            pNodo = uNodo = null;
        }

        public bool Vacia()
        {
            if (pNodo == null)
                return true;
            else
                return false;
        }

        public void InsAlIni(Amigo amiIns)
        {
            if (Vacia())
            {
                Nodo nuevo = new Nodo(amiIns);
                pNodo = uNodo = nuevo;
            }
            else
            {
                Nodo nuevo = new Nodo(amiIns, pNodo);
                pNodo = nuevo;
            }
        }


        public void InsAlFin(Amigo amiIns)
        {
            if (Vacia())
            {
                Nodo nuevo = new Nodo(amiIns);
                pNodo = uNodo = nuevo;
            }
            else
            {
                Nodo nuevo = new Nodo(amiIns);
                uNodo.SIG = nuevo;
                uNodo = nuevo;
            }
        }


        public Amigo EliAlIni()
        {
            if (!Vacia())
            {
                Amigo ret = new Amigo();
                if (pNodo == uNodo)
                {
                    ret = pNodo.DATOS;
                    pNodo = uNodo = null;
                }
                else
                {
                    ret = pNodo.DATOS;
                    pNodo = pNodo.SIG;
                }
                return ret;
            }
            else
                MessageBox.Show("La lista esta vacia","Lista Vacia",MessageBoxButtons.OK,MessageBoxIcon.Hand);
            return new Amigo();
        }

      
        public Amigo elimAlFin()
        {
            if (!Vacia())
            {
                Amigo ret = new Amigo();
                if (pNodo == uNodo)
                {
                    ret = uNodo.DATOS;
                    pNodo = uNodo = null;
                }
                else
                {
                    Nodo aux = pNodo;
                    while (aux.SIG != uNodo)
                        aux = aux.SIG;
                    ret = uNodo.DATOS;
                    uNodo = aux;
                    aux.SIG = null;
                }
                return ret;
            }
            else
                MessageBox.Show("La lista esta vacia");
            return new Amigo();
        }

        public int noNodos()
        {
            if (!Vacia())
            {
                int nodos = 0;
                Nodo aux = pNodo;

                while (aux != null)
                {
                    nodos++;
                    aux = aux.SIG;
                }
                return nodos;
            }
            else
            {
                MessageBox.Show("La lista esta vacia");
                return new int();
            }
        }


        public void Mostrar(DataGridView dgv)
        {
            if (!Vacia())
            {
                dgv.Rows.Clear();
                dgv.Columns.Clear();
                dgv.ColumnCount = noNodos();
                dgv.RowCount = 10;

                int col = 0;
                Nodo aux = pNodo;
                while (aux != null)
                {
                    aux.DATOS.MostrarArticulo(dgv, col++);
                    aux = aux.SIG;
                }
            }
            else
            {
                MessageBox.Show("La Lista esta vacia");
                dgv.Rows.Clear();
                dgv.Columns.Clear();
            }

        }
    }
}
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;
using System.IO;



namespace Proyecto_Listas
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Lista obLista;
   
        Amigo obAmi;

        private void butInsertarIni_Click(object sender, EventArgs e)
        {
            try
            {
                obAmi=new Amigo(textNombre.Text,textApellido.Text, Convert.ToInt32(textEdad.Text),Convert.ToChar(comboSexo.Text),Convert.ToString(textTelCasa.Text),Convert.ToString(textTelCel.Text),Convert.ToString(textFechaCumple.Text),Convert.ToString(textMail.Text),Convert.ToString(textFacebook.Text),Convert.ToString(textTwitter.Text));
                    obLista.InsAlIni(obAmi);
                    obLista.Mostrar(dataGridView1);

                    dataGridView1.Rows[0].HeaderCell.Value = "NOMBRE";
                    //dataGridView1.Rows[0].Height = 50;
                    dataGridView1.Rows[1].HeaderCell.Value = "APELLIDO";
                    dataGridView1.Rows[2].HeaderCell.Value = "EDAD";
                    dataGridView1.Rows[3].HeaderCell.Value = "SEXO";
                    dataGridView1.Rows[4].HeaderCell.Value = "TEL";
                    dataGridView1.Rows[5].HeaderCell.Value = "CEL";
                    dataGridView1.Rows[6].HeaderCell.Value = "CUMPLE";
                    dataGridView1.Rows[7].HeaderCell.Value = "CORREO";
                    dataGridView1.Rows[8].HeaderCell.Value = "FACEBOOK";
                    dataGridView1.Rows[9].HeaderCell.Value = "TWEET";

              
            }
            catch
            {
                MessageBox.Show("Para guardar datos se debe crear la lista primero");
            }
        }

        private void butInsertarFin_Click(object sender, EventArgs e)
        {
            try
            {
                obAmi = new Amigo(textNombre.Text,Convert.ToString(textApellido.Text), Convert.ToInt32(textEdad.Text), Convert.ToChar(comboSexo.Text), Convert.ToString(textTelCasa.Text), Convert.ToString(textTelCel.Text), Convert.ToString(textFechaCumple.Text), Convert.ToString(textMail.Text), Convert.ToString(textFacebook.Text), Convert.ToString(textTwitter.Text));
                obLista.InsAlFin(obAmi);
                obLista.Mostrar(dataGridView1);

                dataGridView1.Rows[0].HeaderCell.Value = "NOMBRE";
                //dataGridView1.Rows[0].Height = 50;
                dataGridView1.Rows[1].HeaderCell.Value = "APELLIDO";
                dataGridView1.Rows[2].HeaderCell.Value = "EDAD";
                dataGridView1.Rows[3].HeaderCell.Value = "SEXO";
                dataGridView1.Rows[4].HeaderCell.Value = "TEL";
                dataGridView1.Rows[5].HeaderCell.Value = "CEL";
                dataGridView1.Rows[6].HeaderCell.Value = "CUMPLE";
                dataGridView1.Rows[7].HeaderCell.Value = "CORREO";
                dataGridView1.Rows[8].HeaderCell.Value = "FACEBOOK";
                dataGridView1.Rows[9].HeaderCell.Value = "TWEET";

            }
            catch
            {
                MessageBox.Show("Para guardar datos se debe crear la lista primero");
            }
        }

        private void butCrear_Click(object sender, EventArgs e)
        {
            try
            {
                    obLista = new Lista();
                    MessageBox.Show("Lista Creada");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butAleatorio_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            string[] ArregloNombres = { "Claudia", "Abel", "Ana Laura", "Leo Decko", "Brenda", "Kike", "Elizabeth", "Che", "Mariela", "Nando", "Zita", "Edardo" };
            string[] ArregloApellido = { "Fabela", "Gonzalez", "Rico", "Martinez", "Rodriguez", "Gomez", "Villa", "Hernandez", "Castro" };
            string[] ArregloCumple = { "02-04-1992", "16-10-1991", "29-08-1996", "24-01-1989", "28-03-1990", "10-12-1992" };
            string[] ArregloMail = { "luis_34s@hotmail.com", "elpatoloco@hotmail.com", "PepePecasPicaPapas@hotmail.com", "AmeicaNoSirve@hotmail.com", "CamaronkeseDuerme@hotmail.com" };
            string[] ArregloFacebook = { "luis_34s@hotmail.com", "elpatoloco@hotmail.com", "PepePecasPicaPapas@hotmail.com", "AmeicaNoSirve@hotmail.com", "CamaronkeseDuerme@hotmail.com" };
            string[] ArregloTwitter = { "@luis_34s", "@Lucas","@NoSoyNiña", "@Gamer","@RanaRene", "@PlazaSesamo"};

            int nom = r.Next(0, 12);
            textNombre.Text = ArregloNombres[nom];
            textApellido.Text = ArregloApellido[r.Next(0, 9)];
            textEdad.Text = r.Next(18, 26).ToString();
          
            if (nom % 2 == 0)
                //textSexo.Text=Convert.ToString('F');
                comboSexo.Text=Convert.ToString('F');
            else
                //textSexo.Text = Convert.ToString('M');
            comboSexo.Text = Convert.ToString('M');


            textTelCasa.Text = 77+"-"+r.Next(3,9)+"-"+r.Next(10,99)+"-"+r.Next(10,99)/* r.Next(30000, 99999)*/.ToString();

            textTelCel.Text = r.Next(872) + "-" + r.Next(111, 999) + "-" + r.Next(11, 99) + "-" + r.Next(10, 99)/*r.Next(1000000, 9999999)*/.ToString();

            textFechaCumple.Text = ArregloCumple[r.Next(0, 6)];
            textMail.Text = ArregloMail[r.Next(0, 5)];

            textFacebook.Text = ArregloFacebook[r.Next(0, 5)];
            textTwitter.Text = ArregloTwitter[r.Next(0, 6)];
        }

        private void butElimInicio_Click(object sender, EventArgs e)
        {
            try
            {
                    obLista.EliAlIni();
                    obLista.Mostrar(dataGridView1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bueElimFinal_Click(object sender, EventArgs e)
        {
            try
            {
                    obLista.elimAlFin();
                    obLista.Mostrar(dataGridView1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void guardarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            // Grabamos los datos el dgv a un archivo binario
            FileStream fs = null;
            BinaryWriter bw = null;

            try
            {
                fs = new FileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.Write);
                bw = new BinaryWriter(fs);

                //progressBar1.Maximum = dgv.RowCount - 1;

                // grabamos el número de renglones

                bw.Write(dataGridView1.ColumnCount - 1);

                for (int i = 0; i < dataGridView1.ColumnCount - 1; i++)
                {

                    bw.Write(dataGridView1[i,1].Value.ToString());
                    bw.Write(dataGridView1[i,2].Value.ToString());
                    bw.Write(Convert.ToInt32(dataGridView1[i, 3].Value));
                    bw.Write(Convert.ToChar(dataGridView1[i, 4].Value.ToString()));
                    bw.Write(dataGridView1[i, 5].Value.ToString());
                    bw.Write(dataGridView1[i, 6].Value.ToString());
                    bw.Write(dataGridView1[i, 7].Value.ToString());
                    bw.Write(dataGridView1[i, 8].Value.ToString());
                    bw.Write(dataGridView1[i, 9].Value.ToString());
                    bw.Write(dataGridView1[i, 10].Value.ToString());

                    //bw.Write(dataGridView1[0, i].Value.ToString()); // cadena
                    //bw.Write(Convert.ToDouble(dataGridView1[1, i].Value)); // doble
                    //bw.Write(Convert.ToInt32(dataGridView1.[2, i].Value)); // entero

                   // progressBar1.Value = i + 1;
                }

                MessageBox.Show("Archivo grabado ...");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                if (bw != null)
                    bw.Close();

                if (fs != null)
                    fs.Close();
            }

           
        }

        private void abrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            FileStream fs = null;
            BinaryReader br = null;

            try
            {
                fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
                br = new BinaryReader(fs);

                dataGridView1.Rows.Clear();
                int ren = 0;

                // leemo el total de renglones
                //progressBar1.Maximum = br.ReadInt32();

                while(fs.Position!=fs.Length)
                {
                    dataGridView1.Rows.Add();

                    dataGridView1[ren,0].Value = br.ReadString();
                    dataGridView1[ren,1].Value = br.ReadString();
                    dataGridView1[ren,2].Value = br.ReadInt32();
                    dataGridView1[ren,3].Value = br.ReadString();
                    dataGridView1[ren,4].Value = br.ReadChar();
                    dataGridView1[ren,5].Value = br.ReadString();
                    dataGridView1[ren,6].Value = br.ReadString();
                    dataGridView1[ren,7].Value = br.ReadString();
                    dataGridView1[ren,8].Value = br.ReadString();
                    dataGridView1[ren,9].Value = br.ReadString();

                    ren++;
                    //progressBar1.Value = ren;      
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                if (br != null)
                    br.Close();

                if (fs != null)
                    fs.Close();
            }
        }
    }
}



No hay comentarios:

Publicar un comentario