Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta charset="utf-8">
- <title>Hemming code</title>
- </head>
- <body>
- <h1>Код Хэмминга</h1>
- <input type='text' id='input_mes' maxlength='4' />собственно вводимый текст
- <br /><br />
- <input type='button' value='Code' onclick='code()' />
- <br /><br />
- <input type='text' id='code_mes' maxlength = '7' />собственно закодированный текст
- <br /><br />
- <input type='button' value='Decode' onclick='decode()' />
- <br /><br />
- <input type='text' id='decode_mes' maxlength='4' />собственно декодированный текст
- <br /><br />
- <input type="text" id="mistake" />Ошибочка
- </body>
- <script>
- document.getElementById('input_mes').onkeypress = function (e){
- e = e || event;
- if (e.ctrlKey || e.altKey || e.metaKey){
- return;
- }
- var chr = getChar(e);
- if (chr == null){
- return;
- }
- if (chr < '0' || chr > '1'){
- return false;
- }
- }
- document.getElementById('code_mes').onkeypress = function(e) {
- e = e || event;
- if (e.ctrlKey || e.altKey || e.metaKey){
- return;
- }
- var chr = getChar(e);
- if (chr == null){
- return;
- }
- if (chr < '0' || chr > '1'){
- return false;
- }
- }
- document.getElementById('decode_mes').onkeypress = function(e){
- return false;
- }
- function getChar(event) {
- if (event.which == null){
- if (event.keyCode < 32){
- return null;
- }
- return String.fromCharCode(event.keyCode);
- }
- if (event.which != 0 && event.charCode != 0){
- if (event.which < 32){
- return null;
- }
- return String.fromCharCode(event.which);
- }
- return null;
- }
- function code() {
- str = document.all.input_mes.value;
- if (str.length < 4){
- alert('братишка, введи ещё раз, надо стоку длины 4');
- return;
- }
- mas = str.split('');
- for (i = 0; i < mas.length; i++){
- mas[i] = parseInt(mas[i]);
- }
- mas.push(((mas[0] + mas[2] + mas[3])%2).toString());
- mas.push(((mas[0] + mas[1] + mas[3])%2).toString());
- mas.push(((mas[1] + mas[2] + mas[3])%2).toString());
- str += mas[4] + mas[5] + mas[6];
- document.all.code_mes.value = str;
- }
- function decode() {
- str = document.all.code_mes.value;
- mas = str.split('');
- temp = str.split('');
- if (str.length < 7){
- alert('длина строки меньше чем семь, тут что-то не так');
- return;
- }
- for(i = 0; i < mas.length; i++){
- mas[i] = parseInt(mas[i]);
- }
- var sum = [];
- sum.push((mas[0] + mas[2] + mas[3])%2);
- sum.push((mas[0] + mas[1] + mas[3])%2);
- sum.push((mas[1] + mas[2] + mas[3])%2);
- if (sum[0] != mas[4] && sum[1] != mas[5] && sum[2] != mas[6]){
- mas[3] = (mas[3] + 1)%2;
- }
- else if (sum[0] != mas[4] && sum[1] != mas[5]){
- mas[0] = (mas[0] + 1)%2;
- }
- else if (sum[0] != mas[4] && sum[2] != mas[6]){
- mas[2] = (mas[2] + 1)%2;
- }
- else if (sum[1] != mas[5] && sum[2] != mas[6]){
- mas[1] = (mas[1] + 1)%2;
- }
- str='';
- for(i = 0; i < 4; i++)
- str += mas[i];
- document.all.mistake.value = mistake(mas, temp, sum);
- document.all.decode_mes.value = str;
- }
- function mistake(mas, temp, sum){
- for (i = 0; i < temp.length; i++){
- if (mas[i] != temp[i]){
- k = i;
- k++;
- return 'ошибка в ' + k + ' позиции';
- }
- }
- if (mas[4] != sum[0]){
- return 'ошибка в ' + 5 + ' позиции';
- }
- else if (mas[5] != sum[1]){
- return 'ошибка в ' + 6 + ' позиции';
- }
- else if (mas[6] != sum[2]){
- return 'ошибка в ' + 7 + ' позиции';
- }
- else{
- return 'все идёт по плану';
- }
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement