c++/当前日期与你的生日之间间隔多少天?生日是出生日期吗?考虑闰年平年,运用结构化编程方法编写程序。结构化编程方法是结构体来写吗?
结构化编程是一种编程范例,旨在通过广泛使用选择(if / then / else)和重复(while and for),块结构的结构化控制流构造来提高计算机程序的清晰度、质量和开发时间(引用自参考链接,更正),函数应该也是属于结构化编程的一种。生日应该是指出生的年月日。
把相差的天数分为三部分计算即可,一部分是生日那年距离年尾的天数,一部分是今年1月1号到今日的天数,一部分是生日的第二年的1月1号到今年1月一号之间的天数,三者相加即为相差的天数,下面是一个实现,供参考:
参考链接:
结构化编程_全球百科
结构化编程_百度百科
函数式编程_百度百科
使用Python计算今天距离公元1年1月1日的天数 验证数据正确性的测试代码
闰年 每月多少天_百度知道
c语言获取当前日期和时间_落春只在无意间的博客-CSDN博客_c语言获取当前日期
#include <stdio.h>
#include <time.h>
#include <string.h>
//https://zhidao.baidu.com/question/33084105.html
int leapDays [12] ={31,29,31,30,31,30,31,31,30,31,30,31}; //闰年每月的天数
int ordinaryDays[12] ={31,28,31,30,31,30,31,31,30,31,30,31}; //平年每月的天数
//计算两个年份的1月1号之间有多少天数
long getDays(int preyear, int year){
//https://baijiahao.baidu.com/s?id=1637775291345587615&wfr=spider&for=pc 天数数据验证代码
if(year==preyear){ //如果为公元1年,则天数为0
return 0;
}
else if(((year%4==0)&&(year%100!=0))||(year%400==0)){ //如果是公元年份 能被4整除,且不能被100整除,或者公元年份能别400整除则为闰年,即一年有366天
return (long)(366+getDays(preyear,year-1));
}
else{ //其他情况是平年365天
return (long)(365+getDays(preyear,year-1));
}
}
//计算生日的那年距离此年最后一天有多少天
//验证数据正确性的代码 https://www.py.cn/faq/python/17698.html
int birthdayYearDays(int year,int month,int day ){
int days = 0;
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
days+= leapDays[month-1]-day;
for(int i=month;i<12;i++){
days+= leapDays[i];
}
// printf("生日是公元%d年%d月%d日,此年是闰年,距离此年年尾共有%d天.\n",year,month,day,days);
}else{
days+= ordinaryDays[month-1]-day;
for(int i=month;i<12;i++){
days+= ordinaryDays[i];
}
//printf("生日是公元%d年%d月%d日,此年是平年,距离此年年尾共有%d天.\n",year,month,day,days);
}
return days;
}
////验证数据正确性的代码 https://www.py.cn/faq/python/17698.html
//计算年初到今天这天有多少天
int thisYearGoDays(struct tm * t,int year,int month,int day){
int days = 0;
if((t->tm_year + 1900)==year){
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
for(int i=month-1;i<t->tm_mon+1-2;i++){
days+= leapDays[i];
}
days+= t->tm_mday-day-1;
// printf("今天是%d年%d月%d日,此年是闰年,从年初距离现在共有%d天.\n",year,month,day,days);
}else{
for(int i=month-1;i<t->tm_mon+1-2;i++){
days+= ordinaryDays[i];
}
days+= t->tm_mday-day-1;
//printf("今天是%d年%d月%d日,此年是平年,从年初距离现在共有%d天.\n",year,month,day,days);
}
}else if((t->tm_year + 1900)>year){
if(((year%4==0)&&(year%100!=0))||(year%400==0)) {
for(int i=0;i<t->tm_mon+1-1;i++){
days+= leapDays[i];
}
days+= t->tm_mday-1;
// printf("今天是%d年%d月%d日,此年是闰年,从年初距离现在共有%d天.\n",year,month,day,days);
}else{
for(int i=0;i<t->tm_mon+1-1;i++){
days+= ordinaryDays[i];
}
days+= t->tm_mday-1;
//printf("今天是%d年%d月%d日,此年是平年,从年初距离现在共有%d天.\n",year,month,day,days);
}
}
return days;
}
int main(void){
int year, month,day;
printf("请输入你的生日(格式:年-月-日):");
scanf("%d-%d-%d",&year,&month,&day);
int birthdayDays ;
//https://blog.csdn.net/qq_39838607/article/details/120169453
struct tm * t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
t = localtime( &now); //获取当地日期和时间
//格式化输出本地时间
// printf("年:%d\n", t->tm_year + 1900);
// printf("月:%d\n", t->tm_mon + 1);
// printf("日:%d\n", t->tm_mday);
long betweenDays ;
// printf("betweenDays=%ld\n",betweenDays);
////计算年初到今天这天有多少天
int thisYearDays ;
// printf("%d\n",thisYearDays) ;
thisYearDays=thisYearGoDays(t,year,month,day) ;
// printf("thisYearDays=%d\n",thisYearDays);
long allDays = 0;
if(year==(t->tm_year + 1900)){
// printf("year=%d\n",year);
birthdayDays = 0;
betweenDays = 0;
allDays = (long)(birthdayDays+thisYearDays+betweenDays);
}else if(year<(t->tm_year + 1900)){
//计算生日的那年距离此年最后一天共有多少天
birthdayDays =birthdayYearDays(year,month,day)+1 ;
// printf("birthdayDays=%d\n",birthdayDays);
////计算两个年份的1月1号之间共有多少天数
betweenDays = getDays(year+1,t->tm_year + 1900)+1;
// printf("betweenDays=%d\n",betweenDays);
allDays = (long)(birthdayDays+thisYearDays+betweenDays);
}
printf("从你的生日公元%d年%d月%d日,距离今天公元%d年%d月%d日共有%ld天。\n",year,month,day,t->tm_year + 1900,t->tm_mon+1,t->tm_mday,allDays);
return 0;
}