{"id":235,"date":"2011-11-02T22:29:11","date_gmt":"2011-11-02T14:29:11","guid":{"rendered":"http:\/\/www.kiccleaf.com\/?p=235"},"modified":"2020-08-28T12:22:19","modified_gmt":"2020-08-28T04:22:19","slug":"%e4%b8%80%e4%b8%aa%e6%a0%a1%e9%aa%8c%e8%ba%ab%e4%bb%bd%e8%af%81%e5%8f%b7%e7%a0%81%e5%90%88%e6%b3%95%e6%80%a7%e7%9a%84c%e7%a8%8b%e5%ba%8f","status":"publish","type":"post","link":"http:\/\/www.kiccleaf.com\/?p=235","title":{"rendered":"\u4e00\u4e2a\u6821\u9a8c\u8eab\u4efd\u8bc1\u53f7\u7801\u5408\u6cd5\u6027\u7684C\u7a0b\u5e8f"},"content":{"rendered":"\n<p>\u4e00\u4e2a\u6821\u9a8c\u8eab\u4efd\u8bc1\u53f7\u7801\u5408\u6cd5\u6027\u7684C\u7a0b\u5e8f\uff0c\u6536\u85cf\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"c\" class=\"language-c\">#include &lt;stdio.h>\r\n#include &lt;string.h>\r\n#include &lt;math.h>\r\n#include &lt;time.h>\r\n \r\nint IsDigitBuf(char *sBuf, int nLen)\r\n{\r\n    int    i;\r\n \r\n    if (nLen == 0) return 1;\r\n    if (nLen > strlen(sBuf)) nLen = strlen(sBuf);\r\n \r\n    for (i = 0; i &lt; nLen; i++)\r\n        if (!isdigit(sBuf[i])) return 0;\r\n \r\n    return 1;\r\n}\r\n \r\nint checkdate(int iYear, int iMonth, int iDay)\r\n{\r\n    if (iYear &lt; 0 || iYear > 9999)\r\n    return -1;\r\n    switch (iMonth)\r\n    {\r\n    case 1:\r\n    case 3:\r\n    case 5:\r\n    case 7:\r\n    case 8:\r\n    case 10:\r\n    case 12:\r\n        if (iDay &lt;= 0 || iDay > 31)\r\n        {\r\n            return -3;\r\n        }\r\n        break;\r\n    case 4:\r\n    case 6:\r\n    case 9:\r\n    case 11:\r\n        if (iDay &lt;= 0 || iDay > 30)\r\n        {\r\n            return -3;\r\n        }\r\n        break;\r\n    case 2:\r\n        if ((iYear % 4 == 0 &amp;&amp; iYear % 100 != 0) || iYear % 400 == 0)\r\n        {\r\n            if (iDay &lt;= 0 || iDay > 29)\r\n            {\r\n                return -3;\r\n            }\r\n        }\r\n        else\r\n        {\r\n            if (iDay &lt;= 0 || iDay > 28)\r\n            {\r\n                return -3;\r\n            }\r\n        }\r\n        break;\r\n    default:\r\n        return -2;\r\n    }\r\n    return 0;\r\n}\r\n \r\nint CheckStrDate(char *sDate)\r\n{\r\n    int iRet;\r\n \r\n    char sYear[5];\r\n    char sMonth[3];\r\n    char sDay[3];\r\n \r\n    memset(sYear,  0, sizeof(sYear));\r\n    memset(sMonth, 0, sizeof(sMonth));\r\n    memset(sDay,   0, sizeof(sDay));\r\n \r\n    if (strlen(sDate) != 8 )\r\n    {\r\n        return -1;\r\n    }\r\n \r\n    memcpy(sYear,  sDate, 4);\r\n    memcpy(sMonth, sDate+4, 2);\r\n    memcpy(sDay,   sDate+6, 2);\r\n \r\n    iRet = checkdate(atoi(sYear), atoi(sMonth), atoi(sDay));\r\n    if (iRet != 0)\r\n    {\r\n        return -1;\r\n    }\r\n \r\n    return 0;\r\n}\r\n \r\n \r\nint main(int argc, char *argv[])\r\n{\r\n    int    i;\r\n    int    iRet;\r\n    int    iWeight[18] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};\r\n    char   cCheck[11] = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};\r\n    char   sIdCardNo[20];\r\n    char   sDate[8 + 1];\r\n    int    iDate;\r\n    int    Sum = 0;\r\n \r\n    if (argc &lt; 2) {\r\n        printf(\"FAIL\\n\");\r\n        exit(1);\r\n    }\r\n \r\n    memset(sIdCardNo, 0, 20);\r\n    memcpy(sIdCardNo, argv[1], 18);\r\n    if ((strlen(sIdCardNo) == 15) &amp;&amp; ((iRet = IsDigitBuf(sIdCardNo, 15)) > 0)) {\r\n        printf(\"OK\\n\");\r\n        exit(0);\r\n    } else if (strlen(sIdCardNo) != 18) {\r\n        printf(\"FAIL\\n\");\r\n        exit(1);\r\n    }\r\n \r\n    \/* \u8eab\u4efd\u8bc17-14\u4f4d\u662f\u5426\u6709\u6548 *\/\r\n    memset(sDate, 0, sizeof(sDate));\r\n    memcpy(sDate, sIdCardNo + 6, 4);\r\n    iDate = atoi(sDate);\r\n    if (iDate &lt; 1900 || iDate > 2020) {\r\n        printf(\"FAIL\\n\");\r\n        exit(1);\r\n    }\r\n    memset(sDate, 0, sizeof(sDate));\r\n    memcpy(sDate, sIdCardNo + 6, 8);\r\n    iRet = CheckStrDate(sDate);\r\n    if (iRet &lt; 0) {\r\n        printf(\"FAIL\\n\");\r\n        exit(1);\r\n    }\r\n \r\n    \/* \u8eab\u4efd\u8bc118\u4f4d\u6821\u9a8c\u4f4d\u662f\u5426\u6709\u6548 *\/\r\n    for (i = 0; i &lt; 17; i ++) {\r\n        memset(sDate, 0, sizeof(sDate));\r\n        sDate[0] = sIdCardNo[i];\r\n        iDate = atoi(sDate);\r\n        Sum += iWeight[i] * iDate;\r\n    }\r\n    Sum %= 11;\r\n    if ('x' == sIdCardNo[17]) {\r\n        sIdCardNo[17] = 'X';\r\n    }\r\n    if (cCheck[Sum] != sIdCardNo[17]) {\r\n        printf(\"FAIL\\n\");\r\n        exit(1);\r\n    }\r\n \r\n    printf(\"OK\\n\");\r\n    exit(0);\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u6821\u9a8c\u8eab\u4efd\u8bc1\u53f7\u7801\u5408\u6cd5\u6027\u7684C\u7a0b\u5e8f\uff0c\u6536\u85cf\u4e00\u4e0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":11,"footnotes":""},"categories":[27],"tags":[28,13,49],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-linux","tag-49","entry"],"views":3202,"_links":{"self":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=235"}],"version-history":[{"count":1,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":738,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=\/wp\/v2\/posts\/235\/revisions\/738"}],"wp:attachment":[{"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.kiccleaf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}