algorytm.org

Implementacja w Delphi/Pascal



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?

REGON - Implementacja w Delphi/Pascal
Ocena użytkownikóww: *****  / 1
SłabyŚwietny
Nadesłany przez Tomasz Lubiński, 13 października 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.

regon_d/regon_d.dpr:
//
// @author Tomasz Lubinski
// www.algorym.org
// (c) 2005
//
// REGON
//


program regon_d;
{$APPTYPE CONSOLE}
uses
  SysUtils;

var

  REGON : array [0..13] of Integer;
  valid : boolean;
  RegonStr : String;
  RegonLength : Integer = 0;


function checkSum9(): boolean;
var
        sum : integer;
begin
	sum := 8 * REGON[0] +
	       9 * REGON[1] +
	       2 * REGON[2] +
	       3 * REGON[3] +
	       4 * REGON[4] +
	       5 * REGON[5] +
	       6 * REGON[6] +
	       7 * REGON[7];
	sum := sum mod 11;
	if sum = 10 then
		sum := 0;

	if sum = REGON[8] then
		result := true
	else
		result := false;
end;


function checkSum14(): boolean;
var
        sum : integer;
begin
	sum := 2 * REGON[0] + 
	       4 * REGON[1] +
	       8 * REGON[2] +
	       5 * REGON[3] +
	       0 * REGON[4] +
	       9 * REGON[5] +
	       7 * REGON[6] +
	       3 * REGON[7] +
	       6 * REGON[8] +
	       1 * REGON[9] +
	       2 * REGON[10] +
	       4 * REGON[11] +
	       8 * REGON[12];
	sum := sum mod 11;
	if sum = 10 then
		sum := 0;

	if sum = REGON[13] then
		result := true
	else
		result := false;
end;


function checkSum(): boolean;
begin
	if REGONLength = 9 then
		result := checkSum9()
	else
		result := (checkSum9() and checkSum14());
end;


procedure RegonValidator(RegonNumber: String);
var
        i : integer;
begin

        if (length(RegonNumber) <> 9) and (length(RegonNumber) <> 14) then
		valid := false
	else
           begin
                REGONLength := length(RegonNumber);
		for i := 0 to REGONLength - 1  do
			REGON[i] := StrToInt(REGONNumber[i+1]);
		if (checkSum()) then
			valid := true
		else
			valid := false;
            end;
end;


begin
        writeln('Podaj numer REGON');
        readln(REGONStr);

        RegonValidator(REGONStr);

        if valid then
           begin
        	writeln('Numer REGON jest prawidlowy');
           end
        else
           begin
        	writeln('Numer REGON jest nieprawidlowy');
           end;

end.
Dodaj komentarz