C++封装一个Date类,实现简单的日历程序


程序代码如下:


Date.h


#include<iostream>
using namespace std;class Date
{public:Date(int year = 2000, int month = 1, int day = 1);Date(const Date& d);//赋值运算符重载Date& operator=(const Date& d);//前置++Date& operator++();//后置++Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//计算日期是那一年的第几天int DaysInYear(int year, int month, int day);//计算当前日期向后n天的日期Date Date::operator+(int days);//计算当前日期向前n天的日期Date Date::operator-(int days);//计算两个日期相差天数int operator-(Date& d);//判断两个日期是否相等bool operator==(const Date& d)const;bool operator!=(const Date& d)const;bool operator>(const Date& d)const;bool operator>=(const Date& d)const;bool operator<(const Date& d)const;bool operator<=(const Date& d)const;private://交换函数void Swap(int& left, int& right);//判断是否为闰年bool IsLeap(int year);//获取当月天数int GetDaysOfMonth(int year, int month);//判断日期是否合法bool IsDateValid(int year, int month, int day);private:int _year;int _month;int _day;friend ostream& operator<<(ostream& _cout, const Date& d);friend istream& operator>>(istream& _cin, Date& d);};class Calendar
{public:Calendar(int year = 2000);bool IsLeap(int year);int GetDaysOfMonth(int year, int month);void PrintCalendar();void PrintTitle(int m);
private:int _year;int _weekday;
};

Date.c

#define _CRT_SECURE_NO_WARNINGS#include "Date.h"//日期类
Date::Date(int year, int month, int day)
: _year(year)
, _month(month)
, _day(day)
{if (!IsDateValid()){cout << "日期非法!使用默认值2000.1.1" << endl;_year = 2000;_month = 1;_day = 1;}
}Date::Date(const Date& d)
: _year(d._year)
, _month(d._month)
, _day(d._day)
{}//赋值运算符重载
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}//交换函数
void Date::Swap(int& left, int& right)
{int tmp = left;left = right;right = tmp;
}//判断是否为闰年
bool Date::IsLeap(int year)
{if (((0 == year % 4) && (0 != year % 100)) ||(0 == year % 400))return true;elsereturn false;
}//获取当月天数
int Date::GetDaysOfMonth(int year,int month)
{//方法一int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && IsLeap(year))return days[month] + 1;elsereturn days[month];//方法二/*switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;case 4:case 6:case 9:case 11:return 30;case 2:if (IsLeap(year))return 29;elsereturn 28;}*/
}//判断日期是否合法
bool Date::IsDateValid()
{if (_year <1 || (_month < 1 || _month > 12) ||(_day<1 || _day>GetDaysOfMonth(_year, _month)))return false;elsereturn true;
}//前置++
Date& Date::operator++()
{if (_day < GetDaysOfMonth(_year,_month))++_day;else{if (_month < 12){++_month;_day = 1;}else{++_year;_month = 1;_day = 1;}}return *this;
}//后置++
Date Date::operator++(int)
{Date tmp(*this);if (_day < GetDaysOfMonth(_year, _month))++_day;else{if (_month < 12){++_month;_day = 1;}else{++_year;_month = 1;_day = 1;}}return tmp;
}//前置--
Date& Date::operator--()
{if (_day > 1)--_day;else{if (_month > 1){--_month;_day = GetDaysOfMonth(_year, _month);}else{--_year;_month = 12;_day = 31;}}return *this;
}
//后置--
Date Date::operator--(int)
{Date tmp = *this;if (_day > 1)--_day;else{if (_month > 1){--_month;_day = GetDaysOfMonth(_year, _month);}else{--_year;_month = 12;_day = 31;}}return tmp;
}//计算日期是那一年的第几天
int Date::DaysInYear(int year, int month, int day)
{int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (IsLeap(year))days[2] = 29;for (int i = 1; i < month; ++i){day += days[i];}return day;
}//计算当前日期向后n天的日期
Date Date::operator+(int days)
{if (days<0)return *this - (0 - days);Date tmp(*this);tmp._day += days;int DaysOfMonth = 0;while (tmp._day > (DaysOfMonth = GetDaysOfMonth(tmp._year, tmp._month))){tmp._day -= DaysOfMonth;++tmp._month;if (tmp._month > 12){++tmp._year;tmp._month = 1;}}return tmp;
}//计算当前日期向前n天的日期
Date Date::operator-(int days)
{if (days<0)return *this + (0 - days);Date tmp(*this);tmp._day -= days;while (tmp._day <= 0){--tmp._month;if (tmp._month < 1){--tmp._year;tmp._month = 12;}tmp._day += GetDaysOfMonth(tmp._year, tmp._month);}return tmp;
}//计算两个日期相差天数
int Date::operator-(Date& d)
{//同年同月if (_year == d._year && _month == d._month)return _day > d._day ? (_day - d._day) : (d._day - _day);//同年else if (_year == d._year){int d1, d2;d1 = DaysInYear(_year, _month, _day);d2 = DaysInYear(d._year, d._month, d._day);return d1 > d2 ? (d1 - d2) : (d2 - d1);}//年、月都不相同else{int flag = 0;//确保_year日期比d._year大if (_year < d._year){Swap(_year, d._year);Swap(_month, d._month);Swap(_day, d._day);flag = 1;}int d1, d2, d3 = 0;//1.求小的日期在该年剩余天数if (IsLeap(d._year))d1 = 366 - DaysInYear(d._year, d._month, d._day);elsed1 = 365 - DaysInYear(d._year, d._month, d._day);//2.求大的日期是该年的第几天d2 = DaysInYear(_year, _month, _day);//3.求两个日期中间相差几个整年for (int i = d._year+1; i < _year; ++i){if (IsLeap(i))d3 += 366;elsed3 += 365;}if (1 == flag){Swap(_year, d._year);Swap(_month, d._month);Swap(_day, d._day);}return d1 + d2 + d3;}}//判断两个日期是否相等
bool Date::operator==(const Date& d)const
{return _year == d._year &&_month == d._month &&_day == d._day;
}bool Date::operator!=(const Date& d)const
{return !(*this == d);
}bool Date::operator>(const Date& d)const
{if (_year > d._year ||_year == d._year && _month > d._month ||_year == d._year && _month == d._month && _day > d._day){return true;}return false;
}
bool Date::operator>=(const Date& d)const
{return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d)const
{return !(*this >= d);
}
bool Date::operator<=(const Date& d)const
{return (*this < d) || (*this == d);
}//输入运算符重载
istream& operator>>(istream& _cin, Date& d)
{_cin >> d._year >> d._month >> d._day;while (!d.IsDateValid()){cout << "日期非法!请重新输入:";_cin >> d._year >> d._month >> d._day;}return _cin;
}//输出运算符重载
ostream& operator<<(ostream& _cout, const Date& d)
{_cout << d._year << "-" << d._month << "-" << d._day;return _cout;
}//日历类
#include <iomanip>
Calendar::Calendar(int year)
:_year(year)
{if (_year < 1){cout << "年份非法!使用默认值2000" << endl;_year = 2000;}
}bool Calendar::IsLeap(int year)
{if (((0 == year % 4) && (0 != year % 100)) ||(0 == year % 400))return true;elsereturn false;
}int Calendar::GetDaysOfMonth(int year, int month)
{//方法一int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && IsLeap(year))return days[month] + 1;elsereturn days[month];
}void Calendar::PrintCalendar()
{for (int n = 1; n < 13; n++){PrintTitle(n);//基姆拉尔森计算公式---求某年某月一号对应的星期if (1 == n || 2 == n){_weekday = (1 + 2 * (n + 12) + 3 * (n + 12 + 1) / 5 + _year - 1 +(_year - 1) / 4 - (_year - 1) / 100 + (_year - 1) / 400 + 1) % 7;}else{_weekday = (1 + 2 * n + 3 * (n + 1) / 5 + _year +_year / 4 - _year / 100 + _year / 400 + 1) % 7;}for (int i = 0; i < _weekday; i++){cout << setw(4) << " ";//每月一号和对应的星期对齐}int days = GetDaysOfMonth(_year, n);for (int j = 1; j <= days; j++){cout << setw(4) << j;_weekday = (_weekday + 1) % 7;if (0 == _weekday)cout << endl;}cout << endl/* << "____________________________" << endl*/;}}void Calendar::PrintTitle(int m)
{cout << endl;cout << "             " << m << "月             " << endl;cout << "-----------------------------" << endl;cout << "  日  一  二  三  四  五  六  " << endl;cout << "-----------------------------" << endl;}

test.c

#define _CRT_SECURE_NO_WARNINGS#include "Date.h"//测试打印全年日历到桌面
void TestFunc()
{int year;cout << "请输入你要查看的年份:";cin >> year;cout << year << "年全年日历如下:" << endl;Calendar C(year);C.PrintCalendar();
}void Menu1();
void Menu2();void Menu0(int flag)
{int i;cout << endl;cout << "************************************" << endl;cout << "            1.返回上一页            " << endl;cout << "            0.退出                  " << endl;cout << "************************************" << endl;cout << "请选择:";cin >> i;switch (i){case 0:cout << "退出成功,祝您生活愉快!" << endl;return;case 1:if (1 == flag)Menu1();elseMenu2();}
}void Menu2()
{int i;int flag = 2;cout << endl;cout << "********************************" << endl;cout << "      1.日期之间的间隔时间      " << endl;cout << "      2.增加天数                " << endl;cout << "      3.减去天数                " << endl;cout << "      4.返回上一页              " << endl;cout << "      0.退出                    " << endl;cout << "********************************" << endl;cout << "请选择:";cin >> i;switch (i){case 0:cout << "退出成功,祝您生活愉快!" << endl;return;case 1:{Date d1;Date d2;cout << "请输入第一个日期:";cin >> d1;cout << "请输入第二个日期:";cin >> d2;cout << d1 << "与" << d2 << "间隔" << d1 - d2 << "天" << endl;Menu0(flag);break;}case 2:{Date d1;Date d2;int days;cout << "请输入日期:";cin >> d1;cout << "请输入要增加的天数:";cin >> days;d2 = d1 + days;cout << d1 << "的100天后的日期:" << d2 << endl;Menu0(flag);break;}case 3:{Date d1;Date d2;int days;cout << "请输入日期:";cin >> d1;cout << "请输入要减去的天数:";cin >> days;d2 = d1 - days;cout << d1 << "的100天前的日期:" << d2 << endl;Menu0(flag);break;}case 4:Menu1();break;}
}void Menu1()
{int i;int flag = 1;cout << endl;cout << "欢迎使用每天日历,记录每天的美好生活:)" << endl;cout << "************************************" << endl;cout << "   1.查看全年日历      2.日期计算   " << endl;cout << "   0.退出                           " << endl;cout << "************************************" << endl;cout << "请选择:";cin >> i;switch (i){case 0:cout << "退出成功,祝您生活愉快!" << endl;return;case 1:TestFunc4();Menu0(flag);break;case 2:Menu2();}
}int main()
{//TestFunc();Menu1();return 0;
}

程序运行结果如下:


看截图不太明白的话,可以拷代码到自己的编译器运行查看

1.查看全年日历(只展示部分)

2.两个日期间的间隔天数

3.增加天数

4.减去天数

日历---C++封装一个Date类,Calendar类,实现简单的日历+日期计算器程序相关推荐

  1. 14.常见对象(正则表达式,Pattern和Matcher类,Math类,Random类,System类,BigDecimal类,Date类,SimpleDateFormat类,Calendar类)

    1.正则表达式的概述和简单使用 1.正则表达式:正确规则的表达式 规则java给我们定的     是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.其实就是一种规则.有自己特殊的应用 ...

  2. java 日历工具_java之日历处理工具类Calendar类

    编程语言 java之日历处理工具类Calendar类 字号+ 作者:小虾米 2016-11-08 12:43 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_O ...

  3. java Date 和 Calendar类 万字详解(通俗易懂)

    Date类介绍 Date类构造器 Date类使用 关于SimpleDateFormat类 Date类对象的格式化 构造Date对象的补充 Date类对象成员方法 Calendar类介绍及使用 字段演示 ...

  4. Java中的常用类——Calendar类

    Calendar类 主要用于完成日期和时间字段的操作,他可以通过特定的方法来读取日期.Calendar类是一个抽象类,不可以被实例化,需要通过调用静态方法**getInstance()**来得到一个c ...

  5. Java学习(16)--System 类/Date 类/ Calendar类

    System (1)系统类 ,提供了一些有用的字段和方法 (2)成员方法  A:运行垃圾回收器 public static void gc() B:退出 jvm public static void ...

  6. java中calendarr,Java学习(16)--System 类/Date 类/ Calendar类

    System (1)系统类 ,提供了一些有用的字段和方法 (2)成员方法 A:运行垃圾回收器 public static void gc() B:退出 jvm public static void e ...

  7. String类 Object类 System类 Math类 Date类 Calendar类 DateFormat类

    API 全称Application Programming Interface,即应用程序编程接口. API是一些预先定义函数,目的是用来提供应用程序与开发人员基于某软件或者某硬件得以访问一组例程的能 ...

  8. 【Java6】Date类/Calendar类,System类/Math类,包装类,集合,泛型,内部类

    文章目录 1.Date类:getTime(),SimpleDateFormat 2.Calendar类:只有子类对象才能向上转型 3.System类:System.exit(0) 4.Math类:ce ...

  9. Java常用接口与类——Date、Calendar、DateFormat、TimeZone(日期相关)

    >Date类和DateFormat类 Date d=new Date(); //System.out.println(d.toLocaleString()); SimpleDateFormat ...

最新文章

  1. python 字典排序 最大键_Python中的列表、元祖、字典、集合操作大全
  2. Visual Studio 2010在简洁中强调团队合作
  3. centos memcached php,centos系统为php安装memcached扩展步骤
  4. 解秘亿级网站的一本书——亿级流量网站架构核心技术
  5. 10 分钟上手 Vim,常用命令大盘点
  6. ssl1747-登山机器人【离散化,玄学,贪心】
  7. JSON合并补丁:JSON-P 1.1概述系列
  8. Node.js下载安装及各种npm、cnpm、nvm、nrm配置(保姆式教程—提供全套安装包)—nvm的安装与配置(4)
  9. asyncio 文件io高并发_请问这个 asyncio 异步访问页面怎么写可以更加快?
  10. 远程接入CBTS的应用
  11. 【DTM】HUAWEI Ads与DTM网页转化追踪(二)
  12. 项目管理知识体系指南 (一)
  13. mysql group by 用法解析
  14. 一些非常有趣的python爬虫例子
  15. 2022国赛数学建模思路 - 复盘:生成规划模型
  16. 泰国旅游必拍照打卡景点推荐,高清靓图欣赏
  17. webstorm 2018 激活破解方法大全 亲测第三个有用
  18. java邮箱地址正则表达式_JAVA 电子邮箱格式验证,使用正则表达式
  19. [清风数学建模]层次分析法(AHP)笔记及代码实现
  20. PERT(计划评审技术Program Evaluation an Review Technique)

热门文章

  1. 04 cefsharp谷歌浏览器多开页面的实现
  2. 深度长文:AMD的崛起、衰落与复兴
  3. unity退出,从新开始,暂停
  4. 转一篇有关竞争对手LANDESK CEO的故事
  5. redis 集群 实操 (史上最全、5w字长文)
  6. https网站地图生成工具
  7. ubuntu 调 2K 分辨率
  8. CentOS 7 网络配置
  9. AI教育公司北极星获数千万Pre-A 轮融资,估值2.5亿元
  10. 宏碁暗影骑士擎2022-重装系统-扬声器无声音