日历计算器
来源:互联网 发布:淘宝无线视频看不到了 编辑:程序博客网 时间:2024/05/17 05:01
有时候,我们需要知道某个日期的多少天后是哪一天,或者多少天之前是哪一天,甚至需
要计算两个日期之间隔了多久等等。
此篇文章用来实现日历计算器,包括以下功能:
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; } #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;elsereturn 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;}}}elsereturn 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 - 1if ((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;}
- 日历计算器
- 日历计算器
- 日历计算器
- 第九周(日历计算器)
- Linux 日历和计算器命令
- JAVA综合(系统信息,定时关机,计算器,日历,通讯录)
- C#实现日历样式的下拉式计算器
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- 日历
- c++ vector 基本用法 (3)
- PHP快速入门
- Struts2工作流程
- CRC的校验原理以及例子
- 复合事件
- 日历计算器
- Target Sum
- 数据库中隔离性的四种级别详解与例子
- hdoj 6025 Coprime Sequence
- 1.SparkRDD join案例
- 我是一名工程师, 我真的够牛逼, 能要求人性化的管理吗?!
- HDU6029-Graph Theory-图论简单推理
- C 学习一:令牌(Tokens)、关键字
- 新版Solr中的core概念