香港身份證HKID用c語言如何寫?

2006-11-29 10:10 pm
有冇高手指教一下
hkID 求最後果個數c語言.

回答 (1)

2006-11-29 11:04 pm
✔ 最佳答案
for the algorithm and VB function, pls check: http://www.kgv.net/ict-ks4/TheoryTerm2/HKCheckDigit.htm

pls check following C code:
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{ char cID[7];
int n1, nSum;
int i;

strcpy( cID, argv[1] );
n1 = ( cID[0] - 64 ) % 11;
n1 = ( n1 == 0 ? 11 : n1 );
nSum = n1 * 8;
for ( i = 1; i < 7; i++ )
nSum = nSum + ( ( cID[i] - 48 ) * ( 8 - i ) );

printf( "Check sum : %d", 11 - ( nSum % 11 ) );

return 0;
}


hope can help u

2006-11-29 15:10:09 補充:
this C program has no any validation. u need to enter a correct HKID. for example, &#39;a123456&#39; is error and need to enter &#39;A123456&#39;.

2006-11-29 16:10:53 補充:
Sorry.if the last calculation is 10, the check digit would be &#39;A&#39;. Please change the last printf statement. if ( 11 - ( nSum % 11 ) == 10 ) printf( &quot;Check sum : A&quot; ); else printf( &quot;Check sum : %d&quot;, 11 - ( nSum % 11 ) );

2006-11-30 15:33:44 補充:
So sorry. also need to handle the check sum is 11. just change last printf statement.if ( 11 - ( nSum % 11 ) == 10 )printf( “Check sum : A” );elseprintf( “Check sum : %d”, ( 11 - ( nSum % 11 ) ) % 11 );


收錄日期: 2021-04-24 22:54:50
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20061129000051KK01486

檢視 Wayback Machine 備份