Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何验证新加坡FIN?_Algorithm_Validation - Fatal编程技术网

Algorithm 如何验证新加坡FIN?

Algorithm 如何验证新加坡FIN?,algorithm,validation,Algorithm,Validation,有人能提供一个算法来验证新加坡FIN吗 我知道,对于新加坡的NRIC,我可以通过模11对其进行验证,然后将结果与查找表进行比较,但找不到与FIN类似的查找表 我也不确定模11是否是正确的验证方法 我知道政府以400美元出售一种算法,但也许有人知道一种更便宜的方法 c#实施奖励积分 在四处搜索之后,我找到了一种验证它们的方法。这并不一定意味着鳍是有效的,只是它在有效范围内 我是基于 我还包括了一个类似的检查NRIC的方法,因为我认为任何遇到这个问题并且对其中一个感兴趣的人都会对另一个感兴趣 希望这

有人能提供一个算法来验证新加坡FIN吗

我知道,对于新加坡的NRIC,我可以通过模11对其进行验证,然后将结果与查找表进行比较,但找不到与FIN类似的查找表

我也不确定模11是否是正确的验证方法

我知道政府以400美元出售一种算法,但也许有人知道一种更便宜的方法


c#实施奖励积分

在四处搜索之后,我找到了一种验证它们的方法。这并不一定意味着鳍是有效的,只是它在有效范围内

我是基于

我还包括了一个类似的检查NRIC的方法,因为我认为任何遇到这个问题并且对其中一个感兴趣的人都会对另一个感兴趣

希望这对别人有帮助

    private static readonly int[] Multiples = { 2, 7, 6, 5, 4, 3, 2 };

    public static bool IsNricValid(string nric)
    {
        if (string.IsNullOrEmpty(nric))
        {
            return false;
        }

        //  check length
        if (nric.Length != 9)
        {
            return false;
        }

        int total = 0
            , count = 0
            , numericNric;
        char first = nric[0]
            , last = nric[nric.Length - 1];

        if (first != 'S' && first != 'T')
        {
            return false;
        }

        if (!int.TryParse(nric.Substring(1, nric.Length - 2), out numericNric))
        {
            return false;
        }

        while (numericNric != 0)
        {
            total += numericNric % 10 * Multiples[Multiples.Length - (1 + count++)];

            numericNric /= 10;
        }

        char[] outputs;
        if (first == 'S')
        {
            outputs = new char[] { 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' };
        }
        else
        {
            outputs = new char[] { 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' };
        }

        return last == outputs[total % 11];

    }

    public static bool IsFinValid(string fin)
    {
        if (string.IsNullOrEmpty(fin))
        {
            return false;
        }

        //  check length
        if (fin.Length != 9)
        {
            return false;
        }

        int total = 0
            , count = 0
            , numericNric;
        char first = fin[0]
            , last = fin[fin.Length - 1];

        if (first != 'F' && first != 'G')
        {
            return false;
        }

        if (!int.TryParse(fin.Substring(1, fin.Length - 2), out numericNric))
        {
            return false;
        }

        while (numericNric != 0)
        {
            total += numericNric % 10 * Multiples[Multiples.Length - (1 + count++)];

            numericNric /= 10;
        }

        char[] outputs;
        if (first == 'F')
        {
            outputs = new char[] { 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' };
        }
        else
        {
            outputs = new char[] { 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' };
        }

        return last == outputs[total % 11];
    }

下面是用JavaScript编写的类似代码

var nric = [];

nric.multiples = [ 2, 7, 6, 5, 4, 3, 2 ];

nric.isNricValid = function (theNric) {

    if (!theNric || theNric == '')
    {
        return false;
    }

    if (theNric.length != 9)
    {
        return false;
    }

    var total = 0
        , count = 0
        , numericNric;
    var first = theNric[0]
        , last = theNric[theNric.length - 1];

    if (first != 'S' && first != 'T')
    {
        return false;
    }

    numericNric = theNric.substr(1, theNric.length - 2);

    if (isNaN(numericNric)) {
        return false
    }

    while (numericNric != 0)
    {
        total += (numericNric % 10) * nric.multiples[nric.multiples.length - (1 + count++)];

        numericNric /= 10;
        numericNric = Math.floor(numericNric);
    }

    var outputs;
    if (first == 'S')
    {
        outputs = [ 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' ];
    }
    else
    {
        outputs = [ 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' ];
    }

    return last == outputs[total % 11];

}

nric.isFinValid = function(fin)
{
    if (!fin || fin == '')
    {
        return false;
    }

    if (fin.length != 9)
    {
        return false;
    }

    var total = 0
        , count = 0
        , numericNric;
    var first = fin[0]
        , last = fin[fin.length - 1];

    if (first != 'F' && first != 'G')
    {
        return false;
    }

   numericNric = fin.substr(1, fin.length - 2);

    if (isNaN(numericNric)) {
        return false;
    }

    while (numericNric != 0)
    {
        total += (numericNric % 10) * nric.multiples[nric.multiples.length - (1 + count++)];

        numericNric /= 10;
        numericNric = Math.floor(numericNric);
    }

    var outputs;
    if (first == 'F')
    {
        outputs = [ 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' ];
    }
    else
    {
        outputs = [ 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' ];
    }

    return last == outputs[total % 11];
}
在PHP中也是如此

function isNricValid ($theNric) {

    $multiples = array( 2, 7, 6, 5, 4, 3, 2 );

    if (!$theNric || $theNric == '')
    {
        return false;
    }

    if (strlen($theNric) != 9)
    {
        return false;
    }

    $total = 0;
    $count = 0;
    $numericNric = 0;

    $first = $theNric[0];
    $last = $theNric[strlen($theNric) - 1];

    if ($first != 'S' && $first != 'T')
    {
        return false;
    }

    $numericNric = substr($theNric, 1, strlen($theNric) - 2);

    if (!is_numeric ($numericNric)) {
        return false;
    }

    while ($numericNric != 0)
    {
        $total += ($numericNric % 10) * $multiples[sizeof($multiples) - (1 + $count++)];

        $numericNric /= 10;
        $numericNric = floor($numericNric);
    }

    $outputs = '';
    if (strcmp($first, "S") == 0)
    {
        $outputs = array( 'J', 'Z', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A' );
    }
    else
    {   
        $outputs = array( 'G', 'F', 'E', 'D', 'C', 'B', 'A', 'J', 'Z', 'I', 'H' );
    }

    return $last == $outputs[$total % 11];

}

function isFinValid ($fin)
{
    $multiples = array( 2, 7, 6, 5, 4, 3, 2 );
    if (!$fin || $fin == '')
    {
        return false;
    }

    if (strlen($fin) != 9)
    {
        return false;
    }

    $total = 0;
    $count = 0;
    $numericNric = 0;
    $first = $fin[0];
    $last = $fin[strlen($fin) - 1];

    if ($first != 'F' && $first != 'G')
    {
        return false;
    }

    $numericNric = substr($fin, 1, strlen($fin) - 2);

    if (!is_numeric ($numericNric)) {
        return false;
    }

    while ($numericNric != 0)
    {
        $total += ($numericNric % 10) * $multiples[sizeof($multiples) - (1 + $count++)];

        $numericNric /= 10;
        $numericNric = floor($numericNric);
    }

    $outputs = array();

    if (strcmp($first, 'F') == 0)
    {
        $outputs = array( 'X', 'W', 'U', 'T', 'R', 'Q', 'P', 'N', 'M', 'L', 'K' );
    }
    else
    {
        $outputs = array( 'R', 'Q', 'P', 'N', 'M', 'L', 'K', 'X', 'W', 'U', 'T' );
    }

    return $last == $outputs[$total % 11];
}

以下是NRIC和FIN algaritham的URL


我添加了一个库,其中包含几种不同语言的实现-以“F”和“G”开头的NRIC如何?以F&G(FINs)开头的NRIC包含在isFinValid方法中。这是用于NRIC FIN检查逻辑检查此解决方案的多语言,如.NET、C#、Java等。
public class NRIC_FIN {

    public static void main(String[] args) {

        String idNumber = "T123d121J";
        String NRIC = idNumber.toUpperCase();
        String number;
        int sum = 0;
        if (NRIC.length() != 9) {
            System.out.println("must be 9 digits..");
            return;
        }

        number = NRIC.substring(1, 8);

        if (!isNAN(at(number, 0)))
            sum += valueOf(at(number, 0)) * 2;
        if (!isNAN(at(number, 1)))
            sum += valueOf(at(number, 1)) * 7;
        if (!isNAN(at(number, 2)))
            sum += valueOf(at(number, 2)) * 6;
        if (!isNAN(at(number, 3)))
            sum += valueOf(at(number, 3)) * 5;
        if (!isNAN(at(number, 4)))
            sum += valueOf(at(number, 4)) * 4;
        if (!isNAN(at(number, 5)))
            sum += valueOf(at(number, 5)) * 3;
        if (!isNAN(at(number, 6)))
            sum += valueOf(at(number, 6)) * 2;

        if (NRIC.charAt(0) == 'T' || NRIC.charAt(0) == 'G') {
            sum += 4;
        }

        char checkChar = NRIC.charAt(8);

        double p = 11 - (sum % 11);

        if (NRIC.charAt(0) == 'S' || NRIC.charAt(0) == 'T') {

            if ((p == 1 && checkChar == 'A') || (p == 2 && checkChar == 'B')
                    || (p == 3 && checkChar == 'C')
                    || (p == 4 && checkChar == 'D')
                    || (p == 5 && checkChar == 'E')
                    || (p == 6 && checkChar == 'F')
                    || (p == 7 && checkChar == 'G')
                    || (p == 8 && checkChar == 'H')
                    || (p == 9 && checkChar == 'I')
                    || (p == 10 && checkChar == 'Z')
                    || (p == 11 && checkChar == 'J')) {
                System.out.println("true...NRIC");
            }
        }

        if (NRIC.charAt(0) == 'F' || NRIC.charAt(0) == 'G') {
            if ((p == 1 && checkChar == 'K') || (p == 2 && checkChar == 'M')
                    || (p == 3 && checkChar == 'L')
                    || (p == 4 && checkChar == 'N')
                    || (p == 5 && checkChar == 'P')
                    || (p == 6 && checkChar == 'Q')
                    || (p == 7 && checkChar == 'R')
                    || (p == 8 && checkChar == 'T')
                    || (p == 9 && checkChar == 'U')
                    || (p == 10 && checkChar == 'W')
                    || (p == 11 && checkChar == 'X')) {
                System.out.println("true ...FIN");
            }

        }
        System.out.println("flase ...");
    }

    public static boolean isNAN(char c) {
        return !Character.isDigit(c);
    }

    public static int valueOf(char c) {
        return (((int) c) - 48);
    }

    public static char at(String from, int index) {
        return from.charAt(index);
    }
}