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?

EAN-8 - Implementacja w C/C++
Ocena użytkownikóww: *****  / 1
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 08 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 EAN8
// 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 EAN8[8];
byte bars[67];

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[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}
        };

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        int i, j;

        if (Kod->Text.Length() != 8) {
                ShowMessage("Incorrect EAN8 number");
                return;
        }

        for (i=0; i<8; i++) {
                EAN8[i] = Kod->Text[i+1] - 48;
        }
        for (i=0; i<67; i++) {
                bars[i] = 0;
        }

        if (Check() == 0) {
                ShowMessage("Incorrect EAN8 number");
                return;
        }

        bars[0] = 1;
        bars[1] = 0;
        bars[2] = 1;

         for (i=0; i<4; i++) {
                for (j=0; j<7; j++) {
                        bars[i*7 + 3 + j] = left[EAN8[i]][j];
                }
        }

        bars[31] = 0;
        bars[32] = 1;
        bars[33] = 0;
        bars[34] = 1;
        bars[35] = 0;

         for (i=4; i<8; i++) {
                for (j=0; j<7; j++) {
                        bars[(i-4)*7 + 36 + j] = right[EAN8[i]][j];
                }
        }

        bars[64] = 1;
        bars[65] = 0;
        bars[66] = 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;
        for (i=0; i<67; i++) {
                if ((i >= 0 && i<= 2) ||
                    (i >= 31 && i<= 35) ||
                    (i >= 64 && i<= 66)) {
                    length = 110;
                } else {
                    length = 100;
                }
                if (bars[i] == 1) {
                        Image1->Canvas->Rectangle(i*2 + 20, 10, i*2 + 22, length);
                }
        }

        Image1->Canvas->Font->Size = 10;
        Image1->Canvas->Brush->Color = clWhite;
        Image1->Canvas->Font->Color = clBlack;
        for (i=0; i<4; i++) {

                Image1->Canvas->TextOutA(i*14 + 31, 100, IntToStr(EAN8[i]));
        }

        for (i=4; i<8; i++) {

                Image1->Canvas->TextOutA(i*14 + 41, 100, IntToStr(EAN8[i]));
        }

}

//---------------------------------------------------------------------------


int TForm1::Check()
{
        int sum = 0;
        int sum_even = 0;
        int sum_uneven = 0;
        int i;

        for (i=1; i<7; i+=2) {
                sum_uneven += EAN8[i];
        }
        for (i=0; i<7; i+=2) {
                sum_even += EAN8[i];
        }
        sum = sum_uneven + 3*sum_even;
        sum %= 10;
        sum = 10 - sum;
        sum %= 10;

        if (EAN8[7] == sum) {
                return 1;
        }
        else {
                return 0;
        }
}
//---------------------------------------------------------------------------
Dodaj komentarz