C语言(身份证问题)2017.4.23

/*你有身份证吗?但无论如何您一定有身份证号码。从身份证号码上可以得到每个人的具体个人信息。身份证号码有18位,其中前17位包含特殊的特殊含义: 前6位代表你来自的区域,然后8位代表你的生日。其他4位代表什么?你可以百度一下。 
以下是本题中可能用到的地区的代码。
在身份证号码中,可能只有33出现,0000是由其他数字取代。 
这是塞缪尔的身份证号码331004198910120036你能告诉我他来自哪里吗? 最初的两位表示他来自浙江省,19891012表示是他的生日日期(年/月/日)。 
Input
输入包含两部分: 
第一行为一个整数n,代表有n组数据
接下来为测试数据,每一行为一个代表身份证号码的字符串。 
Output
根据题目描述,输出他来自哪里和他的出生日期。具体格式可以参照样例输出。
Sample Input
1
330000198910120036
Sample Output

He/She is from Zhejiang,and his/her birthday is on 10,12,1989 based on the table.*/

方法一:

#include<stdio.h>
#include<string.h>
void result(char *a, int from, int to);
char provin[100][20] = {[33] = "Zhejiang", [11] = "Beijing", [71] = "Taiwan",
                        [81] = "Hong Kong", [82] = "Macao", [54] = "Tibet",
                        [21] = "Liaoning", [31] = "Shanghai"};//用二位数组存省份
int main()
{


    int n, i;
    char a[1000];
    int sum;
    scanf("%d", &n);
    for (i = 1; i <= n; i++)
    {
        scanf("%s", a);
        printf("He/She is from ");
        result(a, 0, 1); //provin
        printf(",and his/her birthday is on ");
        result(a, 10, 11); //month
        putchar(',');
        result(a, 12, 13); //date
        putchar(',');
        result(a, 6, 9); //year
        printf(" based on the table.\n");
    }
    return 0;
}


void result(char *a, int from, int to)
{
    int i, sum;
    if (from == 0)
    {
        sum = (a[0] - '0') * 10 + (a[1] - '0');//-‘0’是将字符变为数字
        printf("%s", provin[sum]);
        return;
    }
    
    for (i = from; i <= to; i++)
    {
printf("%d", a[i] - '0');//-‘0’是将字符变为数字
    }
}

方法二:

#include<stdio.h>
#include<string.h>
void SubString(char *dest, char *origin, int from, int to) {
int i = 0;
for (i = from; i <= to; i++) {
dest[i - from] = origin[i];//将原数组指定位数存在新数组中

dest[to - from + 1] = '\0';//因为存入新数组是一个字符一个字符存储,所以末尾家‘\0’
}

int main()
{
char idno[20], year[5], month[3], day[3], pro[20];//建立年,月,日,省的新数组
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", idno);
SubString(pro, idno, 0, 1);
SubString(year, idno, 6, 9);
SubString(month, idno, 10, 11);
SubString(day, idno, 12, 13);
if (strcmp(pro, "33") == 0)//省去二维数组的麻烦,利用strcmp函数比较开头两个字符,写出省份
strcpy(pro, "Zhejiang");
else if (strcmp(pro, "11") == 0)
strcpy(pro, "Beijing");
else if (strcmp(pro, "71") == 0)
strcpy(pro, "Taiwan");
else if (strcmp(pro, "81") == 0)
strcpy(pro, "Hong Kong");
else if (strcmp(pro, "82") == 0)
strcpy(pro, "Macao");
else if (strcmp(pro, "54") == 0)
strcpy(pro, "Tibet");
else if (strcmp(pro, "21") == 0)
strcpy(pro, "Liaoning");
else if (strcmp(pro, "31") == 0)
strcpy(pro, "Shanghai");

printf("He/She is from %s,and his/her birthday is on %s,%s,%s based on the table.\n", pro, month, day, year);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值