Nadesłany przez Tomasz Lubiński, 17 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.
Kody Kreskowe - C++/Unit1.cpp:
//--------------------------------------------------------------------------- // Generowanie kodow kreskowych Extended CODE 39 // www.algorytm.org // (c)2006 Tomasz Lubinski //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" char code39sign[43] = { '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', '-', '.', ' ', '$', '/', '+', '%'}; char code39bars[43][9] = { {'N', 'N', 'N', 'W', 'W', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W'}, {'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'W'}, {'W', 'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'W'}, {'W', 'N', 'N', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'W', 'W', 'W', 'N', 'N', 'N', 'N'}, {'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'W'}, {'W', 'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N'}, {'N', 'N', 'W', 'W', 'N', 'N', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W'}, {'N', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'W'}, {'W', 'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'W'}, {'W', 'N', 'N', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'W', 'N', 'W', 'W', 'N', 'N', 'N'}, {'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'W'}, {'W', 'N', 'N', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'W', 'N', 'N', 'W', 'W', 'N', 'N'}, {'N', 'N', 'N', 'N', 'W', 'W', 'W', 'N', 'N'}, {'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W'}, {'N', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'W'}, {'W', 'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'W'}, {'W', 'N', 'N', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'W', 'N', 'W', 'N', 'N', 'W', 'N'}, {'N', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'W'}, {'W', 'N', 'N', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'W', 'N', 'N', 'N', 'W', 'W', 'N'}, {'N', 'N', 'N', 'N', 'W', 'N', 'W', 'W', 'N'}, {'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N', 'W'}, {'N', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'W'}, {'W', 'W', 'W', 'N', 'N', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'W'}, {'W', 'W', 'N', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'W', 'N', 'W', 'N', 'N', 'N', 'N'}, {'N', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'W'}, {'W', 'W', 'N', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'W', 'N', 'N', 'N', 'W', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'W', 'N', 'N', 'N'}, {'N', 'W', 'N', 'W', 'N', 'N', 'N', 'W', 'N'}, {'N', 'W', 'N', 'N', 'N', 'W', 'N', 'W', 'N'}, {'N', 'N', 'N', 'W', 'N', 'W', 'N', 'W', 'N'} }; char *extendedCode39Replace [128] = { "%U", "$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", "%A", "%B", "%C", "%D", "%E", " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G", "/H", "/I", "/J", "/K", "/L", "-", ".", "/O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "/Z", "%F", "%G", "%H", "%I", "%J", "%V", "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", "%K", "%L", "%M", "%N", "%O", "%W", "+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", "%P", "%Q", "%R", "%S", "%T" }; char start_stop[9] = { 'N', 'W', 'N', 'N', 'W', 'N', 'W', 'N', 'N' }; int wide_multiply = 2; TForm1 *Form1; int curentPos; //converts to code 39 set of characters char* convert(char *txt, char *result) { result[0] = 0; for (unsigned int i=0; i<strlen(txt); i++) { if ((txt[i] >= 0) && (txt[i] <= 127)) result = strcat(result, extendedCode39Replace[txt[i]]); else result = strncat(result, txt+i, 1); } return result; } // returns 1 if letter is correct, 0 otherwise int checkLetter(char ch) { for (int i=0; i<43; i++) if (code39sign[i] == ch) return 1; return 0; } // reurns 1 if txt is correct, 0 otherwise int check(char *txt) { for (unsigned int i=0; i<strlen(txt); i++) if (checkLetter(txt[i]) == 0) return 0; return 1; } void drawSignBars(char bars[]) { int nextPos = 0; Form1->Image1->Canvas->Brush->Color = clBlack; //print bars for (int i=0; i<9; i++) { if (bars[i] == 'W') nextPos = curentPos + 2*wide_multiply; else nextPos = curentPos + 2; if ((i % 2) == 0) Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, nextPos + 20, 100); curentPos = nextPos; } curentPos += 2; } void drawBars(char *txt, char *converted) { curentPos = 0; Form1->Image1->Canvas->Brush->Color = clWhite; Form1->Image1->Canvas->Rectangle(0, 0, 441, 121); //print start character drawSignBars(start_stop); //print characters for (unsigned int i=0; i<strlen(converted); i++) for (int j=0; j<43; j++) if (code39sign[j] == converted[i]) { drawSignBars(code39bars[j]); break; } //Draw text Form1->Image1->Canvas->Font->Size = 10; Form1->Image1->Canvas->Brush->Color = clWhite; Form1->Image1->Canvas->Font->Color = clBlack; Form1->Image1->Canvas->TextOut(curentPos/2 + 20 - strlen(converted)*3, 100, txt); //print stop character drawSignBars(start_stop); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { char converted[1000]; convert(Kod->Text.c_str(), converted); if (check(converted) == 0) { ShowMessage("Incorrect text"); return; } drawBars(Kod->Text.c_str(), converted); } //---------------------------------------------------------------------------