#include <string.h> #include <stdio.h> int luhn(const char* cc) { const int m[] = {0,2,4,6,8,1,3,5,7,9}; // mapping for rule 3 int i, odd = 1, sum = 0; for (i = strlen(cc); i--; odd = !odd) { int digit = cc[i] - '0'; sum += odd ? digit : m[digit]; } return sum % 10 == 0; } int main() { const char* cc[] = { "49927398716", "49927398717", "1234567812345678", "1234567812345670", 0 }; int i; for (i = 0; cc[i]; i++) printf("%16s\t%s\n", cc[i], luhn(cc[i]) ? "ok" : "not ok"); return 0; }
输出结果
49927398716 ok 49927398717 not ok 1234567812345678 not ok 1234567812345670 ok
收藏
0人收藏
- 2014-03-06 17:20:46JavaScript调用prototype通过Luhn算法验证信用卡卡号是否有效 by sxgkwei
- 2014-05-01 13:01:25C++验证信用卡卡号是否有效 by 灵剑子
- 2014-05-16 15:10:56Perl通过Luhn算法验证信用卡卡号的有效性 by 赵振华
- 2014-07-12 09:35:20ActionScript 验证信用卡卡号是否有效 by Loli控
- 2014-08-14 16:49:23JavaScript调用prototype通过Luhn算法验证信用卡卡号是否有效 by 0晓风残月0
- 2014-10-05 15:12:36Java通过Luhn算法验证信用卡卡号是否有效 by 法名空虚
- 2014-11-17 21:02:56Fortran通过Luhn验证信用卡卡号是否有效 by Merrill
- 2014-02-22 16:39:45C# 通过Luhn算法验证信用卡卡号是否有效 by 叠搭宝箱
- 2014-03-09 09:10:49Java通过Luhn算法验证信用卡卡号是否有效 by Koon.LY
- 2014-03-31 18:00:49Objective C通过Luhn算法实现的信用卡校验代码 by sxgkwei
- 2018-03-30 21:56:50java自动识别用户上传的文本文件编码 by Hugh
相关聚客文章
- Object-C:通过Luhn算法验证信用卡卡号是否有效
- hello,chrome,我是Firefox。。。
- Objective-C的新特性
- 大型门户站 css样式命名和应用原则
- 512M VPS上Apache性能和内存优化
- Mac OS X Lion 图标150P
- 关于Memcached的一些事之深入memcached内部
- Montana Spray Paint Concept
- Samsung Galaxy S 4 Benchmarks Nearly Twice as Fast
- Pixelmator Crashing Issue Fixed in OS X 10.8.3 [Ma
- MapR Raises $30mil in Series C
- Black Pixel to Revive 'NetNewsWire' With Cross-Dev