Nadesłany przez Tomasz Lubiński, 16 stycznia 2007 01:00
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)2006 algorytm.org * * Code 11 Bar Code */ public class BarCode { private static final char code11signs[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'}; private static final char code11bars[][] = { {1, 0, 1, 0, 1, 1}, {1, 1, 0, 1, 0, 1, 1}, {1, 0, 0, 1, 0, 1, 1}, {1, 1, 0, 0, 1, 0, 1}, {1, 0, 1, 1, 0, 1, 1}, {1, 1, 0, 1, 1, 0, 1}, {1, 0, 0, 1, 1, 0, 1}, {1, 0, 1, 0, 0, 1, 1}, {1, 1, 0, 1, 0, 0, 1}, {1, 1, 0, 1, 0, 1}, {1, 0, 1, 1, 0, 1} }; private static final char start_stop[] = { 1, 0, 1, 1, 0, 0, 1 }; public boolean check(String txt) { for (int i=0; i<txt.length(); i++) { if (checkLetter(txt.charAt(i)) == false) return false; } return true; } private boolean checkLetter(char ch) { for (int i=0; i<code11signs.length; i++) { if (code11signs[i] == ch) return true; } return false; } public char[] getBars(char ch) { for (int i=0; i<code11signs.length; i++) { if (code11signs[i] == ch) return code11bars[i]; } return null; } public char[] getStartStopBars() { return start_stop; } //returns letter value for check digit private int getLetterValue(char ch) { for (int i=0; i<code11signs.length; i++) if (code11signs[i] == ch) return i; return 0; } // return check digit C private int checkDigitC(String txt) { int sum = 0; int w = 0; for (int i=txt.length()-1; i>=0; i--) { sum += (getLetterValue(txt.charAt(i)) * ((w % 10) + 1)); w++; } return (sum % 11); } // return check digit K private int checkDigitK(String txt) { int sum = 0; int w = 0; for (int i=txt.length()-1; i>=0; i--) { sum += (getLetterValue(txt.charAt(i)) * ((w % 9) + 1)); w++; } return (sum % 11); } public char[] getCheckDigitBarsC(String txt) { return code11bars[checkDigitC(txt)]; } public char[] getCheckDigitBarsK(String txt) { return code11bars[checkDigitK(txt)]; } public char getCheckDigitC(String txt) { return code11signs[checkDigitC(txt)]; } public char getCheckDigitK(String txt) { return code11signs[checkDigitK(txt)]; } }