【C++】类与对象的项目实践 — 日期管理工具

2024-02-26 1157阅读

温馨提示:这篇文章已超过387天没有更新,请注意相关的内容是否还可用!

【C++】类与对象的项目实践 — 日期管理工具

类与对象的实践

  • 项目背景
  • 项目需求
  • 项目实现
    • 1 日期结构设计
    • 2 构造函数
      • 2.1 全缺省构造函数
      • 2.2 拷贝构造函数
      • 2.3 析构函数
      • 3 赋值运算符重载
        • 3.1 =重载
        • 3.2 += 重载
        • +重载
        • 前置++ 和 后置++
        • 4 关系操作符重载
        • 5 工具方法
          • 5.1 计算日期差
          • 5.2 日期转换为字符串
          • 5.3 通过字符串构建对象
          • 完整源代码
            • Date.h
            • Date.cpp
            • Thanks♪(・ω・)ノ谢谢阅读!!!
            • 下一篇文章见!!!

              根据我们对C++特性的学习,我们现在可以尝试下一些新玩法,来强化我们对C++的理解。

              项目背景

              在现代的软件开发中,日期作为一个常见的基础需求,广泛用于各类系统的日程管理,数据分析,交易记录等场景。但是C++库中的时间日期功能比较有限,无法满足复杂的开发需求。为此我们需要开发一款简单高效的“日期类”C++项目。

              【C++】类与对象的项目实践 — 日期管理工具

              项目需求

              1. 日期结构设计:我们需要实现一个名为“Date”的C++自定义类型,包含年(_year),月(_month),日(_day)。并相应提供构造函数,析构函数,拷贝复制函数等函数。
              2. 日期运算方法:实现日期的加减运算,支持用户通过增加或减少年、月、日来实现新的日期对象。同时,提供比较两个日期大小的方法,包括、 ==、 = 、!=等关系操作符的重载。
              3. 日期有效性检查:Date类需要实现对日期有效性的严格检查,确保月份正常,保证闰年的判断,符合各个月份的实际天数。
              4. 日期格式转换:提供将Date对象转换为“XXXX—YY—ZZ”的方法,同时也支持从标准“XXXX—YY—ZZ”字符串中解析创建Date对象。
              5. 实用工具方法:提供获取当前日期,判断是否为闰年,计算两个日期的天数差等功能。

              以上就是该项目的基本需求,请务必确保程序的健壮性与可维护性。

              项目实现

              1 日期结构设计

              该部分我们给出以下框架:

              class Date{
              public:
              	// 获取某年某月的天数
              	int GetMonthDay(int year, int month);
              	//展示日期
              	void show();
              	// 全缺省的构造函数
              	Date(int year = 1900, int month = 1, int day = 1);
              	// 拷贝构造函数
                  // d2(d1)
              	Date(const Date& d);
              	// 析构函数
              	~Date();
              	
              	//---------------------------------------------------------------
              	// 赋值运算符重载
                  // d2 = d3 -> d2.operator=(&d2, d3)
              	Date& operator=(const Date& d);
              	// 日期+=天数
              	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);
              	// 
              	_year = year;
              	_month = month;
              	_day = day;
              }
              
              	_year = d._year;
              	_month = d._month;
              	_day = d._day;
              }
              
              	_year = d._year;
              	_month = d._month;
              	_day = d._day;
                  //灵活使用this指针
              	return *this;
              }
              
              	int day[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 day[month];
              }
              
              	_day += day;
              	while (_day  GetMonthDay(_year, _month)) {
              		_day -= GetMonthDay(_year, _month);
              		_month++;
              		if (_month  12) {
              			_year++;
              			_month = 1;
              		}
              	}
              	return *this;
              }
              
              	Date temp(*this);
              	temp += day;
              	return temp;
              }
              
              //直接返回+=后答案即可
              	return *this += 1;
              }
              //后置++ 不能传引用
              Date Date::operator++(int) {
              //创建一个临时变量来储存 ++前的值
              	Date temp(*this);
              	*this += 1;
              	//返回++前的值
              	return temp;
              }
              
              h34 关系操作符重载/h3 p这里十分巧妙,我们只需要实现== > 即可通过巧妙使用完成全部操作符重载:
              bool Date::operator==(const Date& d) {
              	return d._year == _year && d._month == _month && d._day == _day;
              }
              bool Date::operator>(const Date& d) {
              	if (_year = (const Date& d) {
              	return *this > d || *this == d;
              }
              bool Date::operator = d);
              }
              bool Date::operator 
              	return !(*this  d);
              }
              bool Date::operator != (const Date& d) {
              	return !(*this == d);
              }
              

              5 工具方法

              5.1 计算日期差

              这里使用朴素算法,逐渐++到较大日期即可即可

              //注意正负
              int Date::operator-(const Date& d) {
              	//默认前值大
              	int flag = 1;
              	Date max = *this;
              	Date min = d;
              	if (max  
              

              5.2 日期转换为字符串

              使用库函数轻松实现:

              string Date::toString() const {
              	char buffer[11]; // 预留足够的空间存储 "YYYY-MM-DD"
              	snprintf(buffer, sizeof(buffer), "%04d-%02d-%02d", _year, _month, _day);
              	return string(buffer);
              }
              

              5.3 通过字符串构建对象

              这一步比较复杂:

              首先在声明中加入static 保证可以随时调用。

              然后需要保证日期的合法性

              最后返回类对象

              static Date fromString(const string& dateStr);
               Date Date::fromString(const std::string& dateStr) {
              	int year = 0, month = 0, day= 0;
              	sscanf(dateStr.c_str(), "%d-%d-%d", &year, &month, &day); // 使用sscanf解析字符串
              	Date d(year,month,day);
              	// 在这里应该添加必要的日期合法性检查
              	if (month  12) {
              		throw std::invalid_argument("月份错误\n");
              	}
              	int maxDays = d.GetMonthDay(year, month);
              	if (day  maxDays) {
              		throw std::invalid_argument("给定月份和年份的日期值无效\n");
              	}
              	return Date(year, month, day);
              }
              

              完整源代码

              Date.h

              #pragma once
              #include
              using namespace std;
              class Date{
              public:
              	// 获取某年某月的天数
              	int GetMonthDay(int year, int month);
              	//展示日期
              	void show();
              	// 全缺省的构造函数
              	Date(int year = 1900, int month = 1, int day = 1);
              	// 拷贝构造函数
                  // d2(d1)
              	Date(const Date& d);
              	// 析构函数
              	~Date();
              	// 赋值运算符重载
                  // d2 = d3 -> d2.operator=(&d2, d3)
              	Date& operator=(const Date& d);
              	// 日期+=天数
              	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);
              	// 
              	
              	printf("%4d 年 %2d 月 %2d 日\n",_year,_month,_day);
              }
              int Date::GetMonthDay(int year, int month) {
              	int day[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 day[month];
              }
              Date::Date(int year , int month , int day ) {
              	_year = year;
              	_month = month;
              	_day = day;
              }
              Date::Date(const Date& d) {
              	_year = d._year;
              	_month = d._month;
              	_day = d._day;
              }
              Date::~Date() {
              	_year = 0;
              	_month = 0;
              	_day = 0;
              }
              Date& Date::operator+=(int day) {
              	_day += day;
              	while (_day  GetMonthDay(_year, _month)) {
              		_day -= GetMonthDay(_year, _month);
              		_month++;
              		if (_month  12) {
              			_year++;
              			_month = 1;
              		}
              	}
              	return *this;
              }
              Date& Date::operator=(const Date& d) {
              	_year = d._year;
              	_month = d._month;
              	_day = d._day;
              	return *this;
              }
              Date Date::operator+(int day) {
              	Date temp(*this);
              	temp += day;
              	return temp;
              }
              Date& Date::operator-=(int day) {
              	_day -= day;
              	while (_day = (const Date& d) {
              	return *this > d || *this == d;
              }
              bool Date::operator = d);
              }
              bool Date::operator 
              	return !(*this  d);
              }
              bool Date::operator != (const Date& d) {
              	return !(*this == d);
              }
              int Date::operator-(const Date& d) {
              	Date max = *this;
              	Date min = d;
              	int flag = 1;
              	if (max  12) {
              		throw std::invalid_argument("月份错误\n");
              	}
              	int maxDays = d.GetMonthDay(year, month);
              	if (day  maxDays) {
              		throw std::invalid_argument("给定月份和年份的日期值无效\n");
              	}
              	return Date(year, month, day);
              }
              

              Thanks♪(・ω・)ノ谢谢阅读!!!

              下一篇文章见!!!

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]