Kody źródłowe/Implementacja NIP

Z Wikibooks, biblioteki wolnych podręczników.
Implementacje algorytmu obliczania cyfry kontrolnej w numerze NIP • Kod źródłowy
Implementacje algorytmu obliczania cyfry kontrolnej w numerze NIP
Kod źródłowy
Wikipedia
Zobacz w Wikipedii hasło NIP

Implementacja algorytmu w języku Ruby[edytuj]

class String
    def valid_nip?
        if length === 10 && match(/^\d+$/)
            weights = [6, 5, 7, 2, 3, 4, 5, 6, 7]
            nip = (split //).collect &:to_i
            checksum = weights.inject(0) { |sum, weight| sum + weight * nip.shift }
            return checksum % 11 === nip.shift
        else
            return false
        end
    end
end

Implementacja algorytmu w języku Python[edytuj]

def validate_nip(nip_str):
  nip_str = nip_str.replace('-', '')
  if len(nip_str) != 10 or not nip_str.isdigit(): 
    return False
  digits = [int(i) for i in nip_str]
  weights = (6, 5, 7, 2, 3, 4, 5, 6, 7)
  check_sum = sum(d * w for d, w in zip(digits, weights)) % 11
  return check_sum == digits[9]

Implementacja algorytmu w języku Common Lisp[edytuj]

(defun poprawny-nip? (liczba)
  (let* ((łańcuch (write-to-string liczba))
         (checksum (loop for w in '(6 5 7 2 3 4 5 6 7)
                         for i across łańcuch
                         sum (* w (digit-char-p i)))))
    (= (digit-char-p (elt łańcuch 9)) (rem checksum 11))))

Sprawdzenie:

(poprawny-nip? 7680002466)
T

Implementacja algorytmu w języku Clojure[edytuj]

(defn verify [nip]
  (let [nip (map #(Character/digit % 10) nip)]
    (= (last nip)
       (rem (reduce + (map * nip [6 5 7 2 3 4 5 6 7]))
            11))))

Sprawdzenie:

(verify "7680002466")
=> true

Implementacja algorytmu w języku PHP[edytuj]

function isNipValid(string $nip): bool
{
    $nip = preg_replace('/[\s-]/', '', $nip);
    
    if (preg_match('/^[0-9]{10}$/', $nip) !== 1) {
        return false;
    }
    
    $sum = 0;
    $weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
    for ($i = 0; $i < 9; $i++) {
        $sum += $nip[$i] * $weights[$i];
    }
    
    return $sum % 11 === (int) $nip[9];
}

Implementacja algorytmu w języku Visual Basic[edytuj]

Function check_nip(ByVal str As String) As Boolean

Dim wagi() As String
Dim NIP As String
Dim sum As Integer
Dim i As Integer

check_nip = False

sum = 0

wagi = Split("6, 5, 7, 2, 3, 4, 5, 6, 7", ",")

NIP = Replace(str, "-", "")

If Len(NIP) = 10 And IsNumeric(NIP) Then 
    i = 0    
    For i = 0 To 8 Step 1    
        sum = sum + (Mid(NIP, i + 1, 1) * CInt(wagi(i)))    
    Next i    
    If sum Mod 11 = Mid(NIP, 10, 1) Then check_nip = True
End If

End Function

Implementacja algorytmu w języku Java[edytuj]

boolean isNipValid(String nip) {
	if (nip.length() == 13) {
		nip = nip.replaceAll("-", "");
	}
	if (nip.length() != 10) {
		return false;
	}
	int[] weights = {6, 5, 7, 2, 3, 4, 5, 6, 7};
	try {
		int sum = 0;
		for (int i = 0; i < weights.length; ++i) {
			sum += Integer.parseInt(nip.substring(i, i + 1)) * weights[i];
		}
		return (sum % 11) == Integer.parseInt(nip.substring(9, 10));
	} catch (NumberFormatException e) {
		return false;
	}		
}

Implementacja algorytmu w języku Scala[edytuj]

def isValidNip(number: String): Boolean = {
	val nip = number.replaceAll("\\D*","")
	if (nip.length() != 10) return false
        val weights: List[Int] = List(6,5,7,2,3,4,5,6,7,0)
	val sum: Int = nip.zipWithIndex.foldLeft(0) { case (partSum, (c, i)) =>
            partSum + c.toString.toInt * weights(i)
        }
        (sum % 11 == nip.substring(9,10).toInt)
}

Implementacja algorytmu w języku JavaScript[edytuj]

function NIPIsValid(nip) {
        var weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
        nip = nip.replace(/[\s-]/g, '');
                   
        if (nip.length === 10 && parseInt(nip, 10) > 0) {
                var sum = 0;
                for(var i = 0; i < 9; i++){
                        sum += nip[i] * weights[i];
                }                     
                return (sum % 11) === Number(nip[9]);
        }
        return false;		
}

Implementacja algorytmu w języku C#[edytuj]

public bool ValidateNip(string nip)
{
    nip = nip.Replace("-", string.Empty);

    if (nip.Length != 10 || nip.Any(chr => !Char.IsDigit(chr)))
        return false;

    int[] weights = { 6, 5, 7, 2, 3, 4, 5, 6, 7, 0 };
    int sum = nip.Zip(weights, (digit, weight) => (digit - '0') * weight).Sum();

    return (sum % 11) == (nip[9] - '0');
}

Implementacja algorytmu w języku Perl[edytuj]

sub nip_check {
        my @weights=qw/6 5 7 2 3 4 5 6 7/;
        my $my_sum;
        my $nip=shift;
        #remove hyphens
        $nip=~s/-//g;
        unless($nip=~m/^\d{10}$/){
                print  "NIP must be ten digits length string with/without hyphens\n";
                return 0;
        }
        ($nip,my $their_sum)=$nip=~m/(\d{1,9})/g;
        my @nip=split(//,$nip);
        $_*=shift(@weights) for(@nip);
        $my_sum+=$_ for(@nip);
        $my_sum%=11;
        return 1 if($my_sum==$their_sum);
}

Implementacja algorytmu w języku Bash[edytuj]

function check_nip {
        weights="657234567"
        nip=$1
        #remove hyphens
        nip=${nip//-/}
        if [[! $nip =~ ^[0-9]{10,10}$]] ; then
                echo "NIP must be ten digits length string with/without hyphens"
                return 1
        fi
        their_sum=${nip:$((-1))}
        nip=${nip:0:9}
        while ([ ! -z  $nip ]) ; do
                my_sum=$((my_sum + ${nip:0:1} * ${weights:0:1} ))
                nip=${nip#?}
                weights=${weights#?}
        done
        my_sum=$((my_sum % 11))
        if [ $my_sum -eq $their_sum ] ; then
                return 0
        else
                return 1
        fi
}

Implementacja algorytmu w języku PL/SQL[edytuj]

  function getNIP return varchar2 is
    a number := 0;
    b number := 0;
    TYPE array_t IS TABLE OF number;
    c array_t;
    e array_t;
    d varchar2(100);
  
    function getDigit return number is
    begin
      return substr(dbms_random.value(1, 10), 0, 1);
    end;
    
    function getDigitNZ return number is
      a number := 0;
    begin
      WHILE (a = 0) LOOP
        a := getDigit();
      END LOOP;
    
      return a;
    End;
  
  begin
    c := array_t(6, 5, 7, 2, 3, 4, 5, 6, 7);
    LOOP
      a := 0;
      e := array_t(getDigitNZ(),
                   getDigitNZ(),
                   getDigitNZ(),
                   getDigit(),
                   getDigit(),
                   getDigit(),
                   getDigit(),
                   getDigit(),
                   getDigit());
      FOR b IN 1 .. e.last() LOOP
        a := (c(b) * e(b)) + a;
      END LOOP;
      a := MOD(a, 11);
      EXIT WHEN a <> 10;
    END LOOP;
  
    d := '';
    FOR b IN 1 .. e.last() LOOP
      d := d || to_char(e(b));
    END LOOP;
    d := d || to_char(a);
  
    return d;
  end;

Implementacja algorytmu w języku PL/pgSQL[edytuj]

CREATE OR REPLACE FUNCTION is_valid_nip(nip character varying, optional boolean DEFAULT true) RETURNS boolean AS
 $BODY$
 DECLARE
     nip character varying:= regexp_replace(nip, '[^[:digit:]]','', 'gi');
   niparr smallint[];
   checksum smallint := 0;
    i smallint := 1;
    w smallint;
   retval boolean := length(nip::text)=0 AND optional;
 BEGIN
  
   IF retval=false AND length(nip::text)=10 AND nip::bigint>0 THEN
    niparr = (select regexp_split_to_array(nip, ''));
     FOREACH w IN ARRAY array[6, 5, 7, 2, 3, 4, 5, 6, 7]
        LOOP
            checksum = checksum + (w * niparr[i]);
         i = i + 1;
        END LOOP;
        retval = (mod(checksum, 11) = niparr[10]);
     END IF;
    return retval;
 END;
 $BODY$
 LANGUAGE plpgsql;

Funkcja z argument optional ustawionym na true zwraca true także w przypadku pustego ciągu znaków.

Implementacja algorytmu w języku Pascal[edytuj]

function checkNIP(NIP: string): boolean;
const weights: array[1..10] of shortint = (6, 5, 7, 2, 3, 4, 5, 6, 7, 0);
var
   i, temp: shortint;
   sum, error: word;
   NIPnumber: int64;

begin
  checkNIP := false;
  temp := pos('-', NIP);

  while (temp <> 0) do
  begin
    delete(NIP, temp, 1);
    temp := pos('-', NIP);
  end;

  val(NIP, NIPnumber, error);

  if (error = 0) and (length(NIP) = 10) then
  begin
    sum := 0;

    for i := 1 to 10 do
    begin
      val(NIP[i], temp, error);
      sum := sum + weights[i] * temp;
    end;

    sum := sum mod 11;
    if (sum = temp) then
      checkNIP := true;
  end;
end;

Implementacja algorytmu w języku Dart[edytuj]

static bool validateNip(String nip) {
  final rates = [6,5,7,2,3,4,5,6,7];
  final sum = rates.asMap()
  .map((index, value) => MapEntry(index, value * int.parse(nip[index])))
  .values
  .reduce((value, element) => value + element);
  return sum % 11 == int.parse(nip[9]);
}

Implementacja algorytmu w języku C++[edytuj]

Kod wykorzystuje rozwiązania dostępne począwszy od standardu C++11.

#include <algorithm>
#include <array>
#include <cctype>
#include <iostream>
#include <string>

bool is_nip_valid(std::string &nip)
{
    constexpr std::array<std::uint8_t, 9> nip_weigths{6, 5, 7, 2, 3, 4, 5, 6, 7};
    std::uint8_t nip_ctrl_sum{};

    nip.erase(std::remove(nip.begin(), nip.end(), '-'), nip.end());

    if (std::find_if_not(nip.begin(), nip.end(), [](char ch) { return static_cast<bool>(std::isdigit(ch)); }) != nip.end())
        return false;

    if (nip.length() == 10)
    {
        for (std::size_t i{}; i < 9; ++i)
            nip_ctrl_sum += (nip.at(i) - '0') * nip_weigths.at(i);

        return nip_ctrl_sum % 11 == *nip.rbegin() - '0';
    }

    return false;
}

Implementacja algorytmu w formie formuły dla programu Excel[edytuj]

Formuła sprawdza poprawność numeru NIP z komórki A1

=IF(MOD((MID(A1,1,1)*6+MID(A1,2,1)*5+MID(A1,3,1)*7+MID(A1,4,1)*2+MID(A1,5,1)*3+MID(A1,6,1)*4+MID(A1,7,1)*5+MID(A1,8,1)*6+MID(A1,9,1)*7),11)=INT(MID(A1,10,1)),TRUE,FALSE)