鱼C论坛

 找回密码
 立即注册
查看: 2117|回复: 5

[已解决]C语言课后作业求助:身份证号码识别,测试18位正确,测试15位性别全为女

[复制链接]
发表于 2021-7-1 08:18:53 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<time.h>

void genderJudge(int num) //性别判定函数
{
        if (num % 2 == 1) {
                printf("性别:男\n");
        }
        else {
                printf("性别:女\n");
        }
}

int ageJudge1(char *ID)//18位年龄判定函数
{
        int year, month, day;
        int age;
        time_t t;
        struct tm *lt;
        time(&t);
        lt = localtime(&t);
        year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '0');
        month = (ID[10] - '0') * 10 + (ID[11] - '0'+1);
        day = (ID[12] - '0') * 10 + (ID[13] - '0');
        age = lt->tm_year + 1900 - year;
        if ((lt->tm_mon + 1) < month)
        {
        age--;
        }
        if((lt->tm_mon + 1) == month)
        {
        if((lt->tm_mday) <= day)
        {
        age--;
        }
        }
        return age;
}
int ageJudge2(char *ID)//15位年龄判定函数
{
        int year, month, day;
        int age;
        time_t t;
        struct tm *lt;
        time(&t);
        lt = localtime(&t);
        year = 1900+ (ID[6] - '0') * 10 + (ID[7] - '0');
        month = (ID[8] - '0') * 10 + (ID[9] - '0'+1);
        day = (ID[10] - '0') * 10 + (ID[11] - '0');
        age = lt->tm_year + 1900 - year;
        if ((lt->tm_mon + 1) < month)
        {
        age--;
        }
        if((lt->tm_mon + 1) == month)
        {
        if((lt->tm_mday) <= day)
        {
        age--;
        }
        }
        return age;
}

int main()
{
       
        int n;
        printf("请问身份证是多少位?\n");
        scanf("%d",&n);
        if(n==18)
        {
        char ID[18];
        int i;
        printf("请输入身份证号:\n");
        scanf("%s", &ID[i]);
    genderJudge(ID[16]);
    printf("年龄:%d岁\n", ageJudge1(&ID[i]));
    }
    if(n==15)
        {
        char ID[15];
        int i;
        printf("请输入身份证号:\n");
        scanf("%s", &ID[i]);
    genderJudge(ID[14]);
    printf("年龄:%d岁\n", ageJudge2(&ID[i]));
    }
        return 0;
}
最佳答案
2021-7-1 14:37:37
RoninBoy 发表于 2021-7-1 11:43
不用i的话,测试年龄会有错过没过生日的问题,而且会整个会报错
[Warning] passing argument 1 of 'ageJ ...

没啥毛病
把两处   ageJudge2(&ID);  这里的 &  去掉就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-7-1 08:36:03 From FishC Mobile | 显示全部楼层
scanf("%s", &ID[i]);这i都没有初始值,也能用??
期待编译器给你一个默认值??
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-1 08:58:19 From FishC Mobile | 显示全部楼层
wp231957 发表于 2021-7-1 08:36
scanf("%s", &ID);这i都没有初始值,也能用??
期待编译器给你一个默认值??

谢谢,这个用什么结构来判定更好啊,比如循环体for()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-1 10:03:42 | 显示全部楼层
RoninBoy 发表于 2021-7-1 08:58
谢谢,这个用什么结构来判定更好啊,比如循环体for()
#include <stdio.h>
  
void genderJudge(int num) //性别判定函数
{
        if (num % 2 == 1) {
                printf("性别:男\n");
        }
        else {
                printf("性别:女\n");
        }
}

int main()
{
    char ID[16]={"\0"};
    printf("请输入身份证号:\n");
    scanf("%s", ID);
    genderJudge(ID[14]);
    return 0;
}

/*
    请输入身份证号:
    123456789123456
    性别:女
    PS D:\我> ./wp2
    请输入身份证号:
    123456789123455
    性别:男
    PS D:\我>
*/
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-1 11:43:53 From FishC Mobile | 显示全部楼层
wp231957 发表于 2021-7-1 10:03

不用i的话,测试年龄会有错过没过生日的问题,而且会整个会报错
[Warning] passing argument 1 of 'ageJudge2' makes pointer from integer without a cast

#include<stdio.h>
#include<time.h>

void genderJudge(int num) //性别判定函数
{
        if (num % 2 == 1) {
                printf("性别:男\n");
        }
        else {
                printf("性别:女\n");
        }
}

int ageJudge1(char *ID)//18位年龄判定函数
{
        int year, month, day;
        int age;
        time_t t;
        struct tm *lt;
        time(&t);
        lt = localtime(&t);
        year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '0');
        month = (ID[10] - '0') * 10 + (ID[11] - '0'+1);
        day = (ID[12] - '0') * 10 + (ID[13] - '0');
        age = lt->tm_year + 1900 - year;
        if ((lt->tm_mon + 1) < month)
        {
        age--;
        }
        if((lt->tm_mon + 1) == month)
        {
        if((lt->tm_mday) <= day)
        {
        age--;
        }
        }
        printf("年龄:%d岁\n",age);
        return age;
}
int ageJudge2(char *ID)//15位年龄判定函数
{
        int year, month, day;
        int age;
        time_t t;
        struct tm *lt;
        time(&t);
        lt = localtime(&t);
        year = 1900+ (ID[6] - '0') * 10 + (ID[7] - '0');
        month = (ID[8] - '0') * 10 + (ID[9] - '0'+1);
        day = (ID[10] - '0') * 10 + (ID[11] - '0');
        age = lt->tm_year + 1900 - year;
        if ((lt->tm_mon + 1) < month)
        {
        age--;
        }
        if((lt->tm_mon + 1) == month)
        {
        if((lt->tm_mday) <= day)
        {
        age--;
        }
        }
        printf("年龄:%d岁\n",age);
        return age;
}

int main()
{
       
        int n;
        printf("请问身份证是多少位?\n");
        scanf("%d",&n);
        if(n==18)
        {
        char ID[19] = {"\0"};
        printf("请输入身份证号:\n");
        scanf("%s",&ID);
    genderJudge(ID[16]);
    ageJudge1(&ID);
    }
    if(n==15)
        {
        char ID[16] = {"\0"};
        printf("请输入身份证号:\n");
        scanf("%s",&ID);
    genderJudge(ID[14]);
    ageJudge2(&ID);
    }
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-1 14:37:37 | 显示全部楼层    本楼为最佳答案   
RoninBoy 发表于 2021-7-1 11:43
不用i的话,测试年龄会有错过没过生日的问题,而且会整个会报错
[Warning] passing argument 1 of 'ageJ ...

没啥毛病
把两处   ageJudge2(&ID);  这里的 &  去掉就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 16:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表