Advertisement
palmerstone

Untitled

Apr 21st, 2013
2,672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package hammingdistance;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.math.*;
  6.  
  7. public class Main {
  8.     public static String toBinary(int x){
  9.         StringBuilder str = new StringBuilder("");
  10.         for (; ;){
  11.             if ((x & 1) == 1) str = str.append("1");
  12.             else str = str.append("0");
  13.             x >>= 1;
  14.             if (x == 0) break;
  15.         }
  16.  
  17.         StringBuilder res = new StringBuilder("");
  18.         for (int j = 4 - str.toString().length(); j > 0; j--) res = res.append("0");
  19.         res = res.append(str.reverse());
  20.  
  21.         return (res.toString());
  22.     }
  23.  
  24.     public static String generate(int i){
  25.             String str = toBinary(i);
  26.             int p1 = (((int)str.charAt(0) + (int)str.charAt(1) + (int)str.charAt(2)) & 1) + 48;
  27.             int p2 = (((int)str.charAt(0) + (int)str.charAt(1) + (int)str.charAt(3)) & 1) + 48;
  28.             int p3 = (((int)str.charAt(1) + (int)str.charAt(2) + (int)str.charAt(3)) & 1) + 48;
  29.  
  30.             StringBuilder res = new StringBuilder(str);
  31.             res = res.append((char)p1);
  32.             res = res.append((char)p2);
  33.             res = res.append((char)p3);
  34.  
  35.             String bin = res.toString();
  36.             return bin;
  37.     }
  38.  
  39.     public static int Hamming_Distance(String a, String b){
  40.         int res = 0;
  41.         for (int j = 0; j < 7; j++){
  42.             if (a.charAt(j) != b.charAt(j)) res++;
  43.         }
  44.  
  45.         return res;
  46.     }
  47.  
  48.     public static void main(String[] args) throws Exception{
  49.         String[] codewords = new String[16];
  50.         for (int i = 0; i < 16; i++){
  51.             codewords[i] = generate(i);
  52.             //System.out.println(codewords[i]);
  53.         }
  54.         //codewords = new String[] {"0000000", "1110000", "1001100", "0111100", "0101010", "1011010", "1100110", "0010110", "1101001", "0011001", "0100101", "1010101", "1000011", "0110011", "0001111", "1111111"};
  55.  
  56.         final int h = 3;
  57.         Scanner sc = new Scanner(new File("C:\\Users\\Public\\Documents\\what the hell\\data.txt"));
  58.         PrintWriter pw = new PrintWriter(new File("C:\\Users\\Public\\Documents\\what the hell\\lol.txt"));
  59.  
  60.         int counter = 0;
  61.         while (sc.hasNext()){
  62.             String str = sc.nextLine();
  63.  
  64.             int len = str.length();
  65.             StringBuilder temp = new StringBuilder("");
  66.             for (int j = 0; j < len; j++){
  67.                 int r = (int)(Math.random() * 100);
  68.                 r = (r % 5);
  69.  
  70.                 if (r == 0){
  71.                     if (str.charAt(j) == '0') temp = temp.append('1');
  72.                     else temp = temp.append('0');
  73.                 }
  74.                 else temp = temp.append(str.charAt(j));
  75.             }
  76.  
  77.             String error = new String(temp);
  78.  
  79.             int c = 0;
  80.             for (int j = 0; j < 16; j++){
  81.                 int dis = Hamming_Distance(codewords[j], error);
  82.                 if (dis < h) c++;
  83.             }
  84.  
  85.             if (c != 1) counter++;
  86.             if (c == 1) System.out.println("No Error");
  87.             else System.out.println("Error!");
  88.         }
  89.  
  90.         System.out.println(counter + "Errors Detected");
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement