1149 字
3 分钟
C++ 日期计算器
日期计算器
-
- Date类的规划
- Date类的实现
-
- Date 构造函数
- Date 拷贝构造函数
- ~Date 析构函数
- GetMonthDay 求某年某月的天数
- operator= 赋值操作符重载
- operator+= 加等操作符重载
- operator+ 加号操作符重载
- operator-= 减等操作符重载
- operator- 减法操作符重载 (日期 - 天数)
- operator++ 前置自增操作符重载
- operator++ 后置自增操作符重载
- operator— 前置自减操作符重载
- operator— 后置自减操作符重载
- operator< 小于操作符重载
- operator== 相等操作符重载
- operator!= 不等操作符重载
- operator<= 小于或等于操作符重载
- operator> 大于操作符重载
- operator>= 大于或等于操作符重载
- operator- 减法操作符重载(日期 - 日期)
- Print 打印函数
- Date 类的测试

概要
本篇主要探究C++ 实现日期计算器
Date类的规划
class Date{public: int GetMonthDay(int year, int month); Date(int year = 2024 , int month = 2, int day = 6); Date(const Date& d); Date& operator=(const Date& d); ~Date(); Date& operator+=(int day); Date operator+(int day); Date operator-(int day); Date& operator-=(int day); Date& operator++(); Date operator++(int); Date operator--(int); Date& operator--(); 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; int operator-(const Date& d) const; void Print();
private: int _year; int _month; int _day;};Date类的实现
Date 构造函数
Date::Date(int year, int month, int day) :_year(year) , _month(month) , _day(day){ }构造函数,他是在创建类的时候调用进行初始化操作,我们这里声明与定义分离,所以它的参数不需要填写缺省值,缺省值在声明的时候定义即可。
Date 拷贝构造函数
Date::Date(const Date& x) :_year(x._year) , _month(x._month) , _day(x._day){ }拷贝构造函数和构造函数作用相似,拷贝构造函数是将已有的类的对象拷贝给新创建的类的对象,如上所示。
~Date 析构函数
Date::~Date(){ _year = _month = _day = 0;}构析函数是在对象即将销毁前所调用的函数,常用来清理数据空间,这里我们的内存都在栈上申请的,所以影响不大。
GetMonthDay 求某年某月的天数
int Date::GetMonthDay(int year,int month){ int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if((month==2)&&((year%4==0&&year%100!=0)||(year%400==0))) { return 29; } return a[month];}该函数是用来求某年某月的月份天数,通过创建一个大小为13的数组a, 再对二月份进行判断闰年来确定是否为特殊情况,否则返回已设定好的月份天数。
operator= 赋值操作符重载
Date& Date::operator=(const Date& d){ _year=d._year; _month=d._month; _day=d._day; return *this;}该函数用于对象与对象之间的赋值操作,在对象的年月日进行逐一复制后并返回this指针的解引用。
operator+= 加等操作符重载
Date& Date::operator+=(int day){ _day+=day; int time=GetMonthDay(_year,_month); while(_day>time) { _day-=time; _month++; if(_month>12) { _month-=12; _year++; } time = GetMonthDay(_year,_month); } return *this;}该函数用于日期与天数的相加,在实现该函数时先将this->_day加上想加的day。然后再通过月份的天数以及月与年的关系进行调整,如上所示。
operator+ 加号操作符重载
Date Date::operator+(int day){ Date newdate = *this; newdate+=day; return newdate;}通过复用加等操作符重载,在不改变*this的情况下,返回相加后的对象
operator-= 减等操作符重载
Date& Date::operator(int day){ _day-=day; int time =GetMonthDay(_year,_month) while(_day>=0) { _day+=time; _month--; if(_month<=1) { _month+=12; _year--; } time=GetMonthDay(_year,_month); } return *this;}该函数和加等操作符重载类似,先将this->_day减去day,然后进行月份的天数和年与月之间的调整即可。
operator- 减法操作符重载 (日期 - 天数)
Date Date::operator-(int day){ Date newdate=*this; newdate-=day; return newdate;}通过复用减等操作符重载,在不改变*this的情况下,返回相加后的对象
operator++ 前置自增操作符重载
Date& Date::operator++(){ (*this)+=1; return *this;}operator++ 后置自增操作符重载
Date Date::operator--(int){ Date a = *this; (*this)+=1; return a;}operator— 前置自减操作符重载
Date& Date::operator--(){ (*this)-=1; return *this;}operator— 后置自减操作符重载
Date Date::operator--(int){ Date a=*this; (*this)-=1; return a;}operator< 小于操作符重载
bool Date::operator<(const Date& d){ if(_year<d._year) { return 1; } else if(_year==d._year) { if(_month<d._month) { return 1; } else if(_month==d._month) { if(_day<d._day) { return 1; } } } return 0;}operator== 相等操作符重载
bool Date::operator==(const Date& d){ return _day==d._day&&_month==d._month&&_year==d._year;}operator!= 不等操作符重载
bool Date::operator!=(const Date& d){ return !(*this==d);}operator<= 小于或等于操作符重载
bool Date::operator<=(const Date& d){ return (*this<d)||(*this==d);}operator> 大于操作符重载
bool Date::operator>(const Date& d){ return !(*this<=d);}operator>= 大于或等于操作符重载
bool Date::operator>=(const Date& d){ return !(*this<d);}operator- 减法操作符重载(日期 - 日期)
int operator-(const Date& date) const{ Date max = *this,min = date; int flag=(max>=min); if(!flag) { max=date; min=*this; flag=-1; } int count=0; while(max>min) { min++; count++; } return flag*count;}通过比较选出最大的日期max与最小的日期min,让最小的日期min和记录数的count进行循环自增,直到max与min相等为止,此时刻的count就是相差的天数,而flag先前用于比较的就是它的符号,所以返回flag*count。
Print 打印函数
void Date::Print(){ printf("%d %d %d\n",_year,_month,_day);}Date 类的测试
测试操作符重载(+,+=,- ,-=,x++,++x,x–,–x)
void Test_1(){ std::cout << "Test_1:\n"; Date a(2024, 2, 6); std::cout << "a ||"; a.Print(); Date b = a; std::cout << "b ||"; b.Print(); b += 30; std::cout << "b += 30 ||"; b.Print(); a -= 20; std::cout << "a -= 20 ||"; a.Print(); Date c = b - 30; std::cout << "c 或 b - 30 ||"; c.Print(); Date d = a + 20; std::cout << "d 或 a + 20 ||"; d.Print(); d++; std::cout << "++d ||"; d.Print(); c--; std::cout << "++c ||"; c.Print(); Date e = a++; std::cout << "e = a++ -> e||"; e.Print(); std::cout << "e = a++ -> a||"; a.Print(); Date f = a++; std::cout << "f = b-- -> f||"; f.Print(); std::cout << "f = b-- -> b||"; b.Print();
std::cout << "\n";}运行结果如下:

测试操作符重载(>,>=,==,<,<=,!=)
void Test_2(){ std::cout << "Test_2:\n"; Date a(2024, 2, 6); std::cout << "a ||"; a.Print(); Date b = a + 999; std::cout << "b ||"; b.Print(); Date c = a - 999; std::cout << "c ||"; c.Print(); Date d = a; std::cout << "d ||"; d.Print();
std::cout << "a < b ->" << (a < b) << std::endl; std::cout << "a > b ->" << (a > b) << std::endl; std::cout << "a == b ->" << (a == b) << std::endl; std::cout << "a != b ->" << (a != b) << std::endl;
std::cout << "a < c ->" << (a < c) << std::endl; std::cout << "a > c ->" << (a > c) << std::endl; std::cout << "a == c ->" << (a == c) << std::endl; std::cout << "a != c ->" << (a != c) << std::endl;
std::cout << "a == d ->" << (a == d) << std::endl; std::cout << "a != d ->" << (a != d) << std::endl; std::cout << "a >= d ->" << (a >= d) << std::endl; std::cout << "a <= d ->" << (a <= d) << std::endl; std::cout << "a < d ->" << (a < d) << std::endl; std::cout << "a > d ->" << (a > d) << std::endl;
std::cout << "\n";}运行结果如下:

测试操作符重载(日期 - 日期)
void Test_3(){ std::cout << "Test_3:\n"; Date a(2024, 2, 6); Date b(2025, 2, 6); Date c = a + 99; std::cout << "c ||"; c.Print();
std::cout << "a ||"; a.Print(); std::cout << "b ||"; b.Print();
std::cout << "a - b ||" << (a - b) << std::endl; std::cout << "b - b ||" << (b - b) << std::endl; std::cout << "b - a ||" << (b - a) << std::endl;
std::cout << "c - a ||" << (c - a) << std::endl; std::cout << "a - c ||" << (a - c) << std::endl;
std::cout << "\n";}运行结果如下:

测试完毕,综合上述代码及运行结果,可得代码正确,可以正常模拟日期计算器。
结语
以上就是本期的全部内容了,喜欢就多多关注吧!!!
分享
如果这篇文章对你有帮助,欢迎分享给更多人!
部分信息可能已经过时
相关文章 智能推荐
1
你好,C++对象
C++ 从类和对象的基本概念出发,讲解封装、构造与析构等默认成员函数,建立 C++ 面向对象编程的基础认识。
2
C++ 入门必备知识
C++ 整理 C++ 入门阶段的重要知识,包括命名空间、输入输出、引用、内联函数、auto 与 nullptr。
3
解开缺省参数与函数重载的衣裳
C++ 讲解 C++ 缺省参数与函数重载的语法、匹配规则和使用场景,并结合编译过程理解函数名修饰。
4
[Linux - C] 自主 Shell
Linux 使用 C 语言实现一个简易 Shell,涵盖命令读取与解析、子进程创建、程序替换以及内建命令处理。
5
位操作符(&、|、^) C语言
C语言 结合二进制表示和代码示例讲解按位与、按位或、按位异或等位操作符的规则与常见用途。