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 - C++/Unit1.cpp:
//--------------------------------------------------------------------------- // Generowanie kodow kreskowych Standard Code 2 of 5 // www.algorytm.org // (c)2006 Tomasz Lubinski //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" char codeS25sign[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char codeS25bars[10][14] = { {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} }; char start[8] = { 1, 1, 0, 1, 1, 0, 1, 0 }; char stop[8] = { 1, 1, 0, 1, 0, 1, 1, 0 }; TForm1 *Form1; int curentPos; // returns 1 if letter is correct, 0 otherwise int checkLetter(char ch) { for (int i=0; i<10; i++) if (codeS25sign[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; } // returns letter value for check digit int getLetterValue(char ch) { for (int i=0; i<10; i++) if (codeS25sign[i] == ch) return i; return 0; } // return check digit int checkDigit(char *txt) { int sum = 0; int w1 = 0; int w2 = 0; for (unsigned int i=0; i<strlen(txt); i++) { if (i % 2 == 0) { w1 += getLetterValue(txt[i]); } else { w2 += getLetterValue(txt[i]); } } if (strlen(txt) % 2 == 0) { w2 *= 3; } else { w1 *= 3; } sum = w1+ w2; sum %= 10; sum = 10 - sum; sum %= 10; return sum; } void drawSignBars(char bars[], int count) { Form1->Image1->Canvas->Brush->Color = clBlack; //print bars for (int i=0; i<count; i++) { if (bars[i] == 1) Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 22, 100); curentPos += 2; } } void drawBars(char *txt) { curentPos = 0; Form1->Image1->Canvas->Brush->Color = clWhite; Form1->Image1->Canvas->Rectangle(0, 0, 441, 121); //print start character drawSignBars(start, 8); //print characters for (unsigned int i=0; i<strlen(txt); i++) for (int j=0; j<10; j++) if (codeS25sign[j] == txt[i]) { drawSignBars(codeS25bars[j], 14); break; } //print check digit drawSignBars(codeS25bars[checkDigit(txt)], 14); //print stop character drawSignBars(stop, 8); //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(txt)*3, 100, txt); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { if (check(Kod->Text.c_str()) == 0) { ShowMessage("Incorrect text"); return; } drawBars(Kod->Text.c_str()); } //---------------------------------------------------------------------------