类模块的练习--实现日期类

简介: 类模块的练习--实现日期类

🔎在前面对类的学习中我们已经初步的了解和使用类,现在我们可以尝试独立的实现一个日期类,包括日期类的运算符号重载。

Date.h文件📝

声明头文件,Date类以及类中的各种成员方法✏️

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
//#include<algorithm>
#include<string>
using namespace std;
class Date
{
public:
  // 获取某年某月的天数
  int GetMonthDay(int year, int month)
  {
    static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
     31 };
    int day = days[month];
    if (month == 2
      && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
      day += 1;
    }
    return day;
  }
  //判断是否是闰年
  bool IsLeap(int year);
  //打印
  void Print() {
    cout << _year << "-" << _month << "-" << _day<< endl;
  }
  //交换两个日期
  void Swap(Date& d1,Date& d2);
  // 全缺省的构造函数
  Date(int year=1900,int month=1,int day =1);
  // 拷贝构造函数
 // d2(d1)
  Date(const Date& d);
 
  // 赋值运算符重载
 // d2 = d3 -> d2.operator=(&d2, d3)
  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);
  // ==运算符重载
  bool operator==(const Date& d);
  // >=运算符重载
  bool operator >= (const Date& d);
 
  // <运算符重载
  bool operator < (const Date& d);
  // <=运算符重载
  bool operator <= (const Date& d);
  // !=运算符重载
  bool operator != (const Date& d);
  // 日期-日期 返回天数
  int operator-(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};

Date.cpp文件📝

定义各种成员方法✏️

#include"Date.h"
 
//判断是否是闰年
bool Date::IsLeap(int year) {
  if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){
    return true;
  }
  return false;
}
//交换两个日期
void Date::Swap(Date& d1,Date& d2) {
  swap(d1._year, d2._year);
  swap(d1._month, d2._month);
  swap(d1._day, d2._day);
}
// 全缺省的构造函数
Date::Date(int year ,int month , int day ) {
  _year = year;
  _month = month;
  _day = day;
}
 
// 拷贝构造函数
// d2(d1)
Date::Date(const Date& d) {
  _year = d._year;
  _month = d._month;
  _day = d._day;
}
 
// 赋值运算符重载
 // d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d){
  _year = d._year;
  _month = d._month;
  _day = d._day;
  return *this;
}
// 析构函数
Date::~Date() {
  _year = 0;
  _month = 0;
  _day = 0;
}
// 日期+=天数
Date& Date::operator+=(int day) {
  while (day) {
    if (_day + day <= GetMonthDay(_year, _month)) {
      _day += day;
      day -= day;
    }
    else {
      int d = GetMonthDay(_year, _month) - _day+1;
      day -= d;
      if (_month == 12) {
        _year++;
      }
      _month = (_month) % 12 + 1;
      _day = 1;
    }
  }
  return *this;
}
// 日期+天数
Date Date::operator+(int day) {
  Date temp(*this);
  while (day) {
    if (temp._day + day <= GetMonthDay(temp._year, temp._month)) {
      temp._day += day;
      day -= day;
    }
    else {
      int d = GetMonthDay(temp._year, temp._month) - temp._day + 1;
      day -= d;
      if (temp._month == 12) {
        temp._year++;
      }
      temp._month = (temp._month) % 12 + 1;
      temp._day = 1;
    }
  }
  return temp;
}
// 日期-天数
Date  Date::operator-(int day) {
  Date temp(*this);
  while (day) {
    if (temp._day - day >=1) {
      temp._day += day;
      day -= day;
    }
    else {
      if (temp._month == 1) {
        temp._year--;
      }
      day -= temp._day;
      temp._month = temp._month == 1 ? 12 : temp._month - 1;
      temp._day = GetMonthDay(temp._day, temp._month);
    }
  }
  return temp;
}
// 日期-=天数
Date& Date::operator-=(int day) {
  
  while (day) {
    if (_day - day >= 1) {
      _day += day;
      day -= day;
    }
    else {
      if (_month == 1) {
        _year--;
      }
      day -= _day;
      _month = _month == 1 ? 12 : _month - 1;
      _day = GetMonthDay(_day, _month);
    }
  }
  return *this;
}
// 前置++
Date& Date::operator++() {
  (*this) += 1;
  return *this;
}
// 后置++
Date  Date::operator++(int) {
  Date temp(*this);
  (*this)++;
  return temp;
}
// 后置--
Date  Date::operator--(int) {
  Date temp(*this);
  (*this)--;
  return temp;
}
// 前置--
Date& Date::operator--() {
  (*this) -= 1;
  return *this;
}
 
// >运算符重载
bool  Date::operator>(const Date& d) {
  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;
}
// ==运算符重载
bool  Date::operator==(const Date& d) {
  if (_year == d._year && _month == d._month && _day == d._day)return true;
  return false;
}
// >=运算符重载
bool  Date::operator >= (const Date& d) {
  if ((*this) > d || (*this) == d)return true;
  return false;
}
 
// <运算符重载
bool  Date::operator < (const Date& d) {
  if (!(*this >= d))return true;
  return false;
}
// <=运算符重载
bool  Date::operator <= (const Date& d) {
  if (!(*this > d))return true;
  return false;
}
// !=运算符重载
bool  Date::operator != (const Date& d) {
  if (!(*this == d))return true;
  return false;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d) {
  Date d1 = (*this);
  Date d2 = d;
  if (d1 < d2) {
    Swap(d1, d2);
  }
  int res = 0;
  while (d2 < d1) {
    int t = GetMonthDay(d2._year, d2._month)+1-d2._day;
    if (d2 + t <= d1) {
      d2 += t;
      res += t;
    }
    else {
      int k = d1._day - d2._day;
      d2 += k;
      res += k;
    }
  }
  return res;
}


相关文章
|
9天前
|
数据采集 人工智能 安全
|
4天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:七十、小树成林,聚沙成塔:随机森林与大模型的协同进化
随机森林是一种基于决策树的集成学习算法,通过构建多棵决策树并结合它们的预测结果来提高准确性和稳定性。其核心思想包括两个随机性:Bootstrap采样(每棵树使用不同的训练子集)和特征随机选择(每棵树分裂时只考虑部分特征)。这种方法能有效处理大规模高维数据,避免过拟合,并评估特征重要性。随机森林的超参数如树的数量、最大深度等可通过网格搜索优化。该算法兼具强大预测能力和工程化优势,是机器学习中的常用基础模型。
301 164
|
3天前
|
机器学习/深度学习 自然语言处理 机器人
阿里云百炼大模型赋能|打造企业级电话智能体与智能呼叫中心完整方案
畅信达基于阿里云百炼大模型推出MVB2000V5智能呼叫中心方案,融合LLM与MRCP+WebSocket技术,实现语音识别率超95%、低延迟交互。通过电话智能体与座席助手协同,自动化处理80%咨询,降本增效显著,适配金融、电商、医疗等多行业场景。
315 155
|
12天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
870 6
|
5天前
|
机器学习/深度学习 人工智能 前端开发
构建AI智能体:六十九、Bootstrap采样在大模型评估中的应用:从置信区间到模型稳定性
Bootstrap采样是一种通过有放回重抽样来评估模型性能的统计方法。它通过从原始数据集中随机抽取样本形成多个Bootstrap数据集,计算统计量(如均值、标准差)的分布,适用于小样本和非参数场景。该方法能估计标准误、构建置信区间,并量化模型不确定性,但对计算资源要求较高。Bootstrap特别适合评估大模型的泛化能力和稳定性,在集成学习、假设检验等领域也有广泛应用。与传统方法相比,Bootstrap不依赖分布假设,在非正态数据中表现更稳健。
253 113