C语言课后作业求助:身份证号码识别,测试18位正确,测试15位性别全为女
#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 - '0') * 1000 + (ID - '0') * 100 + (ID - '0') * 10 + (ID - '0');
month = (ID - '0') * 10 + (ID - '0'+1);
day = (ID - '0') * 10 + (ID - '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 - '0') * 10 + (ID - '0');
month = (ID - '0') * 10 + (ID - '0'+1);
day = (ID - '0') * 10 + (ID - '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;
int i;
printf("请输入身份证号:\n");
scanf("%s", &ID);
genderJudge(ID);
printf("年龄:%d岁\n", ageJudge1(&ID));
}
if(n==15)
{
char ID;
int i;
printf("请输入身份证号:\n");
scanf("%s", &ID);
genderJudge(ID);
printf("年龄:%d岁\n", ageJudge2(&ID));
}
return 0;
} scanf("%s", &ID);这i都没有初始值,也能用??
期待编译器给你一个默认值?? wp231957 发表于 2021-7-1 08:36
scanf("%s", &ID);这i都没有初始值,也能用??
期待编译器给你一个默认值??
谢谢,这个用什么结构来判定更好啊,比如循环体for() 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={"\0"};
printf("请输入身份证号:\n");
scanf("%s", ID);
genderJudge(ID);
return 0;
}
/*
请输入身份证号:
123456789123456
性别:女
PS D:\我> ./wp2
请输入身份证号:
123456789123455
性别:男
PS D:\我>
*/ wp231957 发表于 2021-7-1 10:03
不用i的话,测试年龄会有错过没过生日的问题,而且会整个会报错
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 - '0') * 1000 + (ID - '0') * 100 + (ID - '0') * 10 + (ID - '0');
month = (ID - '0') * 10 + (ID - '0'+1);
day = (ID - '0') * 10 + (ID - '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 - '0') * 10 + (ID - '0');
month = (ID - '0') * 10 + (ID - '0'+1);
day = (ID - '0') * 10 + (ID - '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 = {"\0"};
printf("请输入身份证号:\n");
scanf("%s",&ID);
genderJudge(ID);
ageJudge1(&ID);
}
if(n==15)
{
char ID = {"\0"};
printf("请输入身份证号:\n");
scanf("%s",&ID);
genderJudge(ID);
ageJudge2(&ID);
}
return 0;
} RoninBoy 发表于 2021-7-1 11:43
不用i的话,测试年龄会有错过没过生日的问题,而且会整个会报错
passing argument 1 of 'ageJ ...
没啥毛病
把两处 ageJudge2(&ID);这里的 &去掉就可以了
页:
[1]