cola circular

namespace ColaCircular_Vis
{
    class Cola_Circular<T>
    {
        T[] datos;
        int tam;
        int P, U;
       
        public Cola_Circular(int n)
        {
            tam = n;
            datos = new T[tam];
            P = U = -1;        
        }


        public Cola_Circular(int n, T x)
        {
            tam = n;
            datos = new T[tam];
            P = U = -1;


            for (int i = 0; i < tam; i++)
            {
                datos[i] = x;
            }
        }
        


        public bool insertar(T dato,DataGridView dgv)
        {
           
            if((U==tam-1)&&(P==0)||(U+1==P))
            
                return false;
            else
                if(U==tam-1)
                    U=0;
            else
                    U++;
            datos[U]=dato;


            
                for (int i = 0; i < tam; i++)
                {
                    dgv[i, 0].Value = datos[i].ToString();
                }
            
            if(P==-1)
                P=0;
            return true;


            
        }


        public bool eliminar(ref T dato, T x,DataGridView dgv)
        {
            int ii, aux = 0;


            if (P == -1)
            {
                U++;
                return false;
            }
            
            dato = datos[P];
            if (P == U)
            {
                P = U = -1;
                P++;
            }
            else
                if (P == tam - 1)
                {
                    datos[P] = x;
                    P = 0;
                }
                else
                {
                    datos[P] = x;
                    P++;
                }
            for (ii = 0; ii < tam-1; ii++)
            {
                datos[ii] = datos[ii + 1];
                aux = ii;
            }
           
                datos[aux+1] = x;
           


            for (int i = 0; i < tam; i++)
            {
                dgv[i, 0].Value = datos[i].ToString();
            }
            
            P--;
            U--;
            return true;
        }


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


        public bool esta_LLENA()
        {
            return (((U+1)%tam)==P);
        }


    }
}
//----------------------------------------------------------------------------
namespace ColaCircular_Vis
{
    public partial class Form1 : Form
    {
        int tam = 0;
        Cola_Circular<int> cola;
        Cola_Circular<string> c2;
        Cola_Circular<char> c3;
        Cola_Circular<double> c4;
        Cola_Circular<double> c5;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                tam = Convert.ToInt32(textBox1.Text);
                dgv1.RowCount = 1;
                dgv1.ColumnCount = tam;
                //textBox1.Enabled = false;
                //button1.Enabled = false;
                comboBox1.Enabled = true;

            }

            catch
            {
                MessageBox.Show("Ingrese puros números, intente de nuevo");
                textBox1.Text = " ";
                textBox1.Focus();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int op = 0;
            
            if (comboBox1.Text == "Int")
            {
                op = 1;
            }

            if (comboBox1.Text == "String")
            {
                op = 2;
            }

            if (comboBox1.Text == "Char")
            {
                op = 3;
            }

            if (comboBox1.Text == "Double")
            {
                op = 4;
            }

            if (comboBox1.Text == "Float")
            {
                op = 5;
            }

            switch (op)
            {
                case 1:
                    cola = new Cola_Circular<int>(tam);
                    button2.Enabled = false;
                    comboBox1.Enabled = false;
                    break;

                case 2:
                    c2 = new Cola_Circular<string>(tam, " ");
                    button2.Enabled = false;
                    comboBox1.Enabled = false;
                    break;

                case 3:
                    c3 = new Cola_Circular<char>(tam, ' ');
                    button2.Enabled = false;
                    comboBox1.Enabled = false;
                    break;

                case 4:
                    c4 = new Cola_Circular<double>(tam);
                    button2.Enabled = false;
                    comboBox1.Enabled = false;
                    break;

                case 5:
                    c5 = new Cola_Circular<double>(tam);
                    button2.Enabled = false;
                    comboBox1.Enabled = false;
                    break;

                default:
                    MessageBox.Show("Solo las opciones mostradas estan disponibles");
                    comboBox1.Text = "";
                    break;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int op = 0;

            if (comboBox1.Text == "Int")
            {
                op = 1;
            }

            if (comboBox1.Text == "String")
            {
                op = 2;
            }

            if (comboBox1.Text == "Char")
            {
                op = 3;
            }

            if (comboBox1.Text == "Double")
            {
                op = 4;
            }

            if (comboBox1.Text == "Float")
            {
                op = 5;
            }

            switch (op)
            {
                case 1:
                    try
                    {
                        int num = Convert.ToInt32(textBox2.Text);
                        if (!cola.insertar(num, dgv1))
                        {
                            MessageBox.Show("Insuficiencia de memoria");
                            button3.Enabled = false;
                        }
                        //button4.Enabled = true;
                        textBox2.Focus();
                        textBox2.Text = " ";
                    }
                    catch
                    {
                        MessageBox.Show("Solo numeros, por favor");
                        textBox2.Focus();
                        textBox2.Text = "";
                    }
                    break;          

                case 2:
                    try
                    {
                        string cad = Convert.ToString(textBox2.Text);
                        if (!c2.insertar(cad, dgv1))
                        {
                            MessageBox.Show("Insuficiencia de memoria");
                            button3.Enabled = false;
                            //button4.Enabled = true;

                        }
                        //button4.Enabled = true;
                        textBox2.Focus();
                        textBox2.Text = "";

                    }

                    catch
                    {
                        MessageBox.Show("Solo cadena de caracteres");
                        textBox2.Text = " ";
                        textBox2.Focus();
                    }
                    break;

                case 3:
                    {
                        try
                        {
                            char a = Convert.ToChar(textBox2.Text);
                            if (!c3.insertar(a, dgv1))
                            {
                                MessageBox.Show("Insuficiencia de memoria");
                                button3.Enabled = false;
                                //button4.Enabled = true;
                            }
                            //button4.Enabled = true;
                            textBox2.Focus();
                            textBox2.Text = "";
                        }
                        catch
                        {
                            MessageBox.Show("Solo caracteres (una sola letra 'a','b'....)");
                            textBox2.Text = "";
                            textBox2.Focus();
                        }
                    }
                    break;

                case 4:
                    {
                        try
                        {
                            double n = Convert.ToDouble(textBox2.Text);
                            if (!c4.insertar(n, dgv1))
                            {
                                MessageBox.Show("Insuficiencia de memoria");

                                button3.Enabled = false;
                                //button4.Enabled = true;
                            }
                            //button4.Enabled = true;
                            textBox2.Focus();
                            textBox2.Text = "";
                        }
                        catch
                        {
                            MessageBox.Show("Solo numeros");
                            textBox2.Focus();
                            textBox2.Text = "";

                        }
                    }
                    break;

                case 5:
                    {
                        try
                        {
                            double m = Convert.ToDouble(textBox2.Text);
                            if (!c5.insertar(m, dgv1))
                            {
                                MessageBox.Show("Insuficiencia de memoria");

                                button3.Enabled = false;
                                //button4.Enabled = true;

                            }

                        }
                        catch
                        {
                            MessageBox.Show("Solo numeros");
                            textBox2.Focus();
                            textBox2.Text = "";

                        }
                        break;
                    }
            }
        }          

        private void button5_Click(object sender, EventArgs e)
        {
            int op = 0;

            if (comboBox1.Text == "Int")
            {
                op = 1;
            }

            if (comboBox1.Text == "String")
            {
                op = 2;
            }

            if (comboBox1.Text == "Char")
            {
                op = 3;
            }

            if (comboBox1.Text == "Double")
            {
                op = 4;
            }

            if (comboBox1.Text == "Float")
            {
                op = 5;
            }

            switch (op)
            {
                case 1:
                    int num = 1;
                    if (!cola.eliminar(ref num, 0, dgv1))
                    {
                         MessageBox.Show("Ya no hay datos a eliminar, inserte nuevos datos");
                        button3.Enabled = true;
                        //button5.Enabled = false;
                    }
                    textBox3.Text = num.ToString();
                    button3.Enabled = true;
                    break;

                case 2:
                    string cad = " ";
                    if (!c2.eliminar(ref cad, "0", dgv1))
                    {
                        MessageBox.Show("Ya no hay datos a eliminar, inserte nuevos datos");
                        button3.Enabled = true;
                        //button5.Enabled = false;
                    }
                    textBox3.Text = cad.ToString();
                    button3.Enabled = true;
                    break;
                
                case 3:
                    char c=' ';
                    if(!c3.eliminar(ref c, '0',dgv1))
                    {
                        MessageBox.Show("Ya no hay datos a eliminar, inserte nuevos datos");
                        button3.Enabled = true;
                        //button5.Enabled = false;
                    }
                    textBox3.Text = c.ToString();
                    button3.Enabled = true;
                    break;

                case 4:
                    double dato = 2;
                    if(!c4.eliminar(ref dato, 0, dgv1))
                    {
                        MessageBox.Show("Ya no hay datos a eliminar, inserte nuevos datos");
                        button3.Enabled = true;
                        //button5.Enabled = false;
                    }
                    textBox3.Text = dato.ToString();
                    button3.Enabled = true;
                    break;

                case 5:
                    double dato2 = 2;
                    if(!c5.eliminar(ref dato2, 0, dgv1))
                    {
                        MessageBox.Show("Ya no hay datos a eliminar, inserte nuevos datos");
                        button3.Enabled = true;
                        //button5.Enabled = false;
                    }
                    textBox3.Text = dato2.ToString();
                    button3.Enabled = true;
                    break;          
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = "ESCRIBA EL NUMERO..";
            textBox2.Text = "";
            textBox3.Text = "";
            comboBox1.Text = "";
            for (int i = 0; i < dgv1.ColumnCount; i++)
            {
                dgv1[i, 0].Value = " ";
            }
            button5.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            textBox1.Enabled = true;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            Close();
        }        
    }
}

No hay comentarios:

Publicar un comentario