Advertisement
Lusien_Lashans

хэмминг

Mar 29th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Hemming code</title>
  6. </head>
  7.  
  8. <body>
  9. <h1>Код Хэмминга</h1>
  10. <input type='text' id='input_mes' maxlength='4' />собственно вводимый текст
  11. <br /><br />
  12. <input type='button' value='Code' onclick='code()' />
  13. <br /><br />
  14. <input type='text' id='code_mes' maxlength = '7' />собственно закодированный текст
  15. <br /><br />
  16. <input type='button' value='Decode' onclick='decode()' />
  17. <br /><br />
  18. <input type='text' id='decode_mes' maxlength='4' />собственно декодированный текст
  19. <br /><br />
  20. <input type="text" id="mistake" />Ошибочка
  21. </body>
  22.  
  23. <script>
  24. document.getElementById('input_mes').onkeypress = function (e){
  25. e = e || event;
  26.  
  27. if (e.ctrlKey || e.altKey || e.metaKey){
  28. return;
  29. }
  30.  
  31. var chr = getChar(e);
  32.  
  33. if (chr == null){
  34. return;
  35. }
  36.  
  37. if (chr < '0' || chr > '1'){
  38. return false;
  39. }
  40. }
  41.  
  42. document.getElementById('code_mes').onkeypress = function(e) {
  43. e = e || event;
  44.  
  45. if (e.ctrlKey || e.altKey || e.metaKey){
  46. return;
  47. }
  48.  
  49. var chr = getChar(e);
  50.  
  51. if (chr == null){
  52. return;
  53. }
  54.  
  55. if (chr < '0' || chr > '1'){
  56. return false;
  57. }
  58. }
  59.  
  60. document.getElementById('decode_mes').onkeypress = function(e){
  61. return false;
  62. }
  63.  
  64. function getChar(event) {
  65. if (event.which == null){
  66. if (event.keyCode < 32){
  67. return null;
  68. }
  69.  
  70. return String.fromCharCode(event.keyCode);
  71. }
  72.  
  73. if (event.which != 0 && event.charCode != 0){
  74. if (event.which < 32){
  75. return null;
  76. }
  77.  
  78. return String.fromCharCode(event.which);
  79. }
  80.  
  81. return null;
  82. }
  83.  
  84. function code() {
  85. str = document.all.input_mes.value;
  86.  
  87. if (str.length < 4){
  88. alert('братишка, введи ещё раз, надо стоку длины 4');
  89. return;
  90. }
  91.  
  92. mas = str.split('');
  93.  
  94. for (i = 0; i < mas.length; i++){
  95. mas[i] = parseInt(mas[i]);
  96. }
  97.  
  98. mas.push(((mas[0] + mas[2] + mas[3])%2).toString());
  99. mas.push(((mas[0] + mas[1] + mas[3])%2).toString());
  100. mas.push(((mas[1] + mas[2] + mas[3])%2).toString());
  101.  
  102. str += mas[4] + mas[5] + mas[6];
  103. document.all.code_mes.value = str;
  104. }
  105.  
  106. function decode() {
  107. str = document.all.code_mes.value;
  108. mas = str.split('');
  109. temp = str.split('');
  110.  
  111. if (str.length < 7){
  112. alert('длина строки меньше чем семь, тут что-то не так');
  113. return;
  114. }
  115.  
  116. for(i = 0; i < mas.length; i++){
  117. mas[i] = parseInt(mas[i]);
  118. }
  119.  
  120. var sum = [];
  121.  
  122. sum.push((mas[0] + mas[2] + mas[3])%2);
  123. sum.push((mas[0] + mas[1] + mas[3])%2);
  124. sum.push((mas[1] + mas[2] + mas[3])%2);
  125.  
  126. if (sum[0] != mas[4] && sum[1] != mas[5] && sum[2] != mas[6]){
  127. mas[3] = (mas[3] + 1)%2;
  128. }
  129. else if (sum[0] != mas[4] && sum[1] != mas[5]){
  130. mas[0] = (mas[0] + 1)%2;
  131. }
  132. else if (sum[0] != mas[4] && sum[2] != mas[6]){
  133. mas[2] = (mas[2] + 1)%2;
  134. }
  135. else if (sum[1] != mas[5] && sum[2] != mas[6]){
  136. mas[1] = (mas[1] + 1)%2;
  137. }
  138.  
  139. str='';
  140.  
  141. for(i = 0; i < 4; i++)
  142. str += mas[i];
  143.  
  144. document.all.mistake.value = mistake(mas, temp, sum);
  145.  
  146. document.all.decode_mes.value = str;
  147. }
  148.  
  149. function mistake(mas, temp, sum){
  150. for (i = 0; i < temp.length; i++){
  151. if (mas[i] != temp[i]){
  152. k = i;
  153. k++;
  154. return 'ошибка в ' + k + ' позиции';
  155. }
  156. }
  157.  
  158. if (mas[4] != sum[0]){
  159. return 'ошибка в ' + 5 + ' позиции';
  160. }
  161. else if (mas[5] != sum[1]){
  162. return 'ошибка в ' + 6 + ' позиции';
  163. }
  164. else if (mas[6] != sum[2]){
  165. return 'ошибка в ' + 7 + ' позиции';
  166. }
  167. else{
  168. return 'все идёт по плану';
  169. }
  170. }
  171. </script>
  172.  
  173. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement