Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Hemming code</title>
- <script>
- function code ()
- {
- let inpMes = document.getElementById("input").value;
- inpMes = inpMes.split('');
- for (let i in inpMes)
- {
- inpMes[i] = parseInt(inpMes[i]);
- }
- //Добавляем 3 контрольных бита в конец последовательности
- inpMes.push((inpMes[0] + inpMes[1] + inpMes[3]) % 2);
- inpMes.push((inpMes[0] + inpMes[2] + inpMes[3]) % 2);
- inpMes.push((inpMes[1] + inpMes[2] + inpMes[3]) % 2);
- document.getElementById("message").value = inpMes.join('');
- }
- function decode ()
- {
- let inpString = document.getElementById("message").value;
- inpString = inpString.split('');
- for (let i in inpString)
- inpString[i] = parseInt(inpString[i]);
- let errors = new Array();
- //Находим места ошибок, сохраняя их номер
- if ((inpString[0] + inpString[1] + inpString[3]) % 2 != inpString[4])
- errors.push(0);
- if ((inpString[0] + inpString[2] + inpString[3]) % 2 != inpString[5])
- errors.push(1);
- if ((inpString[1] + inpString[2] + inpString[3]) % 2 != inpString[6])
- errors.push(2);
- let textAns = '';
- //Если ошибок нет, то возвращаем исходную последовательность
- if (errors.length == 0)
- {
- textAns = "All right!";
- }
- //Если 1 ошибка, значит поврежден один из контрольных битов
- if (errors.length == 1)
- {
- let i = errors[0] + 4;
- inpString[i] = (inpString[i] + 1) % 2;
- textAns = `Error at index ${i}`;
- }
- //Если 2, то находим место пересечения зон с ошибками
- if (errors.length == 2)
- {
- let sum = 0;
- for (let i of errors)
- sum += i;
- sum--;
- inpString[sum] = (inpString[sum] + 1) % 2;
- textAns = `Error at index ${sum}`;
- }
- //Если 3, то ошибка в бите, который влияет на все контрольные биты
- if (errors.length == 3)
- {
- textAns = 'Error at index 3';
- inpString[3] = (inpString[3] + 1) % 2;
- }
- document.getElementById("decode").value = inpString.join('');
- document.getElementById("answer").value = textAns;
- }
- </script>
- </head>
- <body>
- <h1>Hemming code</h1>
- <input type="text" name="input" id="input">
- <p><input type="button" value="Code" onclick="code()"></p>
- <input type="text" id="message">
- <p><input type="button" value="Decode" onclick="decode()"></p>
- <p><input type="text" name="decode" id="decode"></p>
- <textarea name="answer" id="answer" cols="30" rows="10"></textarea>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement