Nadesłany przez Tomasz Lubiński, 20 listopada 2019 21:36
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
BarCode.java:
/** * @author Tomasz Lubinski * (c)2019 algorytm.org * * BC412 Bar Code */ public class BarCode { private static final char bc412chars[] = { '0', 'R', '9', 'G', 'L', 'V', 'H', 'A', '8', 'E', 'Z', '4', 'N', 'T', 'S', '1', 'J', '2', 'Q', '6', 'C', '7', 'D', 'Y', 'K', 'B', 'U', 'I', 'X', '3', 'F', 'W', 'P', '5', 'M' }; private static final int bc412spaces[][] = { {1, 1, 1, 5}, {3, 1, 2, 2}, {1, 3, 1, 3}, {2, 1, 2, 3}, {2, 2, 3, 1}, {3, 3, 1, 1}, {2, 1, 3, 2}, {1, 3, 2, 2}, {1, 2, 4, 1}, {1, 5, 1, 1}, {5, 1, 1, 1}, {1, 1, 5, 1}, {2, 3, 2, 1}, {3, 2, 1, 2}, {3, 1, 2, 2}, {1, 1, 2, 4}, {2, 2, 1, 3}, {1, 1, 3, 3}, {3, 1, 1, 3}, {1, 2, 2, 3}, {1, 4, 1, 2}, {1, 2, 3, 2}, {1, 4, 2, 1}, {4, 2, 1, 1}, {2, 2, 2, 2}, {1, 3, 3, 1}, {3, 2, 2, 1}, {2, 1, 4, 1}, {4, 1, 2, 1}, {1, 1, 4, 2}, {2, 1, 1, 4}, {4, 1, 1, 2}, {2, 4, 1, 1}, {1, 2, 1, 4}, {2, 3, 1, 2} }; private static final int start[] = {2}; private static final int stop[] = {1}; /** * Check that given txt can be coded * @param txt * @return true txt is correct, false otherwise */ public boolean check(String txt) { for (int i=0; i<txt.length(); i++) { if (getLetterValue(txt.charAt(i)) == -1) return false; } return true; } /** * Return character value * @param ch * @return character value, or -1 f character cannot be coded */ private int getLetterValue(char ch) { for (int i=0; i<bc412chars.length; i++) { if (bc412chars[i] == ch) return i; } return -1; } /** * Return bars for a given character * @param ch - character to code * @return bars for a given character */ public int[] getBars(char ch) { for (int i=0; i<bc412chars.length; i++) { if (bc412chars[i] == ch) return bc412spaces[i]; } return null; } /** * Get start bars * @return start bars */ public int[] getStartBars() { return start; } /** * Get stop bars * @return stop bars */ public int[] getStopBars() { return stop; } /** * Calculate check digit for a given txt * @param txt * @return check digit for a given txt */ public char checkDigit(String txt) { int sum = 0; for (int i = 0; i < txt.length(); i++) sum += getLetterValue(txt.charAt(i)); return bc412chars[(sum % 35)]; } }