Nadesłany przez Tomasz Lubiński, 11 sierpnia 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)2007 algorytm.org
*
* MSI Bar Code
*/
public class BarCode {
private static final char mod10values[] = {0, 1, 4, 3, 8, 5, 3, 7, 7, 9, 2, 11, 6, 13, 10, 15};
private static final char MSIsigns[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final char code11bars[][] = {
{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}
};
private static final char start[] = {
1, 1, 0
};
private static final char stop[] = {
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<MSIsigns.length; i++) {
if (MSIsigns[i] == ch)
return true;
}
return false;
}
public char[] getBars(char ch) {
for (int i=0; i<MSIsigns.length; i++) {
if (MSIsigns[i] == ch)
return code11bars[i];
}
return null;
}
public char[] getStartBars() {
return start;
}
public char[] getStopBars() {
return stop;
}
//returns letter value for check digit
private int getLetterValue(char ch) {
for (int i=0; i<MSIsigns.length; i++)
if (MSIsigns[i] == ch)
return i;
return 0;
}
public boolean checkCheckDigit(String txt, int checkDigitType) {
int len = txt.length();
// check check digits
switch (checkDigitType) {
case 1:
if (getLetterValue(txt.charAt(len-1)) != mod10checkDigit(txt, len-1))
{
return false;
}
break;
case 2:
if (getLetterValue(txt.charAt(len-2)) != mod10checkDigit(txt, len-2) ||
getLetterValue(txt.charAt(len-1)) != mod10checkDigit(txt, len-1))
{
return false;
}
break;
case 3:
if (getLetterValue(txt.charAt(len-1)) != mod11checkDigit(txt, len-1))
{
return false;
}
break;
case 4:
if (getLetterValue(txt.charAt(len-2)) != mod11checkDigit(txt, len-2) ||
getLetterValue(txt.charAt(len-1)) != mod10checkDigit(txt, len-1))
{
return false;
}
break;
}
return true;
}
//calculate modulo 10 check digit
private int mod10checkDigit (String code, int length)
{
int sum = 0;
for (int i=0; i<length; i++)
{
sum += mod10values[getLetterValue(code.charAt(i))];
}
sum %= 10;
sum = 10-sum;
sum %= 10;
return sum;
}
//calculate modulo 11 check digit
private int mod11checkDigit (String code, int length)
{
int sum = 0;
int w = 0;
for (int i=length-1; i>=0; i--)
{
sum += getLetterValue(code.charAt(i)) * (w + 2);
w++;
w %= 6;
}
sum %= 11;
sum = 11-sum;
sum %= 11;
return sum;
}
}

