C++日期计算(某日期加减天数、两日期相差的天数)

实验要求

二、实验内容
设计一个日期类Date,包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。
三、实验要求
在Date类中设计如下重载运算符函数:
Date operator+(int days):返回某日期加上天数得到的日期
Date operator-(int days):返回某日期减去天数得到的日期
int operator-(Date&b):返回两日期相差的天数
在实现这些重载运算符函数时调用以下私有成员函数:
leap(int):判断指定的年份是否为闰年

源代码

//Date.h
#pragma once
#include <iostream>
using namespace std;

class Date
{
	int year;
	int month;
	int day;
	int leap(int);
	int md2d(int leap, int month, int day);
public:
	Date(int y, int m, int d);
	Date();
	friend ostream& operator<<(ostream&, Date&);
	//某日期加上天数
	Date operator+(int days);
	//某日期减去天数
	Date operator-(int days);
	//两日期相差的天数
	int operator-(Date& b);
private:
	int y2d(int year)
	{
		return 365 + leap(year);
	}
};
//Date.cpp
#include "Date.h"
#include <iostream>
using namespace std;
const int day_tab[2][12] = { {31,28,31,30,31,30,31,31,30,31,30,31},
	{31,29,31,30,31,30,31,31,30,31,30,31} };

int Date::leap(int y)
{
	return (y % 4 == 0 && y % 100 != 0 || y % 400 == 0);
}

int Date::md2d(int leap, int month, int day)
{
	for (int i = month-2; i >= 0; i--)
	{
		day += day_tab[leap][i];
	}
	return day;
}

Date::Date(int y, int m, int d)
{
	year = y; month = m; day = d;
}

Date::Date()
{
	cout << "年:";
	cin >> year;
	cout << "月:";
	cin >> month;
	cout << "日:";
	cin >> day;
}

Date Date::operator+(int days)
{
	days += md2d(leap(year), month, day);
	//0<days<=365+leap
	int y = year;
	while (days > y2d(y))
	{
		days -= y2d(y);
		y++;
	}
	while (days <= 0)
	{
		y--;
		days += y2d(y);
	}
	//d2md
	int m = 1;
	int d1;
	while ((d1 = days - day_tab[leap(y)][m - 1]) > 0)
	{
		days = d1;
		m++;
	}
	Date dt(y, m, days);
	return dt;
}

Date Date::operator-(int days)
{
	return *this + (-days);
}

int Date::operator-(Date& b)
{
	int d1 = md2d(leap(year), month, day);
	int d2 = md2d(leap(b.year), b.month, b.day);
	int ds = d1 - d2;
	int y1 = year;
	int y2 = b.year;
	while (y1 > y2)
	{
		y1--;
		ds += y2d(y1);
	}
	while (y1 < y2)
	{
		ds -= y2d(y1);
		y1++;
	}
	return ds;
}

ostream& operator<<(ostream& os, Date& d)
{
	os << d.year << "年" << d.month << "月" << d.day << "日";
	return os;
}
//main.cpp
#include <iostream>
#include "Date.h"
using namespace std;
void add()
{
    Date d1;
    int day;
    cout << "加上天数";
    cin >> day;
    Date d2 = d1 + day;
    cout << d2 << endl;
}
void jian()
{
    Date d1;
    int day;
    cout << "减去天数";
    cin >> day;
    Date d2 = d1 - day;
    cout << d2 << endl;
}
void cha()
{
    Date d1;
    Date d2;
    cout << "相差的天数" << d1 - d2 << endl;
}
int main()
{
    cout << "C++日期计算(某日期加/减去天数、两日期相差的天数)";
    //某日期加上天数、某日期减去天数、两日期相差的天数
    add();
    jian();
    cha();
}

输出结果:

年:2000
月:1
日:1
加上天数730
2001年12月31日
年:2020
月:4
日:13
减去天数25762
1949年10月1日
年:1949
月:10
日:1
年:2020
月:4
日:13
相差的天数-25762

集成开发环境:Visual Studio 2019

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦的乌班图

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值