验证身份证的真伪
写一个程序,验证所报身份证号码是真是假请输入身份证号码:
输入:11010519491231002X
输出:输入身份证为真 或:
输出:输入身份证为假 #include "stdio.h"
int main(int argc, char* argv[])
{
int i, T, sum = 0;
char a; //int a;//身份证的18位数字
int b = { 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 }; //前17位数的系数
char c = { 'f' }; //存放校验结果
printf("请输入身份证号码:");
for (i = 0;i < 18;i++)
{
scanf("%c", &a);
}
for (i = 0;i < 17;i++) // sum = sum + a * b
{
sum += (a - '0') * b;
}
T = sum % 11;
switch (T)
{
case 0: c = '1';break;
case 1: c = '0';break;
case 2: c = 'X';break;
case 3: c = '9';break;
case 4: c = '8';break;
case 5: c = '7';break;
case 6: c = '6';break;
case 7: c = '5';break;
case 8: c = '4';break;
case 9: c = '3';break;
case 10: c = '2';break;
}
if (c == a)
{
printf("输入身份证为真\n");
}
else
{
printf("输入身份证为假\n");
}
} #include<stdio.h>
unsigned char i, weight[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }, code[] = { 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };
unsigned short sum = 0;
int main() {
for (i = 0; i < 17; i++) {
sum += (getchar() - '0') * weight;
}
sum = code;
i = getchar();
if (i - 'X') {
i -= '0';
}
else {
i = 10;
}
puts(i - sum ? "输入身份证为假" : "输入身份证为假");
return 0;
}
页:
[1]