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 Interleaved Code 2 of 5 // www.algorytm.org // (c)2007 Tomasz Lubinski //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" char codeI25sign[10] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; char codeI25bars[10][5] = { {'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'} }; char start[4] = { 1, 0, 1, 0 }; char stop[4] = { 1, 1, 0, 1 }; TForm1 *Form1; int curentPos; // returns 1 if letter is correct, 0 otherwise int checkLetter(char ch) { for (int i=0; i<10; i++) if (codeI25sign[i] == ch) return 1; return 0; } // reurns 1 if txt is correct, 0 otherwise int check(char *txt) { if (strlen(txt) % 2 == 0) return 0; 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 (codeI25sign[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 drawSignBars(char bars[], char spaces[]) { Form1->Image1->Canvas->Brush->Color = clBlack; //print bars for (int i=0; i<5; i++) { if (bars[i] == 'W') // wide bar { Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 24, 100); curentPos += 4; } else // narrow bar { Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 22, 100); curentPos += 2; } if (spaces[i] == 'W') // wide space { curentPos += 4; } else // narrow space { curentPos += 2; } } } void drawBars(char *txt) { int odd, even; curentPos = 0; Form1->Image1->Canvas->Brush->Color = clWhite; Form1->Image1->Canvas->Rectangle(0, 0, 441, 121); //print start character drawSignBars(start, 4); //print characters for (unsigned int i=0; i<strlen(txt)-1; i+=2) { odd = getLetterValue(txt[i]); even = getLetterValue(txt[i+1]); drawSignBars(codeI25bars[odd], codeI25bars[even]); } //print last digit and check digit odd = getLetterValue(txt[strlen(txt)-1]); drawSignBars(codeI25bars[odd], codeI25bars[checkDigit(txt)]); //print stop character drawSignBars(stop, 4); //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()); } //---------------------------------------------------------------------------