Nadesłany przez Tomasz Lubiński, 27 grudnia 2005 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 EAN5
// www.algorytm.org
// (c)2005 Tomasz Lubinski
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
byte EAN5[5];
byte bars[47];
byte right[10][7] = {
{1, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 1, 1, 0, 0},
{1, 0, 0, 1, 1, 1, 0},
{1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0},
{1, 1, 1, 0, 1, 0, 0}
};
byte left[2][10][7] = {
{
{0, 0, 0, 1, 1, 0, 1},
{0, 0, 1, 1, 0, 0, 1},
{0, 0, 1, 0, 0, 1, 1},
{0, 1, 1, 1, 1, 0, 1},
{0, 1, 0, 0, 0, 1, 1},
{0, 1, 1, 0, 0, 0, 1},
{0, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 0, 1, 1},
{0, 1, 1, 0, 1, 1, 1},
{0, 0, 0, 1, 0, 1, 1}
},
{
{0, 1, 0, 0, 1, 1, 1},
{0, 1, 1, 0, 0, 1, 1},
{0, 0, 1, 1, 0, 1, 1},
{0, 1, 0, 0, 0, 0, 1},
{0, 0, 1, 1, 1, 0, 1},
{0, 1, 1, 1, 0, 0 ,1},
{0, 0, 0, 0, 1, 0, 1},
{0, 0, 1, 0, 0, 0 ,1},
{0, 0, 0, 1, 0, 0, 1},
{0, 0, 1, 0, 1, 1, 1}
}
};
byte checksumTable[10][5] = {
{1, 1, 0, 0, 0},
{1, 0, 1, 0, 0},
{1, 0, 0, 1, 0},
{1, 0, 0, 0, 1},
{0, 1, 1, 0, 0},
{0, 0, 1, 1, 0},
{0, 0, 0, 1, 1},
{0, 1, 0, 1, 0},
{0, 1, 0, 0, 1},
{0, 0, 1, 0, 1}
};
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i, j;
int addCheckSum;
if (Kod->Text.Length() != 5) {
ShowMessage("Incorrect EAN5 number");
return;
}
for (i=0; i<5; i++) {
EAN5[i] = Kod->Text[i+1] - 48;
}
for (i=0; i<47; i++) {
bars[i] = 0;
}
bars[0] = 1;
bars[1] = 0;
bars[2] = 1;
bars[3] = 1;
addCheckSum = 3*EAN5[0] + 3*EAN5[2] + 3*EAN5[4];
addCheckSum += 9*EAN5[1] + 9*EAN5[3];
addCheckSum %= 10;
for (i=0; i<5; i++) {
for (j=0; j<7; j++) {
bars[i*9 + 4 + j] = left[checksumTable[addCheckSum][i]][EAN5[i]][j];
}
if (i<4) {
bars[i*9 + 11] = 0;
bars[i*9 + 12] = 1;
}
}
DrawBars();
}
//---------------------------------------------------------------------------
void TForm1::DrawBars()
{
int i, length;
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->Rectangle(0, 0, 177, 121);
Image1->Canvas->Brush->Color = clBlack;
Image1->Canvas->Font->Size = 10;
Image1->Canvas->Brush->Color = clWhite;
Image1->Canvas->Font->Color = clBlack;
for (i=0; i<47; i++) {
if (bars[i] == 1) {
Image1->Canvas->Rectangle(i*2 + 20, 40, i*2 + 22, 100);
}
}
for (i=0; i<5; i++) {
Image1->Canvas->TextOutA(i*18 + 26, 20, IntToStr(EAN5[i]));
}
}
//---------------------------------------------------------------------------

