Nadesłany przez Tomasz Lubiński, 30 marca 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.
Kody Kreskowe - Delphi/Unit1.pas:
//---------------------------------------------------------------------------
// Generowanie kodow kreskowych Standard Code 2 of 5
// www.algorytm.org
// (c)2007 Tomasz Lubinski
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Kod: TEdit;
procedure Button1Click(Sender: TObject);
private
curentPos: Integer;
procedure DrawBars(txt: String);
procedure DrawSignBars(bars: array of Byte);
function Check(txt: String): Boolean;
function CheckLetter(ch: Char): Boolean;
function GetLetterValue(ch: Char) : Integer;
function CheckDigit(txt: String): Integer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
codeS25sign: array[0..9] of char = (
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
codeS25bars: array[0..9] of array[0..13] of byte = (
(1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0),
(1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0),
(1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0),
(1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0),
(1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0),
(1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0),
(1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0),
(1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0),
(1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0),
(1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0)
);
start: array[0..7] of byte = (
1, 1, 0, 1, 1, 0, 1, 0
);
stop: array[0..7] of byte = (
1, 1, 0, 1, 0, 1, 1, 0
);
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
if Check(Kod.Text) = False then
begin
ShowMessage('Incorrect text');
exit;
end;
DrawBars(Kod.Text);
end;
function TForm1.Check(txt: String): Boolean;
var
i: Integer;
res: Boolean;
begin
res := True;
for i:=1 to Length(txt) do
if (CheckLetter(txt[i])) = False then
begin
res := False;
break;
end;
Result := res;
end;
function TForm1.CheckLetter(ch: Char): Boolean;
var
i: Integer;
res: Boolean;
begin
res := False;
for i:=Low(codeS25sign) to High(codeS25sign) do
if codeS25sign[i] = ch then
begin
res := True;
break;
end;
Result := res;
end;
// returns letter value for check digit
function TForm1.GetLetterValue(ch: Char) : Integer;
var
i: Integer;
begin
Result := 0;
for i:=Low(codeS25sign) to High(codeS25sign) do
if (codeS25sign[i] = ch) then
begin
Result := i;
break;
end;
end;
// return check digit
function TForm1.CheckDigit(txt: String): Integer;
var
i, sum, w1, w2 : Integer;
begin
sum := 0;
w1 := 0;
w2 := 0;
for i:=1 to length(txt) do
begin
if (i mod 2) = 0 then
w1 := w1 + getLetterValue(txt[i])
else
w2 := w2 + getLetterValue(txt[i]);
end;
if (length(txt)) mod 2 = 0 then
w1 := w1 * 3
else
w2 := w2 * 3;
sum := w1 + w2;
sum := sum mod 10;
sum := 10 - sum;
sum := sum mod 10;
Result := sum;
end;
procedure TForm1.DrawBars(txt: String);
var
i, j, c, k : Integer;
begin
curentPos := 0;
Image1.Canvas.Brush.Color := clWhite;
Image1.Canvas.Rectangle(0, 0, 441, 121);
//print start character
drawSignBars(start);
//print characters
for i:=1 to Length(txt) do
for j:=0 to 9 do
if codeS25sign[j] = txt[i] then
begin
drawSignBars(codeS25bars[j]);
break;
end;
//print check digit
drawSignBars(codeS25bars[CheckDigit(txt)]);
//print stop character
drawSignBars(stop);
//Draw text
Image1.Canvas.Font.Size := 10;
Image1.Canvas.Brush.Color := clWhite;
Image1.Canvas.Font.Color := clBlack;
Image1.Canvas.TextOut(curentPos div 2 + 20 - Length(txt)*3, 100, txt);
end;
procedure TForm1.DrawSignBars(bars: array of Byte);
var
i : Integer;
begin
Image1.Canvas.Brush.Color := clBlack;
//print bars
for i:=low(bars) to high(bars) do
begin
if (bars[i] = 1) then
Image1.Canvas.Rectangle(curentPos + 20, 10, curentPos + 22, 100);
curentPos := curentPos + 2;
end;
end;
end.

