Nadesłany przez Tomasz Lubiński, 28 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
*
* UPCA Bar Code
*/
public class BarCode {
private static final byte right[][] = {
{1, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 0}
};
private static final byte left[][] = {
{0, 0, 0, 1, 1, 0, 1},
{0, 0, 1, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0, 1},
{0, 1, 0, 0, 0, 1, 1},
{0, 1, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 0, 1, 1},
{0, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 1, 0, 1, 1}
};
public byte[] convert(byte UPCA[]) {
/* inccorect length or check sum, return null */
if ((UPCA == null) ||
(UPCA.length != 12) ||
(checkSum(UPCA) == false)) {
return null;
}
byte bars[] = new byte[95];
for (int i=0; i<95; i++) {
bars[i] = 0;
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
for (int i=0; i<6; i++) {
for (int j=0; j<7; j++) {
bars[i*7 + 3 + j] = left[UPCA[i]][j];
}
}
bars[45] = 0;
bars[46] = 1;
bars[47] = 0;
bars[48] = 1;
bars[49] = 0;
for (int i=6; i<12; i++) {
for (int j=0; j<7; j++) {
bars[i*7 + 8 + j] = right[UPCA[i]][j];
}
}
bars[92] = 1;
bars[93] = 0;
bars[94] = 1;
return bars;
}
/**
* calculates checksum of UPCA
*/
private boolean checkSum(byte UPCA[]) {
int sum = 3 * UPCA[0] +
1 * UPCA[1] +
3 * UPCA[2] +
1 * UPCA[3] +
3 * UPCA[4] +
1 * UPCA[5] +
3 * UPCA[6] +
1 * UPCA[7] +
3 * UPCA[8] +
1 * UPCA[9] +
3 * UPCA[10];
sum %= 10;
sum = 10 - sum;
sum %= 10;
if (sum == UPCA[11]) {
return true;
}
else {
return false;
}
}
}

