Advertisement
Marin126

SINCLAVE IA Entregable

Apr 2nd, 2020
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. /*
  7. * To change this license header, choose License Headers in Project Properties.
  8. * To change this template file, choose Tools | Templates
  9. * and open the template in the editor.
  10. */
  11. /**
  12. *
  13. * @author anton
  14. */
  15. public class SinClave {
  16.  
  17. public static void main(String[] args) {
  18. HashMap<String, Integer> numLetras = new HashMap<>();
  19. String nombreFicheroEntrada = (args[0]).concat(".cfr");
  20.  
  21. String nombreFicheroSalida = nombreFicheroEntrada.concat(".uncfr");
  22.  
  23. String texto = "";
  24. File f;
  25. f = new File(nombreFicheroEntrada);
  26.  
  27. Scanner scFile = null;
  28.  
  29. File fw = new File(nombreFicheroSalida);
  30. int clave = leoFicheroSacoClave(f, scFile, texto, numLetras) * -1;
  31.  
  32. Utils.leoYEscribo(f, clave, nombreFicheroSalida);
  33.  
  34. }
  35.  
  36. public static boolean esLetra(String letra) {
  37.  
  38. boolean resultado = false;
  39.  
  40. if (letra.charAt(0) >= 'a' && letra.charAt(0) <= 'z'
  41. || letra.charAt(0) >= 'A' && letra.charAt(0) <= 'Z') {
  42.  
  43. resultado = true;
  44.  
  45. } else {
  46. resultado = false;
  47. }
  48.  
  49. return resultado;
  50. }
  51.  
  52. public static int getClave(HashMap<String, Integer> numLetras) {
  53. int clave = 0;
  54. int vigente = 0;
  55. int cantidadActual = 0;
  56. String letraMas = "";
  57. for (String c : numLetras.keySet()) {
  58. cantidadActual = numLetras.get(c);
  59. if (cantidadActual > vigente) {
  60. vigente = cantidadActual;
  61. letraMas = c;
  62. }
  63.  
  64. // cifrado.append((char) (texto.charAt(i) + clave - 26))
  65. }
  66. clave = ((char) (letraMas.toLowerCase().charAt(0)) - 101);
  67.  
  68. return clave;
  69. }
  70.  
  71. public static void cuentaLetras(HashMap<String, Integer> numLetras, String linea) {
  72. String letra = "";
  73. for (int i = 0; i < linea.length(); i++) {
  74.  
  75. letra = Character.toString(linea.toLowerCase().charAt(i));
  76. if (esLetra(letra) == true) {
  77.  
  78. if (!numLetras.containsKey(letra)) {
  79. numLetras.put(letra.toLowerCase(), 1);
  80. } else {
  81. numLetras.put(letra.toLowerCase(), numLetras.get(letra) + 1);
  82. }
  83. }
  84. }
  85. }
  86.  
  87. public static int leoFicheroSacoClave(File f, Scanner scFile, String entrada,
  88. HashMap<String, Integer> numLetras) {
  89. int superClave = 0;
  90.  
  91. try {
  92. if (f.exists()) {
  93. {
  94. scFile = new Scanner(f);
  95. while (scFile.hasNextLine()) {
  96. entrada = scFile.nextLine();
  97. cuentaLetras(numLetras, entrada);
  98. }
  99. superClave = getClave(numLetras);
  100. }
  101.  
  102. }
  103.  
  104. } catch (Exception e) {
  105. System.out.println(e.toString());
  106. } finally {
  107. if (scFile != null) {
  108. scFile.close();
  109. }
  110.  
  111. }
  112. return superClave;
  113. }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement