我想在 ColdFusion 中编写以下 C# 函数,但我不能,因为我无法理解代码。我知道该函数使用 mod 操作检查 11 位 CPF 号码(巴西相当于美国 SSN)的有效性。我完全没有使用 C# 的经验。
完整功能可以在this article中阅读.
不明白cpf[0]
,cpf[1]
等...是从哪里来的,方括号之间的数字指的是什么.
//compute 1st verification digit.
var v1 = 10 * cpf[0] + 9 * cpf[1] + 8 * cpf[2] + 7 * cpf[3] + 6 *
cpf[4] + 5 * cpf[5] + 4 * cpf[6] + 3 * cpf[7] + 2 * cpf[8];
v1 = 11 - v1 % 11;
if (v1 >= 10)
v1 = 0;
//compute 2nd verification digit.
var v2 = 11 * cpf[0] + 10 * cpf[1] + 9 * cpf[2] + 8 * cpf[3] + 7 *
cpf[4] + 6 * cpf[5] + 5 * cpf[6] + 4 * cpf[7] + 3 * cpf[8];
v2 += 2 * v1;
v2 = 11 - v2 % 11;
if (v2 >= 10)
v2 = 0;
//True if verification digits are as expected.
return v1 == cpf[9] && v2 == cpf[10];
最佳答案
给定以下 CPF 012.345.678-90
//compute 1st verification digit.
var v1 = 10 * cpf[0] + // 10 x 0 = 0
9 * cpf[1] + // 9 x 1 = 9
8 * cpf[2] + // 8 x 2 = 16
7 * cpf[3] + // 7 x 3 = 21
6 * cpf[4] + // 6 x 4 = 24
5 * cpf[5] + // 5 x 5 = 25
4 * cpf[6] + // 4 x 6 = 24
3 * cpf[7] + // 3 x 7 = 21
2 * cpf[8] // 2 x 8 = 16
; // result = 156
v1 = 11 - v1 % 11; // 11 - 156 % 11 = 11 - 2 = 9
if (v1 >= 10)
v1 = 0; // 9 >= 10 ? v1 = 9
//compute 2nd verification digit.
var v2 = 11 * cpf[0] + // 11 x 0 = 0
10 * cpf[1] + // 10 x 1 = 10
9 * cpf[2] + // 9 x 2 = 18
8 * cpf[3] + // 8 x 3 = 24
7 * cpf[4] + // 7 x 4 = 28
6 * cpf[5] + // 6 x 5 = 30
5 * cpf[6] + // 5 x 6 = 30
4 * cpf[7] + // 4 x 7 = 28
3 * cpf[8] // 3 x 8 = 24
;
v2 += 2 * v1; // 192 + 2 * 9 = 192 + 18 = 210
v2 = 11 - v2 % 11; // 11 - 210 % 11 = 11 - 1 = 10
if (v2 >= 10)
v2 = 0; // 10 >= 10 ? v2 = 0
//True if verification digits are as expected.
// v1 == 9 && v2 == 0
return v1 == cpf[9] && v2 == cpf[10];
它只是打破了纯数学
关于c# - 你能帮我理解这个 C# 函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5843639/