Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Хэмминг</title>
- </head>
- <body>
- <h1>Кодирование</h1>
- <input type='text' id='inp' maxlength='4'>Исходное сообщение<br />
- <input type='button' onclick='encode()' value='кодить'><br />
- <input type='text' id='code' >Закодированное сообщение<br />
- <input type='button' onclick='decode()' value='раскодить'><br />
- <input type='text' id='place' >Место ошибки<br />
- <input type='text' id='truth' >Исходный код <br />
- </body>
- <script>
- function encode(){
- var x = inp.value;
- finArr = new Array();
- finArr = x.split('');
- for(i=0;i<4;i++)
- {
- finArr[i] = parseInt(finArr[i]);
- }
- finArr[4]=(finArr[0]+finArr[1]+finArr[3])%2;
- finArr[5]=(finArr[1]+finArr[2]+finArr[3])%2;
- finArr[6]=(finArr[0]+finArr[2]+finArr[3])%2;
- code.value = finArr.join("");
- }
- function decode(){
- var x = code.value;
- c = new Array();
- a = x.split('');
- for (i=0; i<7; i++)
- a[i] = parseInt(a[i]);
- if ((a[4]+a[0]+a[1]+a[3])%2 == 1)
- {
- if ((a[5]+a[1]+a[2]+a[3])%2 == 1)
- {
- if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
- {
- place.value = '4';
- n=4;
- }
- else
- {
- place.value = '2';
- n=2;
- }
- }
- else
- {
- if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
- {
- place.value = '1';
- n=1;
- }
- else
- {
- place.value = '5';
- n=5;
- }
- }
- }
- else
- {
- if ((a[5]+a[1]+a[2]+a[3])%2 ==1)
- {
- if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
- {
- place.value = '3';
- n=3;
- }
- else {
- place.value = '6';
- n=6;
- }
- }
- else
- if ((a[6]+a[0]+a[2]+a[3])%2 == 1)
- {
- place.value = '7';
- n=7;
- }
- }
- a[n-1]=(a[n-1]+1)%2;
- var truthArr = a.slice(0,4);
- truth.value=truthArr.join("");
- }
- document.getElementById('inp').onkeypress = function(e) {
- e = e || event;
- if (e.ctrlKey || e.altKey || e.metaKey) return;
- var chr = getChar(e);
- /* с null надо осторожно в неравенствах, т.к. например null >= '0' => true! На всякий случай лучше вынести проверку chr == null отдельно*/
- if (chr == null) return;
- if (chr < '0' || chr > '1') return false;
- }
- function getChar(event) {
- if (event.which == null) {
- if (event.keyCode < 32) return null;
- return String.fromCharCode(event.keyCode) // IE
- }
- if (event.which!=0 && event.charCode!=0) {
- if (event.which < 32) return null;
- return String.fromCharCode(event.which) // остальные
- }
- return null; // специальная клавиша
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement