domingo, 16 de junio de 2013

PROYECTO

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

//namespace BusquedaHashing_Consola
//{
    class UNIDAD8
    {
        static public int Hash_Indice(int[] vec, int clave, int n)
        {
            int pos = 0;
            
            //Determinar si "n" es primo.
            for (; ;)
            {
                if (Convert.ToBoolean(vec[pos]))
                    pos++;
                else
                    break;
                if (pos >= n)
                    pos = 0;
            }
            return pos;
        }

        static public int Hash_Buscar(int[] vec, int clave, int n)
        {
            int pos, band = 0;
            pos = clave % n;
            for (; ;)
            {
                if (vec[pos] != clave)
                    pos++;
                else
                    break;
                if (pos >= n)
                    pos = 0;
                band++;
                if (band > n)
                    return -1;
            }
            return pos;
        }
    }
//}
==========================================================
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 BusquedaHashing_Visual
{
    public partial class Form1 : Form
    {
        int n;
        int[] vec;

        string[] vecN;
        string[] Nombres = { "Pinol", "Suavitel", "Ariel", "Cloralex", "Colgate", "Fabuloso", "Crest" };

        double[] vecC;

        int[] vecCan;

        string[] vecF;
        string[] Fecha = { "05/01/2013", "10/02/2013", "15/03/2013", "20/04/2013", "25/05/2013", "30/06/2013", "35/07/2013" };
       
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            n= Convert.ToInt32(textBox1.Text);
            vec = new int[n];
            vecN = new string[vec.Length];
            vecC = new double[vec.Length];
            vecCan = new int[vec.Length];
            vecF = new string[vec.Length];
            dataGridView1.RowCount = n;

            textBox2.Enabled = true;
            button2.Enabled = true;
            textBox2.Clear();
            textBox3.Clear();

            Random ran = new Random();
            for (int ren = 0; ren < vec.Length; ren++)
            {
                int val = ran.Next(10000) + 1;
                vec[ren] = val;
                dataGridView1[0, ren].Value = vec[ren].ToString();
                
                string nom = Nombres[ran.Next(Nombres.Length)].ToString();
                vecN[ren] = nom.ToString();
                dataGridView1[1, ren].Value = vecN[ren].ToString();

                double cos = ran.NextDouble() * 100;
                vecC[ren] = cos;
                dataGridView1[2, ren].Value = vecC[ren].ToString("##.##");

                int can = ran.Next(10) + 1;
                vecCan[ren] = can;
                dataGridView1[3, ren].Value = vecCan[ren].ToString();

                string fech = Fecha[ran.Next(Fecha.Length)].ToString();
                vecF[ren] = fech.ToString();
                dataGridView1[4, ren].Value = vecF[ren].ToString();
            }

            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    this.dataGridView1.Rows[i].HeaderCell.Value = (i + 1).ToString();
                }
            }
            dataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int dato = Convert.ToInt32(textBox2.Text);
            int pos = UNIDAD8.Hash_Buscar(vec, dato, n);
            if (pos == -1)
            {
                MessageBox.Show("El Producto No Existe en la Tabla", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
                textBox3.Text = "- La Clave del Producto es: " + dato + "\r\n- Se Encuentra en la Posición: " + (pos + 1) + " de la Tabla.";
                //MessageBox.Show("El Dato " + dato + " Fue Encontrado en la Posicion: " + (pos + 1), "Correcto");
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
                e.Handled = false;
            else if (char.IsControl(e.KeyChar))
                e.Handled = false;
            else
            {
                MessageBox.Show("Solo Se Permite la Cantidad de Productos", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox1.Focus();
                e.Handled = false;
            }
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar))
                e.Handled = false;
            else if (char.IsControl(e.KeyChar))
                e.Handled = false;
            else
            {
                MessageBox.Show("Solo Se Permite la Clave del Producto", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                textBox2.Focus();
                e.Handled = false;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            textBox1.MaxLength = 3;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox2.MaxLength = 4;
        }
    }
}










PROYECTO USANDO HASH

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.Security.Cryptography;
using System.Collections;


namespace Proyecto_Hash__Visual_
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                HashAlgorithm cod = new SHA1CryptoServiceProvider();
                byte[] texto = Encoding.UTF8.GetBytes(textBox1.Text);
                byte[] valor = cod.ComputeHash(texto);
                cod.Clear();
                textBox2.Text = Convert.ToBase64String(valor);
                textBox3.Text = BitConverter.ToString(valor);
                string suma;

                foreach (byte b in valor)
                {
                    suma = Convert.ToString(b);
                    textBox4.Text += suma + " ";
                }

                FileStream fs1 = new FileStream("original.arp", FileMode.Create, FileAccess.Write);
                BinaryWriter sw = new BinaryWriter(fs1);
                sw.Write(Encoding.ASCII.GetString(texto));
                sw.Flush();
                sw.Close();
                fs1.Close();
                fs1 = new FileStream("cifrado.arp", FileMode.Create, FileAccess.Write);
                BinaryWriter bw = new BinaryWriter(fs1);
                bw.Write(Convert.ToBase64String(valor));
                bw.Close();
            }
            else
                MessageBox.Show("Inserta la palabra o texto a codificar","Error");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
    }
}


HASH

12312313123123
UNIDAD 6    MÉTODOS DE BÚSQUEDA

sdasdasdas

asdasdasd

mezcla

asdasdasdasdasda

Programa con todos los métodos de ordenación


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//se agregan......
using System.Windows.Forms;
using System.Drawing;

namespace Shaker_Sort__VISUAL_
{
    class Ordenamiento
    {
        Label comparaciones, intercambios;
        int[] vec;
        int tam, comp, interc;
        public Ordenamiento(int[] v,Label L1, Label L2)
        {                                                            //forma = f
            comparaciones = L1;
            intercambios = L2;
            tam = v.Length;
            vec = new int[tam];
            for (int i = 0; i < v.Length; i++)
                vec[i] = v[i];
            interc = comp = 0;
        }

        public void BurbujaA(Form forma, bool como)
        {
            for (int i = 0; i < vec.Length; i++)
                for (int j = vec.Length - 1; i < j; j--)
                {
                    comp++;
                    if (vec[j - 1] > vec[j])
                    {
                        int aux = vec[j - 1];
                        vec[j - 1] = vec[j];
                        vec[j] = aux;
                        interc++;
                        //Thread.Sleep(100);
                       // mostrarN(forma);
                    }
                }
            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }
        public void BurbujaD(Form forma, bool como)
        {
            for (int i = 0; i < vec.Length; i++)
                for (int j = vec.Length - 1; i < j; j--)
                {
                    comp++;
                    if (vec[j - 1] < vec[j])
                    {
                        int aux = vec[j - 1];
                        vec[j - 1] = vec[j];
                        vec[j] = aux;
                        interc++;
                        for (int s = 0; s < 100000000; s++) ;
                        mostrarN(forma);
                    }
                }

            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }
//-------------------------------------------------------------------------------------------

        public void SacudidaA(Form forma, bool como)
        {
            int a, b, c, d, aux;
            c = 1;
            b = vec.Length - 1;
            d = vec.Length - 1;
            do
            {
                for (a = d; a >= c; --a)
                {
                    comp++;
                    if (vec[a - 1] > vec[a])
                    {
                        aux = vec[a - 1];
                        vec[a - 1] = vec[a];
                        vec[a] = aux;
                        b = a;
                        interc++;
                    }
                }
                c = b + 1;
                for (a = c; a < d + 1; ++a)
                {
                    comp++;
                    if (vec[a - 1] > vec[a])
                    {
                        aux = vec[a - 1];
                        vec[a - 1] = vec[a];
                        vec[a] = aux;
                        b = a;
                        interc++;
                    }
                }
                d = b - 1;
            } while (c <= d);

            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }

        public void SacudidaD(Form forma, bool como)
        {
            int a, b, c, d, aux;
            c = 1;
            b = vec.Length - 1;
            d = vec.Length - 1;
            do
            {
                for (a = d; a >= c; --a)
                {
                    comp++;
                    if (vec[a - 1] < vec[a])
                    {
                        aux = vec[a - 1];
                        vec[a - 1] = vec[a];
                        vec[a] = aux;
                        b = a;
                        interc++;
                    }
                }
                c = b + 1;
                for (a = c; a < d + 1; ++a)
                {
                    comp++;
                    if (vec[a - 1] < vec[a])
                    {
                        aux = vec[a - 1];
                        vec[a - 1] = vec[a];
                        vec[a] = aux;
                        b = a;
                        interc++;
                    }
                }
                d = b - 1;
            } while (c <= d);

            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }

//------------------------------------------------------------------------------

        //SAKER
        public void SHSORT(Form forma, bool como)
        {
            SAHKERSORT(0, vec.Length - 1);
            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }

        public void SAHKERSORT(int izq, int der)
        {
            int i, j,N;
          //  izq = 2;
            //der = j;
            j = der;
           i = izq;
            N = vec[(izq + der) / 2];
            do
            {
                for (i = der; i > izq; i--)
                    if (vec[i - 1] > vec[i])
                    {
                        j = 1;
                    }
                izq = j + 1;
                for (i = izq; i > der; i++)
                    if (vec[i - 1] > vec[i])
                    {
                        j = 1;
                    }
                der = j - 1;
            }
            while (izq <= der);
            if (izq < j)
                quicksort(izq, j);
            if (i < der)
                quicksort(i, der);
        }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //QUICK SORT
       public void quicksort(int izq, int der)
        {
            int i,j,pivote,aux;
            i = izq;
            j = der;
            pivote = vec[(izq+der)/2];
            do
            {
                comp++;
                while (vec[i] < pivote && i < der)
                {
                    i++;
                }
                comp++;
                while (vec[j] > pivote && j > izq)
                {
                    j--;
                }
                if(i <= j)
                {
                   aux = vec[i];
                    vec[i] = vec[j];
                    vec[j] = aux;
                    i++;
                    j--;
                    interc++;
                }
            }while(i <=j);
             if(izq < j)
                quicksort(izq,j);
            if(i < der)
                quicksort(i,der);
        }

        public void QSORT(Form forma, bool como)
        {
            quicksort(0, vec.Length - 1);
            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }

        public void quicksortD(int izq, int der)
        {
            int i, j, pivote, aux;
            i = izq;
            j = der;
            pivote = vec[(izq + der) / 2];
            do
            {
                comp++;
                while (vec[i] < pivote && i < der)
                {
                    i++;
                }
                comp++;
                while (vec[j] > pivote && j > izq)
                {
                    j--;
                }
                if (i <= j)
                {
                    aux = vec[i];
                    vec[i] = vec[j];
                    vec[j] = aux;
                    i++;
                    j--;
                    interc++;
                }
            }
            while (i >= j);
            if (izq < j)
                quicksort(izq, j);
            if (i < der)
                quicksort(i, der);
        }

        public void QSORTD(Form forma, bool como)
        {
            quicksortD(0, vec.Length - 1);
            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }
//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

        public void Shell_2(Form forma, bool como)
        {
            int i, j, k, s, w, aux;
            int[] a = { 9, 5, 3, 3, 1 };
            for (w = 0; w < a.Length; w++)
            {
                k = a[w];
                s = -k;
                for (i = k; i < vec.Length; ++i)
                {
                    aux = vec[i];
                    j = i - k;
                    //if (s == 0)
                    //{
                    //    s = -k;
                    //    s++;
                    //    vec[s] = aux;
                    //}
                    comp++;
                    try
                    {
                        //while (j >= 0 && j <= vec.Length && aux < vec[j])
                        while (aux < vec[j] && j >= 0 && j <= vec.Length)
                        {
                            vec[j + k] = vec[j];
                            j -= k;
                            interc++;
                        }                       
                    }
                    catch { }
                    vec[j + k] = aux;
                }
            }
            if (como)
                mostrarN(forma);
            else
                mostrarG(forma);
        }

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        //ORDENAMIENTO SHELL SORT

        public void shellSort(int[] vec, int n)
        {
            int i, aux;
            bool band;
            int inc=n;

            while (inc > 1)
            {
                inc = inc / 2;
                band = true;

                while (band)//while (band==true)
                {
                    band = false;
                    i = 0;//i=1;

                    while ((i + inc) < n)// while ((i + inc) <= n)
                    {
                        if (vec[i] > vec[i + inc])
                        {
                            aux = vec[i];
                            vec[i] = vec[i + inc];
                            vec[i + inc] = aux;
                            band = true;
                        }
                        i = i + 1;
                    }
                }
            }
        }//fin del metodo shell sort


        public void shellSort2(Form forma, bool como)
        {
            int i, aux;
            bool band;
            
            int inc = vec.Length;

            while (inc > 1)
            {
                inc = inc / 2;
                band = true;

                while (band)//while (band==true)
                {
                    band = false;
                    i = 0;//i=1;

                    while ((i + inc) < vec.Length)// while ((i + inc) <= n)
                    {
                        if (vec[i] > vec[i + inc])
                        {
                            aux = vec[i];
                            vec[i] = vec[i + inc];
                            vec[i + inc] = aux;
                            band = true;
                        }
                        i = i + 1;
                    }
                }
            }
        }//fin del metodo shell sort

//:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        public void mostrarN(Form f)
        {
            Graphics g = f.CreateGraphics();
            f.Refresh();
            //f.Invalidate();
            Font font = new Font("Arial", 8);
            SolidBrush brush = new SolidBrush(Color.Black);
             int x = (int)font.Size, y = 10;
            for (int i = 0; i < vec.Length; i++)
            {     //DATOS ORDENADOS
                g.DrawString(vec[i].ToString(), font, brush, x, y);
                y += font.Height;
                if (y >= (f.Height - 50))//(int)font.Size * 2))
                {
                    x += (int)font.Size * 5;
                    y = 10;
                }
            }
            comparaciones.Text = "No. de Comp.  =" + "  " + Convert.ToString(comp);
            intercambios.Text = "No. de Interc.  = "+"  "+interc.ToString();                       
        }

        public void mostrarG(Form f)
        {
                Graphics g = f.CreateGraphics();
                f.Refresh();                    
                int ymax = f.Size.Height - 45;
                Pen p = new Pen(Brushes.Blue);
                p.Width = 3;
                g.DrawRectangle(p, 5, 5, 580, ymax); //this.Size.Height - 45);
                for (int i = 0; i < vec.Length; i++)
                {
                    g.DrawLine(Pens.Red, 10 + (5 * i), ymax + 3, 10 + (5 * i), ymax + 3 - vec[i]);                   
                }
                comparaciones.Text ="No. de Comp.  ="+ "  " + Convert.ToString(comp);
                intercambios.Text ="No. de Interc.  = "+"  " + interc.ToString();                        
        }

    }
}

====================================
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 Shaker_Sort__VISUAL_
{
    public partial class Form1 : Form
    {
        int n;
        int[] v;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Refresh();
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            radioButton5.Checked = false;

            label4.Text = "";
            label5.Text = "";

            n = Int32.Parse(numericUpDown1.Value.ToString());

            v = new int[n];


            Graphics g = this.CreateGraphics();
            Font font = new Font("Arial", 8);
            SolidBrush brush = new SolidBrush(Color.Black);

            Random r = new Random();
            if (comboBox1.Text == "Numerica")
            {
                int x = (int)font.Size, y = 5;

                for (int i = 0; i < v.Length; i++)
                {

                    v[i] = r.Next(8999) + 1000;//v[i] = v[i];
                    g.DrawString(v[i].ToString(), font, brush, x, y);

                    y += font.Height;
                    if (y >= (this.Height - 50))
                    {
                        x += (int)font.Size * 5;
                        y = 5;
                    }
                }
            }
            else if (comboBox1.Text == "Grafica")
            {
                int ymax = this.Size.Height - 45;
                Pen p = new Pen(Brushes.Blue);
                p.Width = 2;
                g.DrawRectangle(p, 5, 5, 620, ymax);
                Pen p1 = new Pen(Brushes.Teal);
                p1.Width = 3;

                for (int i = 0; i < v.Length; i++)
                {
                    v[i] = r.Next(ymax - 5) + 1;
                    g.DrawLine(/*Pens.Red*/p1, 10 + (4 * i/*era 1 y salia inclinado*/), ymax + 3, 10 + (4 * i), ymax + 3 - v[i]);
                }

            }

        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (sender == radioButton3)//burbuja simple
            {
                if (comboBox1.Text == "Numerica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.BurbujaA(this, true);
                        this.Text = "Burbuja Simple";
                    }
                    else if (radioButton2.Checked)//descendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.BurbujaD(this, true);
                    }
                }
                else if (comboBox1.Text == "Grafica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.BurbujaA(this, false);
                    }
                    else if (radioButton2.Checked)//descendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.BurbujaD(this, false);
                    }

                }
            }

            if (sender == radioButton4)//burbuja con senal
            {
                if (comboBox1.Text == "Numerica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SacudidaA(this, true);
                        this.Text = "Burbuja Simple";
                    }
                    else if (radioButton2.Checked)//descendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SacudidaD(this, true);
                    }
                }
                else if (comboBox1.Text == "Grafica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SacudidaA(this, false);
                    }
                    else if (radioButton2.Checked)//descendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SacudidaD(this, false);
                    }

                }
            }

            if (sender == radioButton5)
            {
                if (comboBox1.Text == "Numerica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SHSORT(this, true);
                        this.Text = "Burbuja Simple";
                    }
                }
                else if (comboBox1.Text == "Grafica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.SHSORT(this, false);
                    }
                }
            }

             if (sender == radioButton6)
            {
                if (comboBox1.Text == "Numerica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.Shell_2(this, true);
                        this.Text = "Burbuja Simple";
                    }
                }
                else if (comboBox1.Text == "Grafica")
                {
                    if (radioButton1.Checked)//ascendente
                    {
                        Ordenamiento lista = new Ordenamiento(v, label4, label5);
                        lista.Shell_2(this, false);
                    }
                }
            }
             if (sender == radioButton7)
             {
                 if (comboBox1.Text == "Numerica")
                 {
                     if (radioButton1.Checked)//ascendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.QSORT(this, true);
                         this.Text = "Burbuja Simple";
                     }
                     else if (radioButton2.Checked)//descendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.QSORTD(this, true);
                     }
                 }
                 else if (comboBox1.Text == "Grafica")
                 {
                     if (radioButton1.Checked)//ascendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.QSORT(this, false);
                     }
                     else if (radioButton2.Checked)//descendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.QSORTD(this, false);
                     }

                 }

             }
             if (sender == radioButton8)
             {
                 if (comboBox1.Text == "Numerica")
                 {
                     if (radioButton1.Checked)//ascendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.shellSort2(this, true);
                         this.Text = "Burbuja Simple";
                     }
                 }
                 else if (comboBox1.Text == "Grafica")
                 {
                     if (radioButton1.Checked)//ascendente
                     {
                         Ordenamiento lista = new Ordenamiento(v, label4, label5);
                         lista.shellSort2(this, false);
                     }

                 }

             }
        }
    }

}