Nadesłany przez Tomasz Lubiński, 20 grudnia 2011 21: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.
code93.js:
//Code 93
//(c) 2011 by Tomasz Lubinski
//www.algorytm.org
var special_1 = '\u0080'; // ($)
var special_2 = '\u0081'; // (%)
var special_3 = '\u0082'; // (/)
var special_4 = '\u0083'; // (+)
var 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];
var 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]
];
var start_stop = [ 1, 0, 1, 0, 1, 1, 1, 1, 0 ];
var termination_bar = [ 1 ];
/* Check if code is correct */
function check(code93)
{
for (var i=0; i<code93.length; i++)
{
var letterIndex = getLetterIndex(code93[i]);
if (letterIndex == -1 || letterIndex >= code93signs.length - 4)
return false;
}
return true;
}
/* Return letter index, or -1 if not correct */
function getLetterIndex(ch)
{
for (var i=0; i<code93signs.length; i++)
{
if (code93signs[i] == ch)
return i;
}
return -1;
}
/* Return check digit C */
function checkDigitC(code93)
{
var sum = 0;
var w = 0;
for (var i=code93.length-1; i>=0; i--)
{
sum += (getLetterIndex(code93[i]) * ((w % 20) + 1));
w++;
}
return (sum % 47);
}
/* Return check digit K */
function checkDigitK(code93)
{
var sum = checkDigitC(code93);
var w = 1;
for (var i=code93.length-1; i>=0; i--)
{
sum += (getLetterIndex(code93[i]) * ((w % 15) + 1));
w++;
}
return (sum % 47);
}
/* Convert code to bars */
function convert(code93)
{
/* inccorect code return null */
if (check(code93) == false)
{
return null;
}
var bars = new Array(9*(code93.length+4) + 1);
/* add start bars */
for (var j=0; j<9; j++)
{
bars[j] = start_stop[j];
}
/* add character codes */
for (var i=0; i<code93.length; i++)
{
var toCopy = code93bars[getLetterIndex(code93[i])];
for (var j=0; j<9; j++)
{
bars[i*9 + 9 + j] = toCopy[j];
}
}
/* add check digit C */
var toCopy = code93bars[checkDigitC(code93)];
for (var j=0; j<9; j++)
{
bars[(code93.length+1)*9 + j] = toCopy[j];
}
/* add check digit K */
var toCopy = code93bars[checkDigitK(code93)];
for (var j=0; j<9; j++)
{
bars[(code93.length+2)*9 + j] = toCopy[j];
}
/* add stop bars */
for (var j=0; j<9; j++)
{
bars[(code93.length+3)*9 + j] = start_stop[j];
}
/* add termination bar */
bars[(code93.length+4)*9] = termination_bar[0];
return bars;
}
//initialize data
function generate(code93)
{
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
/* clear canvas and set its width */
canvas.width = code93.length*18+93;
/* prepare style */
ctx.fillStyle = "rgb(0,0,0)";
ctx.font = "10pt Arial";
ctx.textBaseline = "top";
ctx.textAlign = "left";
code93 = code93.toUpperCase();
var bars = convert(code93);
if (bars != null)
{
for (i=0; i<bars.length; i++)
{
/* Black bar */
if (bars[i] == 1)
{
ctx.fillRect(10+2*i, 10, 2, 70);
}
}
for (i=0; i<code93.length; i++)
{
ctx.fillText(code93[i], i*18+35, 90);
}
}
else
{
ctx.fillText("Nieprawidłowy", 10, 10);
ctx.fillText("kod", 10, 25);
ctx.fillText("Incorrect", 10, 50);
ctx.fillText("code", 10, 65);
}
}

