Advertisement
Arzybek

Hamming

Nov 8th, 2017
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3.     <title>Хэмминг</title>
  4. </head>
  5. <body>
  6.     <h1>Кодирование</h1>
  7.     <input type='text' id='inp' maxlength='4'>Исходное сообщение<br />
  8.     <input type='button' onclick='encode()' value='кодить'><br />
  9.     <input type='text' id='code' >Закодированное сообщение<br />
  10.     <input type='button' onclick='decode()' value='раскодить'><br />
  11.     <input type='text' id='place' >Место ошибки<br />
  12.     <input type='text' id='truth' >Исходный код <br />
  13. </body>
  14.  
  15. <script>
  16.     function encode(){
  17.         var x = inp.value;
  18.         finArr = new Array();
  19.         finArr = x.split('');
  20.         for(i=0;i<4;i++)
  21.         {
  22.         finArr[i] = parseInt(finArr[i]);
  23.         }
  24.         finArr[4]=(finArr[0]+finArr[1]+finArr[3])%2;
  25.         finArr[5]=(finArr[1]+finArr[2]+finArr[3])%2;
  26.         finArr[6]=(finArr[0]+finArr[2]+finArr[3])%2;
  27.         code.value = finArr.join("");
  28.     }
  29.  
  30.     function decode(){
  31.     var x = code.value;
  32.     c = new Array();
  33.     a = x.split('');
  34.     for (i=0; i<7; i++)
  35.         a[i] = parseInt(a[i]);
  36.     if ((a[4]+a[0]+a[1]+a[3])%2 == 1)
  37.     {
  38.          if ((a[5]+a[1]+a[2]+a[3])%2 == 1)
  39.         {
  40.             if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
  41.             {
  42.                 place.value = '4';
  43.                 n=4;
  44.             }
  45.             else
  46.             {
  47.                 place.value = '2';
  48.                 n=2;
  49.             }
  50.         }
  51.         else
  52.         {
  53.             if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
  54.             {
  55.                 place.value = '1';
  56.                 n=1;
  57.             }
  58.             else
  59.             {
  60.                 place.value = '5';
  61.                 n=5;
  62.             }
  63.         }
  64.     }
  65.     else
  66.     {
  67.         if ((a[5]+a[1]+a[2]+a[3])%2 ==1)
  68.         {
  69.             if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
  70.             {
  71.                 place.value = '3';
  72.                 n=3;
  73.             }
  74.             else {
  75.                 place.value = '6';
  76.                 n=6;
  77.             }
  78.         }
  79.         else  
  80.             if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
  81.                 {
  82.                 place.value = '7';
  83.                 n=7;
  84.                 }
  85.             }
  86.             a[n-1]=(a[n-1]+1)%2;
  87.             var truthArr = a.slice(0,4);
  88.             truth.value=truthArr.join("");
  89.     }
  90.    
  91.     document.getElementById('inp').onkeypress = function(e) {
  92.         e = e || event;
  93.         if (e.ctrlKey || e.altKey || e.metaKey) return;
  94.         var chr = getChar(e);
  95.         /* с null надо осторожно в неравенствах, т.к. например null >= '0' => true!    На всякий случай лучше вынести проверку chr == null отдельно*/
  96.         if (chr == null) return;
  97.         if (chr < '0' || chr > '1') return false;
  98.     }
  99.  
  100.     function getChar(event) {
  101.         if (event.which == null) {
  102.             if (event.keyCode < 32) return null;
  103.             return String.fromCharCode(event.keyCode) // IE
  104.         }
  105.         if (event.which!=0 && event.charCode!=0) {
  106.             if (event.which < 32) return null;
  107.             return String.fromCharCode(event.which)   // остальные
  108.         }
  109.         return null; // специальная клавиша
  110.     }
  111. </script>
  112. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement