Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.78 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3.                             Online C Compiler.
  4.                 Code, Compile, Run and Debug C program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12.  
  13. // A  B  C  ..... Z
  14. // 65 66 67 ..... 90
  15. // 75 76 77 ..... 100
  16.  
  17.  
  18. char
  19. decr (char c, int k)
  20. {
  21.   char suma = c + k;
  22.  
  23.   if (suma > 'Z')
  24.     return 'A' + (suma % 'Z') - 1;
  25.   else
  26.     return (char) suma;
  27. }
  28.  
  29. void
  30. decripteaza (char *text, int k)
  31. {
  32.   printf ("Cheia initiala %d:\n", (26 - k) % 26);
  33.   for (int i = 0; i < strlen (text); i++)
  34.     {
  35.       if (text[i] == ' ')
  36.     printf ("%c", text[i]);
  37.       else
  38.     printf ("%c", decr (text[i], k));
  39.     }
  40.   printf ("\n\n");
  41.  
  42. }
  43.  
  44. // ex2
  45.  
  46.  
  47. void
  48. set_corespondente (char *ptr, char c1, char c2)
  49. {
  50.   ptr[c1] = c2;
  51. }
  52.  
  53. void
  54. print_text (char *ptr, char *text)
  55. {
  56.   printf ("Textul descifrat: \n");
  57.   for (int i = 0; i < strlen (text); i++)
  58.     if (text[i] == ' ')
  59.       printf ("%c", text[i]);
  60.     else
  61.       printf ("%c", ptr[text[i]]);
  62.  
  63.  
  64. }
  65.  
  66. int frecvente[26];
  67.  
  68. typedef struct
  69. {
  70.   char litera;
  71.   int frecventa;
  72. } dictionar;
  73.  
  74.  
  75. void initdict(dictionar *d)
  76. {
  77.     for(int i = 0; i < 26; i++)
  78.     {
  79.         d[i].litera = 'A' + i;
  80.         d[i].frecventa = 0;
  81.     }
  82.    
  83. }
  84.  
  85. void setdict(dictionar *d, char c, int frecventa)
  86. {
  87.     int pozitie = c - 'A';
  88.     d[pozitie].frecventa = frecventa;
  89. }
  90.  
  91. void printdict(dictionar *d){
  92.     for(int i = 0; i < 26; i++)
  93.         printf("Caracterul %c: %d\n",d[i].litera,d[i].frecventa);
  94. }
  95.  
  96. void swap(dictionar *d1, dictionar *d2){
  97.     dictionar temp = *d1;
  98.     *d1 = *d2;
  99.     *d2 = temp;
  100. }
  101. void orderdict(dictionar *d){
  102.     for(int i = 0; i < 26; i++)
  103.         for(int j = 0; j < 26; j++)
  104.             if(d[i].frecventa > d[j].frecventa)
  105.                 swap(&d[i],&d[j]);
  106. }
  107. int
  108. main ()
  109. {
  110.   // exercitiul 1
  111.  
  112.   /*char *text = "ESPCP LCP LWDZ WPEEPCD ZQ STD EZ NTNPCZ LD HPWW LD EZ STD TYETXLEPD ZY ACTGLEP LQQLTCD LYO TY ESP WLEEPC TQ SP SLO LYJESTYR NZYQTOPYETLW EZ DLJ SP HCZEP TE TY NTASPC ESLE TD MJ DZ NSLYRTYR ESP ZCOPC ZQ ESP WPEEPCD ZQ ESP LWASLMPE ESLE YZE L HZCO NZFWO MP XLOP ZFE TQ LYJZYP HTDSPD EZ OPNTASPC ESPDP LYO RPE LE ESPTC XPLYTYR SP XFDE DFMDETEFEP ESP QZFCES WPEEPC ZQ ESP LWASLMPE, YLXPWJ O QZC L LYO DZ HTES ESP ZESPCD ESP WTGPD ZQ ESP NLPDLCD WTQP ZQ UFWTFD NLPDLC";
  113.      for(int i = 0; i < 26; i++)
  114.      decripteaza(text,i);
  115.    */
  116.   // exercitiul 2
  117.  
  118.   char *text =
  119.     "KBH XSQXKZKSKZNF DZCBHTX THCMADH HADB MHKKHT IZKB AFNKBHT MHKKHT ADDNTPZFE KN KBH DZCBHT AMCBAQHK DZCBHTX ZF IBZDB KBH DZCBHT AMCBAQHK THUAZFX SFDBAFEHP KBTNSEBNSK KBH UHXXAEH ATH DAMMHP UNFNAMCBAQHKZD XSQXKZKSKZNF DZCBHTX ZO IH CHTUZK KBH DZCBHT AMCBAQHK KN QH AFR THATTAFEHUHFK NO KBH CMAZF AMCBAQHK KBHF IH DAF EHFHTAKH AF HFNTUNSX FSUQHT NO PZXKZFDK UNPHX NO HFDTRCKZNF HADB DZCBHT AMCBAQHK ZX GFNIF AX A GHR ZO NST UHXXAEH ZX ZFKHTDHCKHP QR KBH HFHUR IBN DNTTHDKMR AXXSUHX KBAK IH BAWH SXHP A UNFNAMCBAQHKZD XSQXKZKSKZNF DZCBHT KBHR ATH XKZMM OADHP IZKB KBH ZUCNXXZQMH DBAMMHFEH NO DBHDGZFE AMM CNXXZQMH GHRX ZO AF HFHUR AEHFK DNSMP DBHDG NFH NO KBHXH CNXXZQMH GHRX HWHTR XHDNFP ZK INSMP KAGH TNSEBMR NFH QZMMZNF KZUHX KBH MZOHKZUH NO KBH SFZWHTXH KN DBHDG AMM NO KBHU AFP OZFP KBH DNTTHDK NFH KBZX XZUCMH QTSKH ONTDH ACCTNADB DMHATMR IZMM FNK INTG KBH QMADG DBAUQHT XZUNF XZFEB";
  120.   printf("\n\nTextul cifrat:\n%s\n\n",text);
  121.  
  122.   char corespondente[26];
  123.   char *ptr_corespondente = corespondente - 'A';
  124.   int *ptr_frecvente = frecvente - 'A';
  125.  
  126.   for (int i = 0; i < strlen (text); i++){
  127.     if (text[i] != ' '){
  128.          ptr_frecvente[text[i]]++;
  129.     }
  130.   }
  131.  
  132.   dictionar d[26];
  133.   initdict(&d);    
  134.   for (char c = 'A'; c <= 'Z'; c++){
  135.         setdict(&d,c,ptr_frecvente[c]);
  136.   }
  137.   printdict(&d);
  138.   orderdict(&d);
  139.  
  140.   printf("\n\nDictionarul sortat:\n");
  141.   printdict(&d);
  142.  
  143.   for (char c = 'A'; c <= 'Z'; c++){
  144.       ptr_corespondente[c] = '*';
  145.   }
  146.  
  147.   // https://www.online-utility.org/text/analyzer.jsp - frecv. cuvintelor
  148.  
  149.   // "THE" din "KBH" (apare de 12 ori)
  150.   set_corespondente (ptr_corespondente, 'K', 'T');
  151.   set_corespondente (ptr_corespondente, 'B', 'H');
  152.   set_corespondente (ptr_corespondente, 'H', 'E');
  153.  
  154.   // "LETTER" din "*ETTE*"
  155.   set_corespondente (ptr_corespondente, 'M', 'L');
  156.   set_corespondente (ptr_corespondente, 'T', 'R');
  157.  
  158.   // "REPLACE" din "RE*L**E"
  159.   set_corespondente(ptr_corespondente, 'C', 'P');
  160.   set_corespondente(ptr_corespondente, 'A', 'A');
  161.   set_corespondente(ptr_corespondente, 'D', 'C');
  162.  
  163.   // "ANOTHER" din "A**THER"
  164.   set_corespondente(ptr_corespondente, 'F', 'N');
  165.   set_corespondente(ptr_corespondente, 'N', 'O');
  166.  
  167.   // "THROUGHOUT" din "THRO**HO*T"
  168.   set_corespondente(ptr_corespondente, 'S', 'U');
  169.   set_corespondente(ptr_corespondente, 'E', 'G');
  170.  
  171.   // "SUBSTITUTION" din "*U**T*TUT*ON"
  172.   set_corespondente(ptr_corespondente, 'X', 'S');
  173.   set_corespondente(ptr_corespondente, 'Q', 'B');
  174.   set_corespondente(ptr_corespondente, 'Z', 'I');
  175.  
  176.   // "CHECKING" din "CHEC*ING"
  177.   set_corespondente(ptr_corespondente, 'G', 'K');
  178.  
  179.   // "WITH" din "*ITH"
  180.   set_corespondente(ptr_corespondente, 'I', 'W');
  181.  
  182.   // "UNCHANGED" din "UNCHANGE*"
  183.   set_corespondente(ptr_corespondente, 'P', 'D');
  184.  
  185.   // "HAVE" din "HA*E"
  186.   set_corespondente(ptr_corespondente, 'W', 'V');
  187.  
  188.   // "ENCRYPTION" din "ENCR*PTION"
  189.   set_corespondente(ptr_corespondente, 'R', 'Y');
  190.  
  191.   // "REARRANGEMENT" din "REARRANGE*ENT"
  192.   set_corespondente(ptr_corespondente, 'U', 'M');
  193.  
  194.   // "FACED" din "*ACED", "OF" din "O*"
  195.   set_corespondente(ptr_corespondente, 'O', 'F');
  196.  
  197.   //GATA!
  198.   print_text(ptr_corespondente, text);
  199.   return 0;
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement