Advertisement
Guest User

Untitled

a guest
May 28th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.88 KB | None | 0 0
  1.  
  2. package hash;
  3.  
  4. import java.util.Random;
  5. import javax.swing.JOptionPane;
  6. /**
  7.  *
  8.  * @author Damião Cunha
  9.  */
  10. public class Hash
  11. {
  12.     private int[] i_vetor = new int[100];
  13.     private int[] i_endereco = new int[100];
  14.     private int[] i_tabela = new int[100];
  15.     private String[] str_colisao = new String[100];
  16.     private int i_chaves=0, i_qntChaves, i_indice = 0;
  17.  
  18.     public void gerarChaves(int qntChaves)
  19.     {
  20.         Random rand = new Random();
  21.  
  22.         System.out.println("---------------------------------------------------------");
  23.         System.out.println("|                   Tabela de Chaves                    |");
  24.         System.out.println("---------------------------------------------------------");
  25.         System.out.println("|   Sequencia  |  Nº Sorteado  |  Chaves  |  Observação |");
  26.         System.out.println("---------------------------------------------------------");
  27.         while (this.i_qntChaves < qntChaves)
  28.         {
  29.             int aux = rand.nextInt(100);
  30.             if(testaIgual(aux) == false)
  31.             {
  32.                 i_vetor[this.i_indice] = aux;
  33.                 System.out.printf("%1s %6d %7s %7d %7s %5d %4s %13s\n","|", this.i_indice,"|",i_vetor[this.i_indice],"|",i_chaves,"|","|");
  34.                 System.out.println("---------------------------------------------------------");
  35.                 this.i_qntChaves++;
  36.                 i_chaves ++;
  37.             }else
  38.                {
  39.                   System.out.printf("%1s %6d %7s %7d %7s %5d %4s %8s %3s\n","|",this.i_indice,"|",aux,"|",i_chaves,"|","Já Existe","|","|");
  40.                   System.out.println("---------------------------------------------------------");
  41.                }
  42.             this.i_indice++;
  43.         }
  44.     }
  45.  
  46.      public boolean testaIgual(int valor)
  47.     {
  48.         for(int i=0; i < this.i_indice;i++)
  49.         {
  50.             if(i_vetor[i] == valor)
  51.                 return true;
  52.         }
  53.         return false;
  54.     }
  55.  
  56.     public void gerarEnderecos()
  57.     {
  58.         for (int i = 0; i < i_qntChaves; i++)
  59.         {
  60.             int i_valor = i_vetor[i] % i_qntChaves;
  61.             i_endereco[i] = i_valor;
  62.             str_colisao[i] = "";
  63.            
  64.             if (i > 0)
  65.             {
  66.                 for (int j = 0; j < i; j++)
  67.                 {
  68.                     if (i_endereco[j] == i_valor)
  69.                     {
  70.                         str_colisao[i] = "colisão";
  71.                         j = i;
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     public boolean testaIgualEndereco(int valor, int indice)
  79.     {
  80.         for(int i=0; i < i_qntChaves;i++)
  81.         {
  82.             if (i != indice)
  83.                 if (i_endereco[i] == valor)
  84.                     return true;
  85.         }
  86.         return false;
  87.     }
  88.  
  89.      public void calculaEndereco()
  90.      {
  91.          gerarEnderecos();
  92.          System.out.println("--------------------------------------------");
  93.          System.out.println("|            Tabela de Endereços           |");
  94.          System.out.println("--------------------------------------------");
  95.          System.out.println("|   Nº Sorteado  |  Endereço  |  Observação|");
  96.          System.out.println("--------------------------------------------");
  97.          for(int i = 0; i< i_qntChaves;i++)
  98.          {
  99.              //System.out.printf("%6d %12d %15s\n", i_vetor[i],i_endereco[i],str_colisao[i]);
  100.              if(str_colisao[i].equalsIgnoreCase("colisão"))
  101.              {
  102.              System.out.printf("%1s %7d %8s %5d %6s %5s %4s\n","|", i_vetor[i],"|",i_endereco[i],"|",str_colisao[i],"|");
  103.              System.out.println("--------------------------------------------");
  104.              }
  105.              else
  106.              {
  107.                 System.out.printf("%1s %7d %8s %5d %6s %3s %8s\n","|", i_vetor[i],"|",i_endereco[i],"|",str_colisao[i],"|");
  108.                 System.out.println("--------------------------------------------");
  109.              }
  110.          }
  111.      }
  112.  
  113.      private void preencherTabela()
  114.      {
  115.          for (int i = 0; i < i_qntChaves;i++)
  116.          {
  117.              this.i_tabela[i] = -1;
  118.          }
  119.      }
  120.  
  121.  
  122.      public void tratarColisao()
  123.      {
  124.          preencherTabela();
  125.          int i_index = 0;
  126.  
  127.         System.out.println("---------------------------------");
  128.         System.out.println("|    Tratamento de Colisões     |");
  129.         System.out.println("---------------------------------");
  130.         System.out.println("|  Nº do Nome  |  Novo Endereþo |");
  131.         System.out.println("---------------------------------");
  132.  
  133.         while (i_index < i_indice)
  134.         {
  135.             if (this.i_tabela[this.i_endereco[i_index]] == -1)
  136.             {
  137.                 System.out.printf(
  138.                      "%1s %6d %7s %7d %8s\n","|",i_vetor[i_index],"|", this.i_endereco[i_index],"|");
  139.                 System.out.println("---------------------------------");
  140.  
  141.                 this.i_tabela[this.i_endereco[i_index]] = this.i_vetor[i_index];
  142.                 i_index++;
  143.             }
  144.             else
  145.             {
  146.                 int i_aux = i_endereco[i_index] + 1;
  147.                 boolean inseriu = false;
  148.  
  149.                 while (inseriu!= true)
  150.                 {
  151.                     if (i_aux == i_indice)
  152.                     {
  153.                         i_aux = 0;
  154.                     }
  155.                     else if (this.i_tabela[i_aux] == -1)
  156.                     {
  157.                         System.out.printf("%1s %6d %7s %7d %8s\n","|",i_vetor[i_index],"|", i_aux,"|");
  158.                         System.out.println("---------------------------------");
  159.  
  160.                         this.i_tabela[i_aux] = this.i_vetor[i_index];
  161.                         i_index++;
  162.                         inseriu = true;
  163.                     }
  164.                     i_aux++;
  165.                 }
  166.             }
  167.         }
  168.      }
  169.  
  170.  
  171.  
  172.      public void chavesNome(int qntChaves)
  173.      {
  174.          int soma_Nome = 0;
  175.         System.out.println("----------------------------------------------------------------------------");
  176.         System.out.println("|                              Tabela de Nomes                             |");
  177.         System.out.println("----------------------------------------------------------------------------");
  178.         System.out.println("|   Sequencia  |        Nome       | Valor do Nome | Chaves  |  Observação |");
  179.         System.out.println("----------------------------------------------------------------------------");
  180.         while (this.i_qntChaves < qntChaves)
  181.         {
  182.             String nome = JOptionPane.showInputDialog("Informe o nome:");
  183.             for(int i=0;i<nome.length();i++)
  184.             {
  185.              soma_Nome += (nome.charAt(i)*1);
  186.             }
  187.  
  188.             if(testaIgual(soma_Nome) == false)
  189.             {
  190.                 i_vetor[this.i_indice] = soma_Nome;
  191.                 System.out.printf("%1s %6d %7s %12s %6s %8d %6s %5d %3s %13s\n","|", this.i_indice,"|",nome,"|",soma_Nome,"|",i_chaves,"|","|");
  192.                 System.out.println("----------------------------------------------------------------------------");
  193.                 this.i_qntChaves++;
  194.                 i_chaves ++;
  195.             }else
  196.                {
  197.                   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","|","|","|");
  198.                   System.out.println("----------------------------------------------------------------------------");
  199.                }
  200.  
  201.             this.i_indice++;
  202.             soma_Nome=0;
  203.         }
  204.      }
  205.    
  206.  
  207.     public static void main(String[] args)
  208.     {
  209.         Hash e = new Hash();
  210.         int menu;
  211.  
  212.         do {
  213.          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"));
  214.  
  215.          switch (menu) {
  216.             case 1:int valor = Integer.parseInt(JOptionPane.showInputDialog("Informe a quantidade de chave:"));
  217. //            valor+=1;
  218. //            int i=2;
  219. //            do{
  220. //                if(valor % i != 0)
  221. //                {
  222. //                      i=0;
  223. //                }
  224. //                i++;
  225. //                valor+=1;
  226. //            }while (i!=0);
  227.                 int i_qntChaves = valor;
  228.             e.gerarChaves(i_qntChaves);
  229.                break;
  230.  
  231.             case 2:
  232.                 e.calculaEndereco();
  233.                break;
  234.  
  235.              case 3:e.tratarColisao();
  236.                  break;
  237.  
  238.              case 4:int qnt_Nome = Integer.parseInt(JOptionPane.showInputDialog("Informe a quantidade de chave:"));
  239.                 e.chavesNome(qnt_Nome);
  240.                  break;
  241.  
  242.             case 0:
  243.                break;
  244.  
  245.             default:
  246.                JOptionPane.showMessageDialog(null, "opção Inválida:");
  247.                break;
  248.          }
  249.       } while (menu != 0);
  250.        
  251.     }
  252.  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement