Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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
C# 在C中验证ACN(澳大利亚公司编号)#_C# - Fatal编程技术网

C# 在C中验证ACN(澳大利亚公司编号)#

C# 在C中验证ACN(澳大利亚公司编号)#,c#,C#,如何在C#中验证ASIC ACN(澳大利亚公司编号) 随着时间的推移,数据将是静态的,因此为了简洁起见,请不要在此处重复这些数据。试试这个 /// <summary> /// http://stackoverflow.com/questions/38781957 /// </summary> public bool IsValidAcn(string acn) { int[] weightings = {8, 7, 6, 5, 4, 3, 2, 1}; v

如何在C#中验证ASIC ACN(澳大利亚公司编号)

随着时间的推移,数据将是静态的,因此为了简洁起见,请不要在此处重复这些数据。

试试这个

/// <summary>
/// http://stackoverflow.com/questions/38781957
/// </summary>
public bool IsValidAcn(string acn)
{
    int[] weightings = {8, 7, 6, 5, 4, 3, 2, 1};
    var accumulatedSum = 0;

    acn = acn?.Replace(" ", ""); // strip spaces

    if (string.IsNullOrWhiteSpace(acn) || !Regex.IsMatch(acn, @"^\d{9}$"))
    {
        return false;
    }

    // Sum the multiplication of all the digits and weights
    for (int i = 0; i < weightings.Length; i++)
    {
        accumulatedSum += Convert.ToInt32(acn.Substring(i, 1)) * weightings[i];
    }

    var remainder = accumulatedSum % 10;

    var expectedCheckDigit = (10 - remainder == 10) ? 0 : (10 - remainder);

    var actualCheckDigit = Convert.ToInt32(acn.Substring(8, 1));

    return expectedCheckDigit == actualCheckDigit;
}
[Theory]
[InlineData("604475587", true)]
[InlineData("00 258 9460", true)]
[InlineData("604475587asdfsf", false)]
[InlineData("444", false)]
[InlineData(null, false)]
public void IsValidAcn(string acn, bool expectedValidity)
{
    var sut = GetSystemUnderTest();
    Assert.True(sut.IsValidAcn(acn) == expectedValidity);
}