algorytm.org

Implementacja w C/C++

Baza Wiedzy
wersja offline serwisu przeznaczona na urządzenia z systemem Android
Darowizny
darowiznaWspomóż rozwój serwisu
Nagłówki RSS
Artykuły
Implementacje
Komentarze
Forum
Bookmarki






Sonda
Implementacji w jakim języku programowania poszukujesz?

Extended Code 93 - Implementacja w C/C++
Ocena użytkownikóww: *****  / 1
SłabyŚwietny
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 Extended CODE 93
// www.algorytm.org
// (c)2006 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 *extendedCode93Replace [128] = {
        "\x81""U", "\x80""A", "\x80""B", "\x80""C", "\x80""D", "\x80""E", "\x80""F", "\x80""G", "\x80""H",
        "\x80""I", "\x80""J", "\x80""K", "\x80""L", "\x80""M", "\x80""N", "\x80""O", "\x80""P", "\x80""Q",
        "\x80""R", "\x80""S", "\x80""T", "\x80""U", "\x80""V", "\x80""W", "\x80""X", "\x80""Y", "\x80""Z",
        "\x81""A", "\x81""B", "\x81""C", "\x81""D", "\x81""E", " ", "\x82""A", "\x82""B", "\x82""C", "\x82""D",
         "\x82""E", "\x82""F", "\x82""G", "\x82""H", "\x82""I", "\x82""J", "\x82""K", "\x82""L", "-", ".", 
         "\x82""O", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "\x82""Z", "\x81""F", "\x81""G", "\x81""H",
        "\x81""I", "\x81""J", "\x81""V", "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", "\x81""K", "\x81""L", "\x81""M", "\x81""N",
        "\x81""O", "\x81""W", "\x83""A", "\x83""B", "\x83""C", "\x83""D", "\x83""E", "\x83""F", "\x83""G",
        "\x83""H", "\x83""I", "\x83""J", "\x83""K", "\x83""L", "\x83""M", "\x83""N", "\x83""O", "\x83""P",
        "\x83""Q", "\x83""R", "\x83""S", "\x83""T", "\x83""U", "\x83""V", "\x83""W", "\x83""X", "\x83""Y", 
        "\x83""Z", "\x81""P", "\x81""Q", "\x81""R", "\x81""S", "\x81""T"
        };

char start_stop[9] = { 
        1, 0, 1, 0, 1, 1, 1, 1, 0
        };

TForm1 *Form1;
int curentPos;

//converts to code 93 set of characters
char* convert(char *txt, char *result)
{
        char *incorrect = "\x84";
        result[0] = 0;

        for (unsigned int i=0; i<strlen(txt); i++)
        {
                if ((txt[i] >= 0) && (txt[i] <= 127))
                        result = strcat(result, extendedCode93Replace[txt[i]]);
                else
                        result = strcat(result, incorrect);
        }

        return result;
}

// returns 1 if letter is correct, 0 otherwise
int checkLetter(char ch)
{
        for (int i=0; i<47; 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 *converted)
{
        char c, k;
        
        c = checkDigitC(converted);
        k = checkDigitK(converted, 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(converted); i++)
                for (int j=0; j<47; j++)
                        if (code93sign[j] == converted[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)
{
        char converted[1000];

        convert(Kod->Text.c_str(), converted);

        if (check(converted) == 0)
        {
                ShowMessage("Incorrect text ");
                return;
        }

        drawBars(Kod->Text.c_str(), converted);
}
//---------------------------------------------------------------------------
Dodaj komentarz