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;
}
}
}
No hay comentarios:
Publicar un comentario