身分證檢查(完整版)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char s[11]="A201411163";
char id[27]="ABCDEFGHJKLMNPQRSTUVXYWZIO";
int i, k, n=strlen(s), sum=0;
char checkDigit;
printf("請輸入身分證號碼 :");
scanf("%s",&s);
for (i=n-2; i>=1; i--)
sum=sum+(s[i]-'0')*(n-1-i);
for(i=1;i<=strlen(id);i++)
{
if(id[i-1] == s[0] )
{
k= 10 + i - 1;
break;
}
}
sum=sum+(k%10)*9+(k/10)*1;
sum = sum % 10;
if (sum == 0)
sum = 0;
else
sum = 10 - sum;
checkDigit=sum+'0';
printf("身份證號碼= \"%s\"\n", s);
if (checkDigit==s[n-1])
printf(" check digit OK!\n");
else
printf("身份證最後一碼=%c\n正確碼應為 %c\n",s[n-1],checkDigit);
system("pause");
return 0;
}