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 - C++/Unit1.cpp:
//--------------------------------------------------------------------------- // Generowanie kodow kreskowych CODE 11 // www.algorytm.org // (c)2006 Tomasz Lubinski //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" char code11sign[11] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'}; char code11bars[11][7] = { {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} }; char code11bars_length[11] = { 6, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6 }; char start_stop[7] = { 1, 0, 1, 1, 0, 0, 1 }; char start_stop_length = 7; TForm1 *Form1; int curentPos; // returns 1 if letter is correct, 0 otherwise int checkLetter(char ch) { for (int i=0; i<11; i++) if (code11sign[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<11; i++) if (code11sign[i] == ch) return i; return 0; } // return check digit C int checkDigitC(char *txt) { int sum = 0; int w = 0; for (int i=strlen(txt)-1; i>=0; i--) { sum += (getLetterValue(txt[i]) * ((w % 10) + 1)); w++; } return (sum % 11); } // return check digit K int checkDigitK(char *txt) { int sum = 0; int w = 0; for (int i=strlen(txt)-1; i>=0; i--) { sum += (getLetterValue(txt[i]) * ((w % 9) + 1)); w++; } return (sum % 11); } void drawSignBars(char bars[], int length) { Form1->Image1->Canvas->Brush->Color = clBlack; //print bars for (int i=0; i<length; i++) { if (bars[i] == 1) Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 22, 100); curentPos += 2; } curentPos += 2; } void drawBars(char *txt) { char c, k; char text[1000]; char tmp[2]; strcpy(text, txt); curentPos = 0; Form1->Image1->Canvas->Brush->Color = clWhite; Form1->Image1->Canvas->Rectangle(0, 0, 441, 121); //print start character drawSignBars(start_stop, start_stop_length); //print characters for (unsigned int i=0; i<strlen(txt); i++) for (int j=0; j<11; j++) if (code11sign[j] == txt[i]) { drawSignBars(code11bars[j], code11bars_length[j]); break; } //print check digit C c = checkDigitC(txt); drawSignBars(code11bars[c], code11bars_length[c]); tmp[0] = code11sign[c]; tmp[1] = 0; strcat(text, tmp); if (strlen(txt)>9) { //print check digit K k = checkDigitK(text); drawSignBars(code11bars[k], code11bars_length[k]); tmp[0] = code11sign[k]; tmp[1] = 0; strcat(text, tmp); } //print stop character drawSignBars(start_stop, start_stop_length); //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, text); } //--------------------------------------------------------------------------- __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()); } //---------------------------------------------------------------------------