C++日期类计算器的模拟实现举例详解

作者:EnticE152 时间:2023-05-22 08:27:16 

日期类计算器的模拟实现::

1.获取某年某月的天数

int GetMonthDay(int year, int month)
{
static int monthDayArray[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;
}
else
{
return monthDayArray[month];
}
}

2.构造函数

Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
//检查日期是否合法
if (!((year >= 1)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= GetMonthDay(year, month))))
{
cout << "非法日期" << endl;
}
}

3.拷贝构造函数

// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}

4.赋值运算符重载

//d1 = d2
//注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
Date& operator=(const Date& d)
{
//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}

5.析构函数

~Date()//可不写
{
   ;
}

日期类因为没有申请资源,所以无需写析构函数,编译器默认生成的析构函数就可以。

6.日期+=天数

//d1 += 100
//天满了进月 月满了进年
Date& operator+=(int day)
{
//避免 d1 += -1000的情形
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}

7.日期+天数

//d1 + 100
Date operator+(int day) const
{
Date ret(*this);
ret += day;//ret.operator+=(day)
return ret;
}

8.日期-天数

//d1 - 100
Date operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}

9.日期-=天数

//d1 -= 100
Date& operator-=(int day)
{
//避免 d1 -= -1000
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}

10.前置++的运算符重载

//前置++
Date& operator++()
{
//会调用 operator+=(int day)
*this += 1;
return *this;
}

11.后置++的运算符重载

//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}

12.前置--的运算符重载

//前置--
Date& operator--()
{
//复用运算符重载-=
*this -= 1;
return *this;
}

13.后置--的运算符重载

//后置--
Date operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}

14.>的运算符重载

//d1 > d2
bool operator>(const Date& d) const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}

15.<的运算符重载

//d1 < d2
bool operator<(const Date& d) const
{
return !(*this >= d);
}

16.==的运算符重载

//d1 == d2
bool operator==(const Date& d) const
{ return _year == d._year
&& _month == d._month
&& _day == d._day;
}

17.>=的运算符重载

//d1 >= d2
bool operator>=(const Date& d) const
{
return *this > d || *this == d;
}

18.<=的运算符重载

//d1 <= d2
bool operator<=(const Date& d) const
{
return !(*this > d);
}

19.!=的运算符重载

//d1 != d2
bool operator!=(const Date& d) const
{
return !(*this == d);
}

20.<<的运算符重载

//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}

21.>>的运算符重载

//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}

22.日期-日期

//日期-日期
int operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
 //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
 //if (d > *this)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
//复用++ ++到和d1日期相等 就是相差多少天
++min;
}
return n * flag;
}

Date.h

#pragma once
#include <iostream>
using namespace std;
class Date
{
//友元声明(类的任意位置)声明友元时可以不用加inline
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
int GetMonthDay(int year, int month)
{
static int monthDayArray[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;
}
else
{
return monthDayArray[month];
}
}
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
//检查日期是否合法
if (!((year >= 1)
&& (month >= 1 && month <= 12)
&& (day >= 1 && day <= GetMonthDay(year, month))))
{
cout << "非法日期" << endl;
}
}
// 拷贝构造函数 形参加const 防止写反了 问题就可以检查出来了
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//d1 == d2
bool operator==(const Date& d) const;
//d1 > d2
bool operator>(const Date& d) const;
//d1 >= d2
bool operator>=(const Date& d) const;
//d1 <= d2
bool operator<=(const Date& d) const;
//d1 < d2
bool operator<(const Date& d) const;
//d1 != d2
bool operator!=(const Date& d) const;
//d1 += 100
Date& operator+=(int day);
//d1 + 100
Date operator+(int day) const;
//d1 = d2 注:1.要注意两个参数的顺序 2.这里面参数不加引用不会导致无穷递归 但为了避免拷贝构造最好加引用
Date& operator=(const Date& d)
{
//为了支持链式赋值 if是为了避免自己给自己赋值 d1 = d1
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
//d1 -= 100
Date& operator-=(int day);
//d1 - 100
Date operator-(int day) const;
//++的操作数只有一个 不传参
//前置++
Date& operator++();
//编译器为了区分前置++和后置++ 规定在后置的函数上加了一个参数
//后置++
Date operator++(int);
//允许成员函数加const 此时this指针的类型为:const Date* const this
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//前置--
Date& operator--();
//后置--
Date operator--(int);
//日期-日期
int operator-(const Date& d) const;
//流插入
//d1 << cout编译器会转化成d1.operator<<(cout) this指针抢了左操作数d1的位置
//<<和>>的重载一般不写成成员函数 因为this默认抢了第一个参数的位置 Date类对象就是左操作数 不符合使用习惯和可读性
/*void operator<<(ostream& out)
{
out << _year << "年" << _month << "月" << _day << "日" << endl;
}*/
//取地址重载
Date* operator&()
{
return this;
}
//const成员取地址重载
const Date* operator&() const
{
return this;
}
//取地址重载和const成员取地址重载不实现 编译器会默认生成
private:
int _year;
int _month;
int _day;
};
//结论:对于自定义类型,尽量用前置,减少拷贝,提高效率
//全局函数调用:cout << d1转化成operator<<(cout,d1)
//全局函数的定义和全局变量不能放在.h文件中 因为函数的定义在Date.cpp和test.cpp都会展开 函数地址进入符号表 链接器链接两个.cpp文件时相同的函数地址会报错
//解决方法:1.改成静态 2.声明和定义分离
//static修饰函数只在当前文件可见 不会进入符号表
//static void operator<<(ostream& out,const Date& d)
//{
//out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//}
//ostream& operator<<(ostream& out, const Date& d);
//内联函数和静态成员一样 调用处展开 不进符号表
inline ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
//cin >> d1 编译器转化成operator(cin,d1) 形参中相比<< 去掉了const
inline istream& operator>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}

Date.cpp

#include"Date.h"
//d1 == d2
bool Date::operator==(const Date& d) const
{ return _year == d._year
&& _month == d._month
&& _day == d._day;
}
//d1 > d2
bool Date::operator>(const Date& d) const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
//d1 >= d2
bool Date::operator>=(const Date& d) const
{
return *this > d || *this == d;
}
//d1 <= d2
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
//d1 < d2
bool Date::operator<(const Date& d) const
{
return !(*this >= d);
}
//d1 != d2
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
//d1 += 100
//天满了进月 月满了进年
Date& Date::operator+=(int day)
{
//避免 d1 += -1000的情形
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
//d1 + 100
Date Date::operator+(int day) const
{
Date ret(*this);
ret += day;//ret.operator+=(day)
return ret;
}
//d1 -= 100
Date& Date::operator-=(int day)
{
//避免 d1 -= -1000
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//d1 - 100
Date Date::operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}
//前置++
Date& Date::operator++()
{
//会调用 operator+=(int day)
*this += 1;
return *this;
}
//后置++ —多一个int参数主要是为了和前置++进行区分 构成函数重载
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//前置--
Date& Date::operator--()
{
//复用运算符重载-=
*this -= 1;
return *this;
}
//后置--
Date Date::operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
//日期-日期
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
 //总结:凡是内部不改变成员变量 也就是不改变*this数据的 这些成员函数都应该加const
 //if (d > *this)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
//复用++ ++到和d1日期相等 就是相差多少天
++min;
}
return n * flag;
}
//为了支持链式流插入 cout<< d1 <<d2 返回cout类对象
//ostream& operator<<(ostream& out,const Date& d)
//{
//out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
//return out;
//}

来源:https://blog.csdn.net/qq_66767938/article/details/130045238

标签:C++,日期类,计算器
0
投稿

猜你喜欢

  • springMvc注解之@ResponseBody和@RequestBody详解

    2022-10-09 17:57:19
  • Android深入探究自定义View之嵌套滑动的实现

    2022-02-21 16:21:42
  • Android实现九宫格横向左右滑动

    2022-03-06 13:09:47
  • Android Rreact Native 常见错误总结

    2021-07-11 16:39:59
  • C++如何调用已经写好的C接口

    2021-10-16 10:16:14
  • JavaMail入门教程之解析邮件(5)

    2023-11-29 08:25:13
  • 详解Maven私 服Nexus的安装与使用

    2023-11-24 12:34:02
  • C++实现连连看游戏

    2023-12-06 02:18:09
  • Android使用Retrofit上传文件功能

    2022-08-28 08:42:38
  • SpringCloud 微服务最佳开发实践

    2022-02-06 10:07:45
  • Java编程倒计时实现方法示例

    2021-05-31 06:30:11
  • springboot配置文件中属性变量引用方式@@解读

    2023-11-24 20:39:18
  • 详解idea从git上拉取maven项目详细步骤

    2023-04-23 08:51:56
  • java面试题之数组中的逆序对

    2021-08-05 10:51:02
  • DevExpress实现禁用TreeListNode CheckBox的方法

    2022-07-20 22:17:43
  • MyBatis超详细讲解如何实现分页功能

    2023-08-22 23:06:51
  • Android7.0中关于ContentProvider组件详解

    2023-10-30 19:48:29
  • C# SaveFileDialog与OpenFileDialog用法案例详解

    2023-01-28 05:44:42
  • Java十分钟精通类 封装 继承

    2023-11-25 10:55:58
  • Spring与Shiro整合及加载权限表达式问题

    2023-10-01 09:29:50
  • asp之家 软件编程 m.aspxhome.com