Nadesłany przez Tomasz Lubiński, 11 sierpnia 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 MSI
// www.algorytm.org
// (c)2007 Tomasz Lubinski
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
char MSIsign[16] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
char MSIbars[16][12] = {
{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0},
{1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0},
{1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0}
};
char start[3] = {
1, 1, 0
};
char stop[4] = {
1, 0, 0, 1
};
char start_length = 3;
char stop_length = 4;
char digit_length = 12;
char mod10values[16] = {0, 1, 4, 3, 8, 5, 3, 7, 7, 9, 2, 11, 6, 13, 10, 15};
TForm1 *Form1;
int curentPos;
// returns 1 if letter is correct, 0 otherwise
int checkLetter(char ch)
{
for (int i=0; i<16; i++)
if (MSIsign[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<16; i++)
if (MSIsign[i] == ch)
return i;
return 0;
}
//calculate modulo 10 check digit
int mod10checkDigit (char *code, int length)
{
int sum = 0;
for (int i=0; i<length; i++)
{
sum += mod10values[getLetterValue(code[i])];
}
sum %= 10;
sum = 10-sum;
sum %= 10;
return sum;
}
//calculate modulo 11 check digit
int mod11checkDigit (char *code, int length)
{
int sum = 0;
int w = 0;
for (int i=length-1; i>=0; i--)
{
sum += getLetterValue(code[i]) * (w + 2);
w++;
w %= 6;
}
sum %= 11;
sum = 11-sum;
sum %= 11;
return sum;
}
// reurns 1 if check digits are correct, 0 otherwise
int checkCheckDigit(char *txt)
{
int len = strlen(txt);
// check check digits
if (Form1->mod10->Checked)
{
if (getLetterValue(txt[len-1]) != mod10checkDigit(txt, len-1))
{
return 0;
}
}
else if (Form1->mod10mod10->Checked)
{
if (getLetterValue(txt[len-2]) != mod10checkDigit(txt, len-2) ||
getLetterValue(txt[len-1]) != mod10checkDigit(txt, len-1))
{
return 0;
}
}
else if (Form1->mod11->Checked)
{
if (getLetterValue(txt[len-1]) != mod11checkDigit(txt, len-1))
{
return 0;
}
}
else if (Form1->mod11mod10->Checked)
{
if (getLetterValue(txt[len-2]) != mod11checkDigit(txt, len-2) ||
getLetterValue(txt[len-1]) != mod10checkDigit(txt, len-1))
{
return 0;
}
}
return 1;
}
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;
}
}
void drawBars(char *txt)
{
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, start_length);
//print characters
for (unsigned int i=0; i<strlen(txt); i++)
for (int j=0; j<16; j++)
if (MSIsign[j] == txt[i])
{
drawSignBars(MSIbars[j], digit_length);
break;
}
//print stop character
drawSignBars(stop, 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;
}
if (checkCheckDigit(Kod->Text.c_str()) == 0)
{
ShowMessage("Incorrect check digit");
return;
}
drawBars(Kod->Text.c_str());
}
//---------------------------------------------------------------------------

