sábado, 15 de junio de 2013

COLA CIRCULAR USANDO SERIALIZACION

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;
using System.Runtime.Serialization.Formatters.Binary;

namespace Colas_VariosTipos_Visual
{

    public partial class Form1 : Form
    {
        double aux2;
        char aux3;
        string aux4;
        string extraer;
        int n, aux1;
        int i = 0;
        bool band = true;
        ColaCirc<int> ent;
        ColaCirc<double> dob;
        ColaCirc<char> cha;
        ColaCirc<string> str;

        public Form1()
        {
            InitializeComponent();
            textBox2.Enabled = false;
            bot_agregar.Enabled = false;
            bot_eliminar.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s = comboBox1.Text;
            switch (s)
            {
                case "Int":
                    try
                    {
                        aux1 = Convert.ToInt32(textBox2.Text);
                        band = true;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Ingrese un entero");
                        band = false;
                    }
                    if (band == true)
                    {
                        if (ent.insertar(aux1))
                            ent.mostrar(dgv1);
                        else
                            MessageBox.Show("Cola llena,Desbordamiento");
                    }
                    break;
                case "Double":
                    try
                    {
                        aux2 = Convert.ToDouble(textBox2.Text);
                        band = true;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Ingrese un doble");
                        band = false;
                    }
                    if (band == true)
                    {

                        if (dob.insertar(aux2))
                            dob.mostrar(dgv1);

                        else
                            MessageBox.Show("Cola llena,Desbordamiento");
                    }
                    break;
                case "Char":
                    try
                    {
                        aux3 = Convert.ToChar(textBox2.Text);
                        band = true;
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Ingrese un entero");
                        band = false;
                    }
                    if (band == true)
                    {

                        if (cha.insertar(aux3))
                            cha.mostrar(dgv1);

                        else

                            MessageBox.Show("Cola llena,Desbordamiento");


                    }
                    break;
                case "String":

                    if (ent.insertar(aux1))
                        str.mostrar(dgv1);

                    else
                        MessageBox.Show("Cola llena,Desbordamiento");
                    break;
            }
            textBox2.Clear();
            textBox2.Focus();

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {

                try
                {
                    n = Convert.ToInt32(textBox1.Text);
                    dgv1.ColumnCount = n;
                    textBox2.Enabled = true;
                    bot_eliminar.Enabled = true;
                    bot_agregar.Enabled = true;
                    textBox1.Enabled = false;
                    comboBox1.Enabled = false;
                    textBox2.Focus();
                    for (int i = 0; i < n; i++)
                    {
                        dgv1[i, 0].Value = " ";
                    }

                }
                catch (Exception)
                {

                    MessageBox.Show("Ingrese un numero");
                }
                string s = comboBox1.Text;
                switch (s)
                {
                    case "Int": ent = new ColaCirc<int>(n); break;
                    case "Double": dob = new ColaCirc<double>(n); break;
                    case "Char": cha = new ColaCirc<char>(n); break;
                    case "String": str = new ColaCirc<string>(n); break;

                    default: ent = new ColaCirc<int>(n); break;
                }
            }
        }

        private void bot_eliminar_Click(object sender, EventArgs e)
        {
            string s = comboBox1.Text;
            switch (s)
            {
                case "Int":
                    aux1 = 0;
                    if (ent.eliminar(ref aux1, -1))
                    {
                        label6.Text = aux1.ToString();
                        ent.mostrar(dgv1);
                    }
                    else
                        MessageBox.Show("Cola vacia,Subdesbordamiento");
                    break;
                case "Double":
                    aux2 = 0.0;
                    if (dob.eliminar(ref aux2, 0.0))
                    {
                        label6.Text = aux2.ToString();
                        dob.mostrar(dgv1);
                    }
                    else
                        MessageBox.Show("Cola vacia,Subdesbordamiento");
                    break;
                case "Char":
                    aux3 = ' ';
                    if (cha.eliminar(ref aux3, ' '))
                    {
                        label6.Text = aux3.ToString();
                        cha.mostrar(dgv1);
                    }
                    else
                        MessageBox.Show("Cola vacia,Subdesbordamiento");
                    break;
                case "String":

                    aux4 = " ";
                    if (str.eliminar(ref aux4, " "))
                    {
                        label6.Text = aux4.ToString();
                        str.mostrar(dgv1);
                    }
                    else
                        MessageBox.Show("Cola vacia,Subdesbordamiento");
                    break;
            }


        }

        public SerializadorBinario serializador = new SerializadorBinario();

        private void butGuarda_Click(object sender, EventArgs e)
        {
            try
            {
                // serializador = new SerializadorBinario();

                ColaCirc<int> a = new ColaCirc<int>(dgv1.Rows.Add());//textbox2.text
                // a= ent.mostrar(dgv1);
                if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    String ruta = saveFileDialog1.FileName;

                    if (serializador.SerializarArchivo(ruta, a))
                    {
                        MessageBox.Show("Guardado Satisfactoriamente");
                    }
                    else
                    {
                        MessageBox.Show("Error al intentar Serializar");
                    }
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void butCarga_Click(object sender, EventArgs e)
        {
            try
            {
                serializador = new SerializadorBinario();
                ColaCirc<int> a;

                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    a = (ColaCirc<int>)serializador.DeserializarArchivo(openFileDialog1.FileName);

                ent.mostrar(dgv1);

                MessageBox.Show("Cargado Satisfactoriamente");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error" + ex.Message);
            }
        }

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Colas_VariosTipos_Visual
{
    [Serializable]
    class ColaCirc<T>
    {
        T[] datos;
        int tam, P, U;
        bool most;

        public ColaCirc(int n)
        {
            tam = n;
            datos = new T[tam];
            P = U = -1;
        }

        public bool insertar(T dato)
        {
            most = true;
            if ((U == tam - 1) && (P == 0) || (U + 1 == P))

                return false;

            else if (U == tam - 1)
                U = 0;
            else
                U++;
            datos[U] = dato;
            if (P == -1)
                P = 0;
            return true;

        }


        public bool eliminar(ref T dato, T x)
        {
            most = false;
            if (P == -1)
                return false;

            dato = datos[P];
            if (P == U)//Hay un solo elemento
                P = U = -1;
            else if (P == tam - 1)
            {
                datos[P] = x;
                P = 0;
            }
            else
            {
                datos[P] = x;
                P++;
            }
            return true;

        }

        public bool esta_vacia()
        {
            return (P == -1);
        }

        public bool esta_llena()
        {
            return (((U + 1) % tam) == P);
        }
        public int u
        {
            get { return U; }
        }
        public int p
        {
            get { return P; }
        }
        public T this[int indice]
        {
            get
            {
               
                return datos[indice];
            }
            set
            {
               
                datos[indice] = value;
            }
        }

        public void mostrar(DataGridView dgv)
        {
            try
            {
                if (most == true)
                {
                    if (U + 1 == tam)
                    {
                        dgv[U, 0].Value = "  ";
                    }
                    else
                    {
                        dgv[U, 0].Value = "  ";
                    }
                    dgv[U, 0].Value = datos[U].ToString();
                   
                }
                else
                   dgv[p - 1, 0].Value = "   ";

                    dgv[P, 0].Value = datos[P].ToString();
               
            }
            catch (Exception)
            {
                MessageBox.Show("Fuera del rango");
               
            }
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Colas_VariosTipos_Visual
{
    class SerializadorBinario
    {
        private FileStream archivo;
        private BinaryFormatter serializador;

        private String logerror;

        public bool SerializarArchivo(string ruta,Object obj)
        {
            try
            {
                serializador=new BinaryFormatter();
                archivo = new FileStream(ruta, FileMode.Create, FileAccess.Write, FileShare.Write);
                serializador.Serialize(archivo, obj);

                return true;
            }
            catch(Exception ex)
            {
                this.logerror = ex.Message;
                return false;
            }
        }


        public Object DeserializarArchivo(string ruta)
        {
            try
            {
                serializador = new BinaryFormatter();
                FileStream archivo = new FileStream(ruta,FileMode.Open,FileAccess.Read,FileShare.Write);
                Object obj;

                obj = serializador.Deserialize(archivo);
                return obj;
            }
            catch (Exception ex)
            {
                this.logerror = ex.Message;
                return new object();
            }
        }


        public String LogError
        {
            get { return logerror; }
            set { logerror = value; }
        }

    }
}


No hay comentarios:

Publicar un comentario