年份的加减运算符重载C++Code

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

class CDate
{
 friend ostream &operator<<(ostream &os, const CDate &other);
public:
 CDate(int y = 0, int m = 0, int d = 0)
 {
  m_year = y;
  m_month = m;
  m_day = d;
 }
 bool IsLeapYear(int year); //判断是否是闰年
 int DateToOn(const CDate &other); //日期转换为从0年0月0日起的天数
 int MonthDay(int year, int month); //当前月份有几天
 CDate operator +(const CDate &other); //两个日期相加
 int operator -(const CDate &other); //两个日期相减的天数
 CDate operator +(int nday);  //当前日期加上nday天
 CDate operator -(int nday);  //当前日期减去nday天

private:
 int m_year;
 int m_month;
 int m_day;
};

bool CDate::IsLeapYear(int year)
{
 return ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0));
}

int CDate::DateToOn(const CDate &other)
{
 int sumday = 0;

 sumday = other.m_year * 365;

 for(int i = 1; i < other.m_year; ++i)
 {
  if(IsLeapYear(i))
  {
   sumday++;
  }
 }
 int sum_day;
 switch(other.m_month)  //计算本月一起共有多少天
 {
 case 1:
  sum_day = 0;
  break;
 case 2:
  sum_day = 31;
  break;
 case 3:
  sum_day = 59;
  break;
 case 4:
  sum_day = 90;
 case 5:
  sum

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相的菜单选项中增日期天数,结果为新日期;日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增日期减天数,结果为新日期;日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增显示星期及英文形式的月份的功能。 (5) 增输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值