2025年日历计算器

日历计算器有时候 我们需要知道某个日期的多少天后是哪一天 或者多少天之前是哪一天 甚至需 要计算两个日期之间隔了多久等等 此篇文章用来实现日历计算器 包括以下功能 1 日期加天数 2 日期减天数 3 日期减日期 4 打印给定月份的日期 下边就各个功能予以分析

大家好,我是讯享网,很高兴认识大家。

有时候,我们需要知道某个日期的多少天后是哪一天,或者多少天之前是哪一天,甚至需

要计算两个日期之间隔了多久等等。

此篇文章用来实现日历计算器,包括以下功能:

1.日期加天数

2.日期减天数

3.日期减日期

4.打印给定月份的日期

下边就各个功能予以分析:

    日期加天数】我们只需要在给定日期的day上加上天数,然后调整日期为正确的日

期。比如:2016,8,18+20 = 2016,8,38.得到的天数必然大于0,所以只需比较天数 与当

月天数的大小关系,明显38大于8月的天数31,所以,先用38减去8月的天数31,然后

将月份加1,即就是2016,9,7,由于7小于9月的天数30,所以日期调整结束。

    【日期减天数】我们只需要在给定日期的day上减去天数,然后调整日期为正确的日

期。比如,2016,8,18-20=2016,8,-2,得到的天数必然小于当月天数,所以,只需比较

天数与0的关系,明显-2小于0,所以,先将月份减1,再用-2加上当前月的天数,即就是

2016,7,29,29正好小于7月的天数31,调整结束。

   【日期减日期】找到两个日期中较小的日期,借助计数器,较小的日期每加1一次,计

数器加1,直到较小的日期等于较大的日期,此时返回计数器即可。


讯享网

   【打印给定月份的日期】对于给定的月份,只需直到当前月份任意一天是周几就可以

了,但是为了好算,我们算出当前月份1号是周几就可以打印日历了。

而计算某一天是周几有 两种办法:

方法一:给定一个日期零点,其他的日期只需计算和它的差值。然后差值模7,如果结

果是0,说明两个日期同为周几。举例说明,日期零点给定为1900年1月1日,星期1,

比如要打印2016年8月的日历,只需要用2016,8,1-1900,1,1,然后差值模7,若结果是

0,说明2016,8,1星期1;如果是1,说明 2016,8,1星期2,以此类推,结果是6就是星期

日。

方法二:蔡勒公式。

w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1

各个符号的意义自己可以查一下~~

特别注意,1月,2月一定要看成上一年的13月,14月。

下边给出源代码:

#include<iostream> #include<cstdio> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) :m_year(year) , m_month(month) , m_day(day) { if (IsCorrectDate()) { m_year = 1900; m_month = 1; m_day = 1; } } void Display()const { cout << m_year << "-" << m_month << "-" << m_day << endl; } public: bool IsCorrectDate() { if ((m_year < 1) || ((m_month < 1) || (m_month>12)) || (m_day<1) || (m_day>DaysOfMonth(*this))) { return true; } else { return false; } } int DaysOfMonth(const Date &d)const { static int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (d.m_month == 2) { return arr[d.m_month] + IsLeap(d); } else { return arr[d.m_month]; } } int IsLeap(const Date &d)const { if (((d.m_year % 4 == 0) && (d.m_year % 100 != 0)) || (d.m_year % 400 == 0)) return 1; else return 0; } bool operator==(const Date & d) { if ((m_year == d.m_year) && (m_month == d.m_month) && (m_day == d.m_day)) return true; else return false; } Date operator+(int count) { Date tmp(*this); tmp.m_day += count; //校正日期 ToCorrectDate(tmp); return tmp; } Date operator-(int count) { Date tmp(*this); tmp.m_day -= count; //校正日期 ToCorrectDate(tmp); return tmp; } void ToCorrectDate(Date &d) { if (d.m_day <= 0) { while (d.m_day <= 0) { d.m_month--; if (d.m_month < 1) { d.m_year--; d.m_month = 12; } d.m_day += DaysOfMonth(d); } } else { while (d.m_day > DaysOfMonth(d)) { (d.m_day) -= DaysOfMonth(d); d.m_month++; if (d.m_month > 12) { d.m_year++; d.m_month = 1; } } } } int operator-(Date d) { Date tmp; int count = 0; if (d > *this) { tmp = *this; } else { tmp = d; d = *this;//大日期在d中存储,小日期在tmp中 } while (tmp != d) { count++; tmp = tmp + 1; } return count; } bool operator>(Date& d)const { if (m_year > d.m_year) { return true; } else if (m_year == d.m_year) { if (m_month > d.m_month) { return true; } else if (m_month == d.m_month) { if (m_day > d.m_day) { return true; } } } else return false; } bool operator!=(Date &d) { return !(*this == d); } void showMonth()const { cout << "日 一 二 三 四 五 六 " << endl; int year = 0; int month = 0; //w = y + [y / 4] + [c / 4] - 2c + [26(m + 1) / 10] + d - 1 if ((m_month == 1) || (m_month == 2)) { year = m_year - 1; month = m_month + 12; } else { year = m_year ; month = m_month ; } int w = year % 100 + (year % 100 / 4) + (year / 400) - 2 * (year / 100) + (26 * (month + 1) / 10) ; //cout << (w % 7) << endl; for (int i = 0; i < (w % 7);i++) { cout << " "; } for (int i = 0; i < 7 - (w % 7);i++)//处理第一行 { //cout << (i+1)<<" "; printf("%2d ",i+1); } cout << endl; for (int i = 7 - (w % 7);i < DaysOfMonth(*this);i++) { //cout << (i + 1) << " "; printf("%2d ", i + 1); if ((i + 1 + w % 7) % 7 == 0) { cout << endl; } } cout << endl; } private: int m_year; int m_month; int m_day; }; void menu() { cout << "" << endl; cout << "--------欢迎使用此万年历----------" << endl; cout << "-----------1.日期加天数-----------" << endl; cout << "-----------2.日期减天数-----------" << endl; cout << "-----------3.日期减日期-----------" << endl; cout << "-----------4.打印某月的日历-------" << endl; cout << "-----------0.退出-----------------" << endl; cout << "" << endl; } int main() { int choose = 1; int year = 0; int month = 0; int day = 0; int count = 0; while (choose) { menu(); cout << "请选择>:"; cin >> choose; switch (choose) { case 0: break; case 1: { cout << "请输入日期>:"; cin >> year >> month >> day; Date d(year, month, day); cout << "请输入间隔天数>:"; cin >> count; d = d + count; d.Display(); break; } case 2: { cout << "请输入日期>:"; cin >> year >> month >> day; Date d(year, month, day); cout << "请输入间隔天数>:"; cin >> count; d = d - count; d.Display(); break; } case 3: { cout << "请输入第一个日期>:"; cin >> year >> month >> day; Date d1(year, month, day); cout << "请输入第二个日期>:"; cin >> year >> month >> day; Date d2(year, month, day); int count = d1 - d2; cout << count << endl; break; } case 4: { cout << "请输入要打印的月份" << endl; cin >> year >> month; Date d(year, month); d.showMonth(); break; } default: { cout << "选择错误" << endl; break; } } } system("pause"); return 0; }

讯享网

当然你可以根据自己的喜好使用颜色代码设置不同的颜色来显示日历。

很多事情的累积都是从所谓的最低位开始,正如日期加减天数,都是直接处理天,然后

调整日期的。通过这个思想,我又想到了一道算法题目,下次再分享~~


小讯
上一篇 2025-02-15 15:09
下一篇 2025-03-29 07:42

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/52233.html