Advertisement
Guest User

Hamming Code

a guest
Oct 23rd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.73 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4.     <title>Хэмминг</title>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. </head>
  7. <body>
  8.     <h1>Кодирование по Хэммингу</h1><!-- заголовок на веб странице-->
  9.     <p><input type='text' id='mes' maxlength='4'>Исходное сообщение </p>
  10.     <p><input type='button' onclick='code()' value='кодить'> </p><!--атрибут onclick, значение атрибута - функци-обработчик события click, здесь это функция code-->
  11.     <p><input type='text' id='mes_cod'>Закодированное сообщение </p>
  12.     <p><input type='button' onclick='decode()' value='декодить'> </p>
  13.     <p><input type='text' id='mes_decod'>Раскодированное сообщение </p>
  14. </body>
  15. <script>
  16. function code(){
  17.     /*document.getElementById('mes') возвращает объект -
  18.     объектное представление текстовой ячейки.*/
  19.     message = document.getElementById('mes').value;
  20.     intBits = message.split('');
  21.     // В массиве intBits лежат 4 символа (0 и 1); это не числа, а строки-из-одного-символа
  22.     for(i=0; i<intBits.length; i++)
  23.         intBits[i] = 1*intBits[i];// неявное преобразование строк-из-одного-символа в числа
  24.     intBits.push((intBits[0]+intBits[1]+intBits[3])%2);// Остаток от деления (%) на 2  - это контрльный бит.
  25.     intBits.push((intBits[1]+intBits[2]+intBits[3])%2);
  26.     intBits.push((intBits[0]+intBits[2]+intBits[3])%2);
  27.     //document.getElementById('mes_cod').value = intBits;
  28.     /* неявное преобразование массива к строке - вызывается метод join с разделителем по умолчанию (,).
  29.     Нам "," между цифрами не нужна.*/
  30.     document.getElementById('mes_cod').value = intBits.join('');
  31. }
  32. //mes_decod
  33. function decode(){
  34.     var code = document.getElementById('mes_cod').value;
  35.     intBits = code.split('');
  36.     for(i=0; i<intBits.length; i++)
  37.         intBits[i] = 1*intBits[i];// неявное преобразование строк-из-одного-символа в числа
  38.    
  39.     var sector = [true, true, true];
  40.     sector[0] = (intBits[0]+intBits[1]+intBits[3])%2 ^ intBits[4] == 0;
  41.     sector[1] = (intBits[1]+intBits[2]+intBits[3])%2 ^ intBits[5] == 0;
  42.     sector[2] = (intBits[0]+intBits[2]+intBits[3])%2 ^ intBits[6] == 0;
  43.     var errors = 0;
  44.     if(!sector[0]) ++errors;
  45.     if(!sector[1]) ++errors;
  46.     if(!sector[2]) ++errors;
  47.     if (errors == 3)
  48.         intBits[3] ^= 1;
  49.     else if (errors == 2){
  50.         if (!sector[0] && !sector[1])
  51.             intBits[1] ^= 1;
  52.         else if(!sector[0] && !sector[2])
  53.                 intBits[0] ^= 1;
  54.             else if (!sector[1] && !sector[2])
  55.                     intBits[2] ^= 1;
  56.     }
  57.     document.getElementById('mes_decod').value = intBits.slice(0, 4).join('');
  58. }
  59.  
  60. document.getElementById('mes').onkeypress = function(e) {
  61.     e = e || event;
  62.     if (e.ctrlKey || e.altKey || e.metaKey) return;
  63.     var chr = getChar(e);
  64.     /* с null надо осторожно в неравенствах, т.к. например null >= '0' => true!
  65.     На всякий случай лучше вынести проверку chr == null отдельно*/
  66.     if (chr == null) return;
  67.     if (chr < '0' || chr > '1') return false;
  68. }
  69.  
  70. function getChar(event) {
  71.     if (event.which == null) {
  72.         if (event.keyCode < 32) return null;
  73.         return String.fromCharCode(event.keyCode) // IE
  74.     }
  75.     if (event.which!=0 && event.charCode!=0) {
  76.         if (event.which < 32) return null;
  77.         return String.fromCharCode(event.which)   // остальные
  78.     }
  79.     return null; // специальная клавиша
  80. }
  81. </script>
  82. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement