Nadesłany przez Tomasz Lubiński, 16 stycznia 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 Code 11 // www.algorytm.org // (c)2006 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; length: Integer); function Check(txt: String): Boolean; function CheckLetter(ch: Char): Boolean; function GetLetterValue(ch: Char) : Integer; function CheckDigitC(txt: String): Integer; function CheckDigitK(txt: String): Integer; { Private declarations } public { Public declarations } end; var Form1: TForm1; code11sign: array[0..10] of char = ( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'); code11bars: array[0..10] of array[0..6] of byte = ( (1, 0, 1, 0, 1, 1, 0), (1, 1, 0, 1, 0, 1, 1), (1, 0, 0, 1, 0, 1, 1), (1, 1, 0, 0, 1, 0, 1), (1, 0, 1, 1, 0, 1, 1), (1, 1, 0, 1, 1, 0, 1), (1, 0, 0, 1, 1, 0, 1), (1, 0, 1, 0, 0, 1, 1), (1, 1, 0, 1, 0, 0, 1), (1, 1, 0, 1, 0, 1, 0), (1, 0, 1, 1, 0, 1, 0) ); code11bars_length: array[0..10] of byte = ( 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6 ); start_stop: array[0..6] of byte = ( 1, 0, 1, 1, 0, 0, 1 ); start_stop_length: byte = 7; 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:=0 to 10 do if code11sign[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:=0 to 10 do if (code11sign[i] = ch) then begin Result := i; break; end; end; // return check digit C function TForm1.CheckDigitC(txt: String): Integer; var i, sum, w : Integer; begin sum := 0; w := 0; for i:=length(txt) downto 1 do begin sum := sum + (getLetterValue(txt[i]) * ((w mod 10) + 1)); w := w+ 1; end; Result := (sum mod 11); end; // return check digit K function TForm1.CheckDigitK(txt: String): Integer; var i, sum, w : Integer; begin sum := 0; w := 0; for i:=length(txt) downto 1 do begin sum := sum + (getLetterValue(txt[i]) * ((w mod 9) + 1)); w := w+ 1; end; Result := (sum mod 11); 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_stop, start_stop_length); //print characters for i:=1 to Length(txt) do for j:=0 to 10 do if code11sign[j] = txt[i] then begin drawSignBars(code11bars[j], code11bars_length[j]); break; end; //print check digit C c := checkDigitC(txt); drawSignBars(code11bars[c], code11bars_length[c]); txt := txt + code11sign[c]; if (length(txt)>9) then begin //print check digit K k := checkDigitK(txt); drawSignBars(code11bars[k], code11bars_length[k]); txt := txt + code11sign[k]; end; //print stop character drawSignBars(start_stop, start_stop_length); //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; length: Integer); var i : Integer; begin Image1.Canvas.Brush.Color := clBlack; //print bars for i:=0 to length-1 do begin if (bars[i] = 1) then Image1.Canvas.Rectangle(curentPos + 20, 10, curentPos + 22, 100); curentPos := curentPos + 2; end; curentPos := curentPos + 2; end; end.