Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hash;
- import java.util.Random;
- import javax.swing.JOptionPane;
- /**
- *
- * @author Damião Cunha
- */
- public class Hash
- {
- private int[] i_vetor = new int[100];
- private int[] i_endereco = new int[100];
- private int[] i_tabela = new int[100];
- private String[] str_colisao = new String[100];
- private int i_chaves=0, i_qntChaves, i_indice = 0;
- public void gerarChaves(int qntChaves)
- {
- Random rand = new Random();
- System.out.println("---------------------------------------------------------");
- System.out.println("| Tabela de Chaves |");
- System.out.println("---------------------------------------------------------");
- System.out.println("| Sequencia | Nº Sorteado | Chaves | Observação |");
- System.out.println("---------------------------------------------------------");
- while (this.i_qntChaves < qntChaves)
- {
- int aux = rand.nextInt(100);
- if(testaIgual(aux) == false)
- {
- i_vetor[this.i_indice] = aux;
- System.out.printf("%1s %6d %7s %7d %7s %5d %4s %13s\n","|", this.i_indice,"|",i_vetor[this.i_indice],"|",i_chaves,"|","|");
- System.out.println("---------------------------------------------------------");
- this.i_qntChaves++;
- i_chaves ++;
- }else
- {
- System.out.printf("%1s %6d %7s %7d %7s %5d %4s %8s %3s\n","|",this.i_indice,"|",aux,"|",i_chaves,"|","Já Existe","|","|");
- System.out.println("---------------------------------------------------------");
- }
- this.i_indice++;
- }
- }
- public boolean testaIgual(int valor)
- {
- for(int i=0; i < this.i_indice;i++)
- {
- if(i_vetor[i] == valor)
- return true;
- }
- return false;
- }
- public void gerarEnderecos()
- {
- for (int i = 0; i < i_qntChaves; i++)
- {
- int i_valor = i_vetor[i] % i_qntChaves;
- i_endereco[i] = i_valor;
- str_colisao[i] = "";
- if (i > 0)
- {
- for (int j = 0; j < i; j++)
- {
- if (i_endereco[j] == i_valor)
- {
- str_colisao[i] = "colisão";
- j = i;
- }
- }
- }
- }
- }
- public boolean testaIgualEndereco(int valor, int indice)
- {
- for(int i=0; i < i_qntChaves;i++)
- {
- if (i != indice)
- if (i_endereco[i] == valor)
- return true;
- }
- return false;
- }
- public void calculaEndereco()
- {
- gerarEnderecos();
- System.out.println("--------------------------------------------");
- System.out.println("| Tabela de Endereços |");
- System.out.println("--------------------------------------------");
- System.out.println("| Nº Sorteado | Endereço | Observação|");
- System.out.println("--------------------------------------------");
- for(int i = 0; i< i_qntChaves;i++)
- {
- //System.out.printf("%6d %12d %15s\n", i_vetor[i],i_endereco[i],str_colisao[i]);
- if(str_colisao[i].equalsIgnoreCase("colisão"))
- {
- System.out.printf("%1s %7d %8s %5d %6s %5s %4s\n","|", i_vetor[i],"|",i_endereco[i],"|",str_colisao[i],"|");
- System.out.println("--------------------------------------------");
- }
- else
- {
- System.out.printf("%1s %7d %8s %5d %6s %3s %8s\n","|", i_vetor[i],"|",i_endereco[i],"|",str_colisao[i],"|");
- System.out.println("--------------------------------------------");
- }
- }
- }
- private void preencherTabela()
- {
- for (int i = 0; i < i_qntChaves;i++)
- {
- this.i_tabela[i] = -1;
- }
- }
- public void tratarColisao()
- {
- preencherTabela();
- int i_index = 0;
- System.out.println("---------------------------------");
- System.out.println("| Tratamento de Colisões |");
- System.out.println("---------------------------------");
- System.out.println("| Nº do Nome | Novo Endereþo |");
- System.out.println("---------------------------------");
- while (i_index < i_indice)
- {
- if (this.i_tabela[this.i_endereco[i_index]] == -1)
- {
- System.out.printf(
- "%1s %6d %7s %7d %8s\n","|",i_vetor[i_index],"|", this.i_endereco[i_index],"|");
- System.out.println("---------------------------------");
- this.i_tabela[this.i_endereco[i_index]] = this.i_vetor[i_index];
- i_index++;
- }
- else
- {
- int i_aux = i_endereco[i_index] + 1;
- boolean inseriu = false;
- while (inseriu!= true)
- {
- if (i_aux == i_indice)
- {
- i_aux = 0;
- }
- else if (this.i_tabela[i_aux] == -1)
- {
- System.out.printf("%1s %6d %7s %7d %8s\n","|",i_vetor[i_index],"|", i_aux,"|");
- System.out.println("---------------------------------");
- this.i_tabela[i_aux] = this.i_vetor[i_index];
- i_index++;
- inseriu = true;
- }
- i_aux++;
- }
- }
- }
- }
- public void chavesNome(int qntChaves)
- {
- int soma_Nome = 0;
- System.out.println("----------------------------------------------------------------------------");
- System.out.println("| Tabela de Nomes |");
- System.out.println("----------------------------------------------------------------------------");
- System.out.println("| Sequencia | Nome | Valor do Nome | Chaves | Observação |");
- System.out.println("----------------------------------------------------------------------------");
- while (this.i_qntChaves < qntChaves)
- {
- String nome = JOptionPane.showInputDialog("Informe o nome:");
- for(int i=0;i<nome.length();i++)
- {
- soma_Nome += (nome.charAt(i)*1);
- }
- if(testaIgual(soma_Nome) == false)
- {
- i_vetor[this.i_indice] = soma_Nome;
- System.out.printf("%1s %6d %7s %12s %6s %8d %6s %5d %3s %13s\n","|", this.i_indice,"|",nome,"|",soma_Nome,"|",i_chaves,"|","|");
- System.out.println("----------------------------------------------------------------------------");
- this.i_qntChaves++;
- i_chaves ++;
- }else
- {
- System.out.printf("%1s %6d %7s %12s %6s %8d %6s %4d %3s %3s %3s\n","|",this.i_indice,"|",nome,"|",soma_Nome,"|",i_chaves,"|","Já Existe","|","|","|");
- System.out.println("----------------------------------------------------------------------------");
- }
- this.i_indice++;
- soma_Nome=0;
- }
- }
- public static void main(String[] args)
- {
- Hash e = new Hash();
- int menu;
- do {
- menu = Integer.parseInt(JOptionPane.showInputDialog("Menu\n1 - Gerar Chaves Aleatorias\n2 - Calcular Endereço\n3 - Tratar Colisão\n4 - Gerar chaves pelo Nome\n0 - SAIR"));
- switch (menu) {
- case 1:int valor = Integer.parseInt(JOptionPane.showInputDialog("Informe a quantidade de chave:"));
- // valor+=1;
- // int i=2;
- // do{
- // if(valor % i != 0)
- // {
- // i=0;
- // }
- // i++;
- // valor+=1;
- // }while (i!=0);
- int i_qntChaves = valor;
- e.gerarChaves(i_qntChaves);
- break;
- case 2:
- e.calculaEndereco();
- break;
- case 3:e.tratarColisao();
- break;
- case 4:int qnt_Nome = Integer.parseInt(JOptionPane.showInputDialog("Informe a quantidade de chave:"));
- e.chavesNome(qnt_Nome);
- break;
- case 0:
- break;
- default:
- JOptionPane.showMessageDialog(null, "opção Inválida:");
- break;
- }
- } while (menu != 0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement