誤り検出符号 error detecting code
改訂新版 C言語による標準アルゴリズム事典をSwiftでコーディング
アルゴリズム
誤り検出符号 error detecting code
実行
Playground
コード
func errorDetectingCode(s: String) {
var d:Int
var w = 1
var t = 0
for c in s {
d = w * Int(String(c))!
if d > 9 {
d = d - 9
}
t = t + d
w = 3 - w
}
(t % 10 == 0) ? print("有効") : print("無効")
}
let s = "5555555555554444"
errorDetectingCode(s: s)






