实验要求
二、实验内容
设计一个日期类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