Nadesłany przez Tomasz Lubiński, 20 września 2006 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 93 Bar Code
*/
public class BarCode {
private static final char special_1 = 127 + 1; // ($)
private static final char special_2 = 127 + 2; // (%)
private static final char special_3 = 127 + 3; // (/)
private static final char special_4 = 127 + 4; // (+)
private static final char code93signs[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$',
'/', '+', '%', special_1, special_2, special_3, special_4};
private static final char code93bars[][] = {
{1, 0, 0, 0, 1, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 0, 0},
{1, 1, 1, 0, 1, 0, 0, 1, 0},
{1, 1, 1, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 1, 0},
{1, 0, 1, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 1, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 0, 1, 0}
};
private static final char start_stop[] = {
1, 0, 1, 0, 1, 1, 1, 1, 0
};
private static final char termination_bar[] = {
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<code93signs.length - 4; i++) {
if (code93signs[i] == ch)
return true;
}
return false;
}
public char[] getBars(char ch) {
for (int i=0; i<code93signs.length; i++) {
if (code93signs[i] == ch)
return code93bars[i];
}
return null;
}
public char[] getStartStopBars() {
return start_stop;
}
public char[] getTermination_bar() {
return termination_bar;
}
//returns letter value for check digit
private int getLetterValue(char ch) {
for (int i=0; i<code93signs.length; i++)
if (code93signs[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 % 20) + 1));
w++;
}
return (sum % 47);
}
// return check digit K
private int checkDigitK(String txt) {
int sum = checkDigitC(txt);
int w = 1;
for (int i=txt.length()-1; i>=0; i--)
{
sum += (getLetterValue(txt.charAt(i)) * ((w % 15) + 1));
w++;
}
return (sum % 47);
}
public char[] getCheckDigitBarsC(String txt) {
return code93bars[checkDigitC(txt)];
}
public char[] getCheckDigitBarsK(String txt) {
return code93bars[checkDigitK(txt)];
}
}

