Nadesłany przez Tomasz Lubiński, 20 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 CODE 93
// www.algorytm.org
// (c)20, 06 Tomasz Lubinski
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
char special_1 = 127 + 1; // ($)
char special_2 = 127 + 2; // (%)
char special_3 = 127 + 3; // (/)
char special_4 = 127 + 4; // (+)
char code93sign[47] = {
'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', '-', '.', ' ', '$',
'/', '+', '%', special_1, special_2, special_3, special_4};
char code93bars[47][9] = {
{1, 0, 0, 0, 1, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 0, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 0, 0, 0, 0},
{1, 0, 0, 0, 1, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 1, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 0, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 0, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 0, 0},
{1, 1, 0, 0, 1, 0, 0, 1, 0},
{1, 1, 0, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 0, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 1, 0, 0},
{1, 0, 1, 0, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 0, 0},
{1, 0, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 0, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 0, 1, 1, 0},
{1, 1, 0, 0, 1, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 0, 0},
{1, 0, 1, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 0, 1, 0, 1, 1, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 0, 0},
{1, 1, 1, 0, 1, 0, 0, 1, 0},
{1, 1, 1, 0, 0, 1, 0, 1, 0},
{1, 0, 1, 1, 0, 1, 1, 1, 0},
{1, 0, 1, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 1, 1, 1, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 1, 0, 1, 1, 0, 1, 0},
{1, 1, 1, 0, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 0, 1, 0}
};
char start_stop[9] = {
1, 0, 1, 0, 1, 1, 1, 1, 0
};
TForm1 *Form1;
int curentPos;
// returns 1 if letter is correct, 0 otherwise
int checkLetter(char ch)
{
for (int i=0; i<43; i++)
if (code93sign[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<47; i++)
if (code93sign[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 % 20) + 1));
w++;
}
return (sum % 47);
}
// return check digit K
int checkDigitK(char *txt, char checkDigitC)
{
int sum = checkDigitC;
int w = 1;
for (int i=strlen(txt)-1; i>=0; i--)
{
sum += (getLetterValue(txt[i]) * ((w % 15) + 1));
w++;
}
return (sum % 47);
}
void drawSignBars(char bars[])
{
Form1->Image1->Canvas->Brush->Color = clBlack;
//print bars
for (int i=0; i<9; i++)
{
if (bars[i] == 1)
Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 22, 100);
curentPos += 2;
}
}
void drawTerminationBar()
{
Form1->Image1->Canvas->Brush->Color = clBlack;
Form1->Image1->Canvas->Rectangle(curentPos + 20, 10, curentPos + 22, 100);
curentPos += 2;
}
void drawBars(char *txt)
{
char c, k;
c = checkDigitC(txt);
k = checkDigitK(txt, c);
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(txt); i++)
for (int j=0; j<43; j++)
if (code93sign[j] == txt[i])
{
drawSignBars(code93bars[j]);
break;
}
//print check digit C
drawSignBars(code93bars[c]);
//print check digit K
drawSignBars(code93bars[k]);
//print stop character
drawSignBars(start_stop);
//print termination bar
drawTerminationBar();
//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());
}
//---------------------------------------------------------------------------

