|  | 
 
| 
本帖最后由 RoninBoy 于 2021-7-1 18:21 编辑
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 从键盘输入一个18位或15位的身份证号码,显示器上输出年龄和性别信息。程序要求:
 1)        键盘输入前,显示器上有适当提示信息;
 2)        自定义2个函数,分别实现年龄、性别,函数具有通用性;
 3)        身份证号码,可以用数组或链表(结构体的一种应用)实现;
 4)        查询时,可以用数组或指针实现;
 5)        遍历(身份证号数的方式),可以用循环语句。
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 
 int main(void)
 {
 int length;
 printf("请输入身份证位数:\n");
 scanf("%d",&length);
 char ID[18];
 int i;
 printf("请输入身份证号:\n");
 
 if(length == 18)
 {
 for (i=0;i<18;i++)
 {
 scanf("%s",&ID[i]);
 }
 
 judgement(ID[16]);
 getAge1(ID);
 
 printf("年龄为:%d\n",getAge1(ID));
 }
 
 if(length == 15)
 {
 for (i=0;i<15;i++)
 {
 scanf("%s",&ID[i]);
 }
 
 judgement(ID[14]);
 getAge2(ID);
 printf("年龄:%d岁\n", getAge2(ID));
 }
 return 0;
 }
 
 void judgement(int num)//判定性别
 {
 if(num%2 == 0)
 {
 printf("性别:女\n");
 }
 else
 {
 printf("性别:男\n");
 }
 }
 
 int getAge1(char*ID)//判定18位身份的年龄
 {
 int day,month,year;
 int age;
 time_t t;
 struct tm * lt;
 time(&t);
 lt = localtime(&t);
 day = (ID[12] - '0') * 10 + (ID[13] - '0');
 month = (ID[10] - '0') * 10 + (ID[11] - '0');
 year = (ID[6] - '0') * 1000 + (ID[7] - '0') * 100 + (ID[8] - '0') * 10 + (ID[9] - '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 getAge2(char*ID)//判定15位身份证的年龄
 {
 int day,month,year;
 int age;
 time_t t;
 struct tm *lt;
 time(&t);
 lt = localtime(&t);
 day = (ID[10] - '0') * 10 + (ID[11] - '0');
 month = (ID[8] - '0') * 10 + (ID[9] - '0'+1);
 year = 1900+ (ID[6] - '0') * 10 + (ID[7] - '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;
 }
 
 会出现警告   [Warning] conflicting types for 'judgement'  这个怎么解决
 | 
 |