万年历查询显是否闰年、查星期几、某月日历、某年日历、退出
发布网友
发布时间:2022-07-20 04:33
我来回答
共2个回答
热心网友
时间:2023-10-19 11:11
//日期函数
int days[12] = {
31,28,31,30,31,30,31,31,30,31,30,31
};
struct Date {
int year, month, day;
Date() {
}
Date(int _year, int _month, int _day)
: year(_year), month(_month), day(_day) {
}
};
//判闰年
inline int leap(int year) {
return (year%4==0&&year%100!=0)||year%400==0;
}
//判合法性
inline int legal(Date a) {
if(a.month < 0 || a.month > 12) {
return 0;
}
if(a.month==2) {
return a.day > 0 && a.day <= 28 + leap(a.year);
}
return a.day > 0 && a.day <= days[a.month - 1];
}
//比较日期大小
inline int datecmp(Date a, Date b) {
if(a.year != b.year) {
return a.year - b.year;
}
if(a.month != b.month) {
return a.month - b.month;
}
return a.day - b.day;
}
//返回指定日期是星期几
int weekday(Date a) {
int tm = a.month >= 3 ? (a.month - 2) : (a.month + 10);
int ty = a.month >= 3 ? a.year : (a.year - 1);
return (ty + ty / 4 - ty / 100 + ty / 400 +
(int)(2.6 * tm - 0.2) + a.day) % 7;
}
//日期转天数偏移
int date2int(Date a) {
int ret = a.year * 365 + (a.year - 1) / 4 -
(a.year - 1) / 100 + (a.year - 1) / 400;
days[1] += leap(a.year);
for (int i = 0; i < a.month - 1; ret += days[i++]);
days[1] = 28;
return ret + a.day;
}
//天数偏移转日期
Date int2date(int a) {
Date ret;
ret.year = a / 146097 * 400;
for (a %= 146097 ; a >= 365 + leap(ret.year);
a -= 365 + leap(ret.year), ret.year++);
days[1] += leap(ret.year);
for ( ret.month = 1; a >= days[ret.month-1];
a -= days[ret.month-1], ret.month++);
days[1] = 28;
ret.day = a + 1;
return ret;
}
//看是否有帮助
热心网友
时间:2023-10-19 11:11
什么语言的?