Advertisement
Fusty03

Hemming

Dec 10th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.     <head>
  3.         <title>Hemming code</title>
  4.         <script>
  5.             function code ()
  6.             {
  7.                 let inpMes = document.getElementById("input").value;
  8.                 inpMes = inpMes.split('');
  9.                 for (let i in inpMes)
  10.                 {
  11.                     inpMes[i] = parseInt(inpMes[i]);
  12.                 }
  13.                 //Добавляем 3 контрольных бита в конец последовательности
  14.                 inpMes.push((inpMes[0] + inpMes[1] + inpMes[3]) % 2);
  15.                 inpMes.push((inpMes[0] + inpMes[2] + inpMes[3]) % 2);
  16.                 inpMes.push((inpMes[1] + inpMes[2] + inpMes[3]) % 2);
  17.                 document.getElementById("message").value = inpMes.join('');
  18.             }
  19.  
  20.             function decode ()
  21.             {
  22.                 let inpString = document.getElementById("message").value;
  23.                 inpString = inpString.split('');
  24.  
  25.                 for (let i in inpString)
  26.                     inpString[i] = parseInt(inpString[i]);
  27.                 let errors = new Array();
  28.                
  29.                 //Находим места ошибок, сохраняя их номер
  30.                 if ((inpString[0] + inpString[1] + inpString[3]) % 2 != inpString[4])
  31.                     errors.push(0);
  32.                 if ((inpString[0] + inpString[2] + inpString[3]) % 2 != inpString[5])
  33.                     errors.push(1);
  34.                 if ((inpString[1] + inpString[2] + inpString[3]) % 2 != inpString[6])
  35.                     errors.push(2);
  36.                
  37.                 let textAns = '';
  38.  
  39.                 //Если ошибок нет, то возвращаем исходную последовательность
  40.                 if (errors.length == 0)
  41.                 {
  42.                     textAns = "All right!";
  43.                 }
  44.                 //Если 1 ошибка, значит поврежден один из контрольных битов
  45.                 if (errors.length == 1)
  46.                 {
  47.                     let i = errors[0] + 4;
  48.                     inpString[i] = (inpString[i] + 1) % 2;
  49.                     textAns = `Error at index ${i}`;
  50.                 }
  51.                 //Если 2, то находим место пересечения зон с ошибками
  52.                 if (errors.length == 2)
  53.                 {
  54.                     let sum = 0;
  55.                     for (let i of errors)
  56.                         sum += i;
  57.                    
  58.                     sum--;
  59.                     inpString[sum] = (inpString[sum] + 1) % 2;
  60.                     textAns = `Error at index ${sum}`;
  61.                 }
  62.                 //Если 3, то ошибка в бите, который влияет на все контрольные биты
  63.                 if (errors.length == 3)
  64.                 {
  65.                     textAns = 'Error at index 3';
  66.                     inpString[3] = (inpString[3] + 1) % 2;
  67.                 }
  68.  
  69.                 document.getElementById("decode").value = inpString.join('');
  70.                 document.getElementById("answer").value = textAns;
  71.             }
  72.         </script>
  73.     </head>
  74.     <body>
  75.         <h1>Hemming code</h1>
  76.         <input type="text" name="input" id="input">
  77.         <p><input type="button" value="Code" onclick="code()"></p>
  78.         <input type="text" id="message">
  79.         <p><input type="button" value="Decode" onclick="decode()"></p>
  80.         <p><input type="text" name="decode" id="decode"></p>
  81.         <textarea name="answer" id="answer" cols="30" rows="10"></textarea>
  82.     </body>
  83. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement