Advertisement
Tecnelm

Untitled

Nov 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Code_analyse_frequence
  4. {
  5.     static ArrayList<Character> text_lettre = new ArrayList<Character>();
  6.     public static Scanner read = new Scanner(System.in);
  7.    
  8.     public static ArrayList<Character> antidoublon ( ArrayList<Character> newlist)
  9.     {
  10.         ArrayList <Character> List = new ArrayList<Character>();
  11.         for(int i = 0 ; i < newlist.size(); i++)
  12.         {
  13.             Object o =newlist.get(i);
  14.             if(!List.contains(o))
  15.                 List.add(newlist.get(i));
  16.         }
  17.         return List;
  18.     }
  19.  
  20.     public static ArrayList<Character> antilettreMaj(ArrayList<Character>newlist)
  21.     {
  22.        
  23.         for (int i =0 ; i < newlist.size(); i++) {
  24.             if((int)newlist.get(i) >= 65 && (int)newlist.get(i) <=90 )
  25.             {
  26.                 newlist.set(i,(char)((int)newlist.get(i)+32));
  27.             }
  28.  
  29.         }
  30.         return newlist;
  31.     }
  32.    
  33.     public static void decode(ArrayList<Character>newlist , int decalage)
  34.     {
  35.         String text= "";
  36.         char lettre='\0';
  37.         int nb_charact = newlist.size();
  38.        
  39.         for(int i = 0 ; i<nb_charact ; i++)
  40.         {
  41.             lettre = newlist.get(i);
  42.             if (lettre <97||lettre >122) {
  43.                 text+=(char)lettre;
  44.                 continue;
  45.             }
  46.             if (decalage >0)
  47.             {
  48.                 for (int nb = 0;nb<decalage;nb++)
  49.                 {
  50.                     lettre -= 1;
  51.                     if (lettre <97)
  52.                         lettre=122;
  53.                 }
  54.                 text+=(char)lettre;
  55.             }
  56.             else
  57.             {
  58.                 for (int nb = 0;nb<-decalage;nb++)
  59.                 {
  60.                     lettre += 1;
  61.                     if (lettre >122)
  62.                         lettre=97;
  63.                 }
  64.                 text+=(char)lettre;
  65.             }
  66.         }
  67.         System.out.println(text);
  68.  
  69.        
  70.        
  71.     }
  72.    
  73.     public static void code(ArrayList<Character>newlist,int decalage)
  74.     {
  75.         String text= "";
  76.         char lettre='\0';
  77.         int nb_charact = newlist.size();
  78.        
  79.         for(int i = 0 ; i<nb_charact ; i++)
  80.         {
  81.             lettre = newlist.get(i);
  82.             if (lettre <97|| lettre >122) {
  83.                 text+=(char)lettre;
  84.                 continue;
  85.             }
  86.             if (decalage >0)
  87.             {
  88.                 for (int nb = 0;nb<decalage;nb++)
  89.                 {
  90.                     lettre+= 1;
  91.                     if (lettre >122)
  92.                         lettre=97;
  93.                 }
  94.                 text+=(char)lettre;
  95.             }
  96.             else
  97.             {
  98.                 for (int nb = 0;nb<-decalage;nb++)
  99.                 {
  100.                     lettre -= 1;
  101.                     if (lettre <97)
  102.                         lettre=122;
  103.                 }
  104.                 text+=(char)lettre;
  105.             }
  106.         }
  107.         System.out.println(text);
  108.     }
  109.    
  110.     public static ArrayList<String> afficherTous(ArrayList<Character>newlist) {
  111.  
  112.         ArrayList<String> texteTraduit = new ArrayList<String>();
  113.         String text= "";
  114.         char lettre='\0';
  115.         int nb_charact = newlist.size();
  116.         for(int ii =0 ; ii<=25 ; ii++)
  117.         {
  118.             for(int i = 0 ; i<nb_charact ; i++)
  119.             {
  120.                 lettre = newlist.get(i);
  121.                 if (lettre <90) {
  122.                     text+=(char)lettre;
  123.                     continue;
  124.                 }
  125.                 for (int nb = 0;nb<=ii;nb++)
  126.                 {
  127.                     lettre += 1;
  128.                     if (lettre >90)
  129.                         lettre=65;
  130.                 }
  131.              text+=(char)lettre;
  132.              
  133.             }
  134.             texteTraduit.add(text);
  135.             text="";
  136.         }
  137.         return texteTraduit;
  138.        
  139.     }
  140.    
  141.     /*public static ArrayList<Character>textList(String texte)
  142.     {
  143.         ArrayList<Character> text_lettre = new ArrayList<Character>();
  144.         int nb_charact = texte.length();
  145.          
  146.          for(int i = 0 ; i<nb_charact ; i++)
  147.              text_lettre.add(i,texte.charAt(i));
  148.          antilettreMaj(text_lettre);
  149.          return text_lettre;
  150.     }*/
  151.    
  152.     public static void AnalyseFreq(ArrayList<Character>newlist)
  153.     {
  154.          int posnbmax=0 , nbmax=0 ,nblettre=0,difference=0;// mot ayant le meme nombre de lettre ;
  155.          char LettreE,lettrefreq;
  156.          boolean test = false ;
  157.          
  158.        
  159.          ArrayList<Character> lettre = new ArrayList<Character>();
  160.        
  161.          lettre.addAll(antidoublon(newlist));
  162.          System.out.println(lettre+"!!!!");
  163.          
  164.         //////////////////////////////////////////////
  165.         //cette partie du code va analiser la frequence des lettre codé
  166.         //////////////////////////////////////////////
  167.          for (int i =0 ;i < lettre.size() ;i++)
  168.           {
  169.              
  170.              for (int ii =0 ;ii<newlist.size() ;ii++)
  171.              {
  172.                 test = lettre.get(i)==newlist.get(ii);
  173.                
  174.                 if(test)
  175.                    nblettre++;
  176.              }
  177.              if(nblettre>nbmax)
  178.              {
  179.                  posnbmax = i;
  180.                  nbmax = nblettre;
  181.              }      
  182.            
  183.             // nb_lettre.add(i,nblettre);
  184.              nblettre = 0;
  185.           }
  186.         //////////////////////////////////////////////
  187.         //cette partie du code va s'occuper de donner la difference avec la lettre la plus commune, et redemander de tenter a chaque fois ou donner l'ordre de tous afficher
  188.         //////////////////////////////////////////////
  189.          
  190.          System.out.println("rentrez la lettre la plus utilisé "+posnbmax);
  191.          lettrefreq= read.nextLine().charAt(0);
  192.          LettreE = lettre.get(posnbmax);
  193.          System.out.println(LettreE+"mlml");
  194.          difference = (int)LettreE-(int)lettrefreq;
  195.          
  196.          System.out.println(difference);
  197.          decode(newlist,difference);
  198.          
  199.     }
  200.    
  201.     static void init() {
  202.        
  203.         System.out.println("rentrez votre texte a traduire");
  204.         String texte = read.nextLine();
  205.         int nb_charact = texte.length();
  206.          
  207.          for(int i = 0 ; i<nb_charact ; i++)
  208.              text_lettre.add(i,texte.charAt(i));
  209.          antilettreMaj(text_lettre);
  210.     }
  211.    
  212. public static void main(String[] args)
  213.     {
  214.         init();
  215.         code(text_lettre,3);    
  216.         read.close();
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement