当前位置:首页 » 《随便一记》 » 正文

C++ 日期计算器

18 人参与  2024年03月01日 13:01  分类 : 《随便一记》  评论

点击全文阅读


日期计算器

概要 Date类的规划Date类的实现Date 构造函数Date 拷贝构造函数~Date 析构函数GetMonthDay 求某年某月的天数operator= 赋值操作符重载operator+= 加等操作符重载operator+ 加号操作符重载operator-= 减等操作符重载operator- 减法操作符重载 (日期 - 天数)operator++ 前置自增操作符重载operator++ 后置自增操作符重载operator-- 前置自减操作符重载operator-- 后置自减操作符重载operator< 小于操作符重载operator== 相等操作符重载operator!= 不等操作符重载operator<= 小于或等于操作符重载operator> 大于操作符重载operator>= 大于或等于操作符重载operator- 减法操作符重载(日期 - 日期)Print 打印函数 Date 类的测试测试操作符重载(+,+=,- ,-=,x++,++x,x--,--x)测试操作符重载(>,>=,==,<,<=,!=)测试操作符重载(日期 - 日期)结语

请添加图片描述

概要

本篇主要探究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";}

运行结果如下:
在这里插入图片描述
测试完毕,综合上述代码及运行结果,可得代码正确,可以正常模拟日期计算器。

结语

以上就是本期的全部内容了,喜欢就多多关注吧!!!


点击全文阅读


本文链接:http://zhangshiyu.com/post/72031.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1