C++日期类计算器的模拟实现举例详解
作者:EnticE152 发布时间:2023-05-22 08:27:16
标签:C++,日期类,计算器
日期类计算器的模拟实现::
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


猜你喜欢
- 网上杂七杂八的说法不一,大多数都是抄来抄去,没有实践,近期在项目频繁遇到boot+jackson处理日期的问题,故开此贴。首先是POM<
- 之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 Web
- 在日常系统开发中,经常需要在adroid的framework修改或添加自己的配置。例如在config.xml 添加一个新的变量。我这边测试发
- 1、产生原因其实显示黑屏或者白屏实属正常,这是因为还没加载到布局文件,就已经显示了window窗口背景,黑屏白屏就是window窗口背景。示
- 在为什么阿里巴巴不建议在for循环中使用”+”进行字符串拼接一文中,我们介绍了几种Java中字符串拼接的方式,以及优缺点。其中还有一个重要的
- 前言作为一个写java的使用最多的轻量级框架莫过于spring,不管是老项目用到的springmvc,还是现在流行的springboot,都
- 1.项目介绍本项目旨在打造一个基于RBAC架构模式的通用的、并不复杂但易用的权限管理系统。通过本项目可以较好的理解权限系统的常见业务同时学习
- 目录一、值类型和引用类型的区别1、赋值时的区别2、内存分配的区别3、来自继承结构的区别二、总结一、值类型和引用类型的区别.NET的类型可以分
- 我这一次讲使用scroll实现弹性滑动,我不会只有一个例子就说完,因为写文章的时候我也在学习,我分几次讲完吧。首先上一段代码,private
- 默认情况下,插件 spring-boot-maven-plugin 会把整个项目打包成一个可运行的Jar包(即所谓的Flat Jar),导致
- log4j的rootLogger及其他坑爹地方这里用的是org.apache.log4j.Loggerlog4j.rootLogger=er
- 假定存在这样一种情况多个用户对数据库进行写,我们的业务逻辑规定,每个用户只能写一次,大部分用户也只发一次请求。public void wri
- 一、Json简介Json(JavaScript Object Notation) 是一种轻量级的数据交换格式。它基于JS的一个子集。 Jso
- CSRF介绍CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click atta
- 简单的理解,MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件。mybatis需要编写sql语句,myba
- Spring Boot如何实现分布式锁的自动释放在分布式系统中,为了保证数据的一致性和可靠性,常常需要使用分布式锁。在实际开发中,我们可以使
- 简介说明本文用实例来介绍@Autowired解决循环依赖的原理。@Autowired是通过 * 缓存来解决循环依赖的。 除了@Aut
- 😜shape属性详解<?xml version="1.0" encoding="utf-8"?
- 目录前言代码一:代码二:方式一:方式二:方式三:总结前言之前写过多线程累加计数,原理跟本篇类似,传送门累加计数比计算数组之和逻辑稍微简单一点
- 对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入,最