当前位置: 首页 > news >正文

C++《日期》实现

C++《日期》实现

  • 头文件
    • 实现文件

头文件

在该文件中是为了声明函数和定义类成员

using namespace std;
class Date
{friend ostream& operator<<(ostream& out, const Date& d);//友元friend istream& operator>>(istream& cin, Date& d);//友元<这是为了把定义在类外面的函数,能够访问到类中的成员>
public:Date(int year = 1990, int month = 1, int days = 1);void print();int Getdaymonth(int year,int month)//日期获取{int getday[13] = { -1,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;}elsereturn getday[month];}bool cheakdate();//d1+=/+Date& operator+=(int days);Date operator+(int days);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;//d1-= 和 - Date& operator-=(int days);Date operator-(int days);//d++,++dDate& operator++();Date& operator++(int x);//d--和--dDate& operator--();Date operator--(int);//d1-d2(俩日期相减)int operator-(const Date& d)const;
private:int _year;int _month;int _days;
};ostream& operator<<(ostream& out,const Date& d);//流输出
istream& operator>>(istream& cin, Date& d);//流提取

实现文件

这里是对头文件外部函数中的成员函数的逐一实现:

#include"标头.h"
bool Date::cheakdate()
{if (_month < 0 || _month>12){return false;}else{return true;}
}Date::Date(int year,int month,int days)
{_year = year;_month = month;_days = days;if (!cheakdate()){cout << "非法日期" << endl;}
}
void Date::print()
{cout << _year << "-" << _month << "-" << _days << endl;
}
Date& Date::operator+=(int days)
{if (days < 0){return *this -= -days;}_days += days;while (_days > Getdaymonth(_year, _month)){_days -= Getdaymonth(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}
Date Date::operator+(int day) 
{Date tmp = *this;tmp += day;return tmp;
}bool Date:: operator<(const Date& d)const
{if (_year < d._year){return true;}else if (_year == d._year){if (_month < d._month){return true;}else if (_month == d._month){return _days < d._days;}}return false;
}
bool Date::operator>=(const Date& d)const
{return !(*this < d);
}
bool Date::operator==(const Date& d)const
{return _year == d._year && _month == d._month && _days == d._days;
}
bool Date:: operator<=(const Date& d)const
{return *this < d || *this == d;
}
bool Date::operator>(const Date& d)const
{return !(*this <= d);
}
bool Date:: operator!=(const Date& d)const
{return !(*this == d);
}
Date& Date::operator-=(int days)
{if (days < 0){return *this += -days;}_days -= days;while (_days<= 0){_month--;if (_month == 0){_year--;_month = 12;}_days += Getdaymonth(_year, _month);}return *this;
}
Date Date::operator-(int days)
{Date tmp = *this;tmp -= days;return tmp;
}
Date& Date:: operator++()
{return *this += 1;
}Date& Date:: operator++(int x)
{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;int count = 0;if (*this <d){max = d;min = *this;flag = -1;}while (min != max){count++;min++;}return count * flag;}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "/" << d._month << "/" << d._days << endl;return out;
}istream& operator>>(istream& cin, Date& d)
{while (1){cout << "请依次输入数据>:";cin >> d._year >> d._month >> d._days;if (!d.cheakdate()){cout << "输入日期非法:";d.print();cout << "请重新输入:";}else{break;}}return cin;
}``
http://www.lryc.cn/news/399083.html

相关文章:

  • 【面试题】MySQL(第三篇)
  • tensorflow之欠拟合与过拟合,正则化缓解
  • vue实现a-model弹窗拖拽移动
  • 速盾:如何加强网站的安全性
  • 【PyTorch单点知识】自动求导机制的原理与实践
  • 【Java】搜索引擎设计:信息搜索怎么避免大海捞针?
  • 【Python】ModuleNotFoundError: No module named ‘distutils.util‘ bug fix
  • 痉挛性斜颈对生活有哪些影响?
  • Javassist 修改 jar 包里的 class 文件
  • 交换机的二三层原理
  • HarmonyOS ArkUi 字符串<展开/收起>功能
  • Lianwei 安全周报|2024.07.09
  • 火遍全网的15个Python的实战项目,你该不会还不知道怎么用吧!
  • 快速使用BRTR公式出具的大模型Prompt提示语
  • Xilinx FPGA DDR4 接口的 PCB 准则
  • 神经网络 | Transformer 基本原理
  • 浅析 VO、DTO、DO、PO 的概念
  • 7.8 CompletableFuture
  • iPad锁屏密码忘记怎么办?有什么方法可以解锁?
  • 了解并缓解 IP 欺骗攻击
  • java LogUtil输出日志打日志的class文件内具体方法和行号
  • 02. Hibernate 初体验之持久化对象
  • MySQL超详细学习教程,2023年硬核学习路线
  • 初识SpringBoot
  • Qt之元对象系统
  • Provider(1)- 什么是AudioBufferProvider
  • 加密与安全_密钥体系的三个核心目标之完整性解决方案
  • 【C++】:继承[下篇](友元静态成员菱形继承菱形虚拟继承)
  • 昇思25天学习打卡营第13天|基于MindNLP+MusicGen生成自己的个性化音乐
  • nigix的下载使用