C语言编程问题,验证出生日期的合法性问题

出生日期合法性验证程序
(1)函数f1()随机生成1000个8位数字,前4位年份范围在[1890,2015]之间
(2)函数f23 ()挑选出其中合法的日期数据
(3)函数f3()显示生日(按中文习惯)
(4)主函数调用它们,列表显示中文格式的日期和生日

#include <stdio.h>
#include <stdlib.h> 
#include <time.h>
#include <conio.h>
const int MAXSIZE = 1000;
unsigned a[MAXSIZE];

void f1() {
unsigned date;
int i;
for(i = 0; i < MAXSIZE; ++i) {
date = rand()%(2015 - 1890 + 1) + 1890;
date = 10000 * date + rand()%1131 + 101;
a[i] = date;
}
}

int f2(unsigned date) {
unsigned year = date/10000;
unsigned month = date%10000/100;
unsigned day = date%10000%100; 
if(year < 1890 || year > 2015) return 0;
if(month < 1 || month > 12) return 0;
if(day < 1 || day > 31) return 0;
if(month == 2) {
if(year%4 == 0 && year%100 != 0 || year%400 == 0) {
if(day > 29) return 0;
}
else if(day > 28) return 0;
}
switch(month) {
case  4:
case  6:
case  9:
case 11:if(day > 30) return 0;
}
return 1;
}

void f3(unsigned date) {
printf("%u年",date/10000);
printf("%02u月",date%10000/100);
printf("%02u日\n",date%10000%100);
}
 
int main() {
    int i;
    srand((unsigned)time(NULL));
    f1();
    for(i = 0; i <MAXSIZE; ++i)
     if(f2(a[i])) f3(a[i]);
    return 0;
}

温馨提示:答案为网友推荐,仅供参考
相似回答