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 Interleaved 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); overload; procedure DrawSignBars(bars: array of Char; spaces: array of Char); overload; 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; codeI25sign: array[0..9] of char = ( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); codeI25bars: array[0..9] of array[0..4] of char = ( ('N', 'N', 'W', 'W', 'N'), ('W', 'N', 'N', 'N', 'W'), ('N', 'W', 'N', 'N', 'W'), ('W', 'W', 'N', 'N', 'N'), ('N', 'N', 'W', 'N', 'W'), ('W', 'N', 'W', 'N', 'N'), ('N', 'W', 'W', 'N', 'N'), ('N', 'N', 'N', 'W', 'W'), ('W', 'N', 'N', 'W', 'N'), ('N', 'W', 'N', 'W', 'N') ); start: array[0..3] of byte = ( 1, 0, 1, 0 ); stop: array[0..3] of byte = ( 1, 1, 0, 1 ); 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; if (Length(txt) mod 2) = 0 then res := False; 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(codeI25sign) to High(codeI25sign) do if codeI25sign[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(codeI25sign) to High(codeI25sign) do if (codeI25sign[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, odd, even : 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) div 2 do begin odd := getLetterValue(txt[2*(i-1) + 1]); even := getLetterValue(txt[2*(i-1) + 2]); drawSignBars(codeI25bars[odd], codeI25bars[even]); end; //print the last digit and check digit odd := getLetterValue(txt[Length(txt)]); drawSignBars(codeI25bars[odd], codeI25bars[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; procedure TForm1.DrawSignBars(bars: array of Char; spaces: array of Char); var i : Integer; begin Image1.Canvas.Brush.Color := clBlack; //print bars for i:=low(bars) to high(bars) do begin if (bars[i] = 'W') then // wide bar begin Image1.Canvas.Rectangle(curentPos + 20, 10, curentPos + 24, 100); curentPos := curentPos + 4; end else // narrow bar begin Image1.Canvas.Rectangle(curentPos + 20, 10, curentPos + 22, 100); curentPos := curentPos + 2; end; if (spaces[i] = 'W') then // wide space curentPos := curentPos + 4 else // narrow space curentPos := curentPos + 2; end; end; end.