为了提高的安全性,我只将要实现对象功能的方法放在了date的public区域中 而操作符重载函数还有一些其他不会被直接调用的函数我放到了private区域。
为了锻炼自己的代码能力 我也将一些常用的date类操作符重载如'<' '>' '<=' ' >=' '='等一些操作符重载函数写了出来
int const Year = 2022, Month = 4, Day = 9;
class Date{
public:
//设置默认构造函数
Date(int year = Year, int month = Month, int day = Day)
:_year(Year)//初始化
, _month(Month)
, _day(Day)//构造函数函数体内放的是赋值
{
if (IsLegal(year, month, day))//检验输入的日期是否合法
{
_year = year;
_month = month;
_day = day;
}
else
cout << "输入格式错误!" << endl<<"请重新输入该日期"<> datescale;
year = datescale / 10000;
month = (datescale % 10000) / 100;
day = datescale % 100;
if (IsLegal(year, month, day)){
flag = 0;
}
else
cout << "输入格式错误!" << endl << "请重新输入该日期" << endl;
}
Date date1(year, month, day);
flag = 1;
while (flag){
cout << "请再输入第二个日期" << endl;
cin >> datescale;
year = datescale / 10000;
month = (datescale % 10000) / 100;
day = datescale % 100;
if (IsLegal(year, month, day)){
flag = 0;
}
else
cout << "输入格式错误!" << endl << "请重新输入该日期" << endl;
}
Date date2(year, month, day);
int datedv = date1 - date2;
if (datedv < 0)
datedv = 0 - datedv;
cout << "两个日期相差"<> datescale;
year = datescale / 10000;
month = (datescale % 10000) / 100;
day = datescale % 100;
if (IsLegal(year, month, day)){
flag = 0;
}
else
cout << "输入格式错误!" << endl << "请重新输入该日期" << endl;
}
Date n(year, month, day);
cout << "请输入要加的天数" << endl;
int days;
cin >> days;
n += days;
LetLegal(n);
n.Print();
}
void GetSubDate(){//计算减少X天后的日期
int datescale, year, month, day, flag = 1;
while (flag){
cout << "请输入一个日期" << endl;
cin >> datescale;
year = datescale / 10000;
month = (datescale % 10000) / 100;
day = datescale % 100;
if (IsLegal(year, month, day)){
flag = 0;
}
else
cout << "输入格式错误!" << endl << "请重新输入该日期" << endl;
}
Date n(year, month, day);
cout << "请输入要减去的天数" << endl;
int days;
cin >> days;
n -= days;
LetLegal(n);
n.Print();
}
private:
int _year;
int _month;
int _day;
bool IsLegal(int year, int month, int day){
int arrm[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
arrm[2] += 1;
if (year >= 0 && 1 <= month&&month <= 12&& 1 <= day &&day<= arrm[month])
return true;
else
return false;
}
Date& operator=(const Date &n)//
{
_year = n._year;
_month = n._month;
_day = n._day;
return *this;
}
Date operator++()
{
_day++;
return *this;
}
Date operator++(int)
{
Date temp(*this);
_day++;
return temp;
}
//a+=b,即a=a+b;
Date& operator+=(int days)
{
_day += days;
return *this;
}
Date& operator-=(int days)
{
_day -= days;
return *this;
}
Date operator-(int days)
{
Date temp(*this);
temp._day--;
return temp;
}
int operator-(const Date &n)//两个日期相减还剩多少天(具有正负)
{
int arrm[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int ydv, dv, day1 = 0, day2 = 0;
if ((_year / 4 == 0 && _year / 100 != 0) || _year / 400 == 0)
arrm[2]++;
for (int i = 1; i < _mouth; i++)
{
day1 += arrm[i];
}
day1 += _day;
int arrm2[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((n._year / 4 == 0 && n._year / 100 != 0) || n._year / 400 == 0)
arrm2[2]++;
for (int i = 1; i 0)
{
int count = 0;
for (int year = _year - 1; year > n._year; year--){
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
count++;
}
dv = ydv * 365 + count + (day1 - day2);
return dv;
}
else{
int count = 0;
for (int year = n._year - 1; year > _year; year--){
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
count++;
}
dv = -ydv * 365 + count + (day2 - day1);
return 0 - dv;
}
}
Date operator+(int days)
{
Date temp(*this);
temp._day++;
return temp;
}
bool operator==(const Date &n)
{
if (_year == n._year&&_month == n._month&&_day == n._day)
return true;
else
return false;
}
bool operator<(const Date &n)
{
if (*this - n)
return false;
return true;
}
bool operator<=(const Date &n)
{
if (*this - n || *this - n == 0)
return false;
return true;
}
bool operator>(const Date &n)
{
if (*this - n)
return true;
return false;
}
bool operator>=(const Date &n)
{
if (*this - n || *this - n == 0)
return true;
return false;
}
void Print()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
void LetLegal(Date &n)
{
//如果日期格式错误,那么错误的地方一定在_day上
int arrm[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (n._day>0)//_day>0
{
while (n._day > arrm[n._month]){
if ((n._year % 4 == 0 && n._year / 100 != 0) || n._year % 400 == 0)
arrm[2] ++;
n._day -= arrm[n._month];
if (n._month == 12){
n._month = 1;
n._year++;
if(arrm[2]==29)arrm[2]--;
}
n._month++;
}
return;
}
while (n._day < 0){
if ((n._year / 4 == 0 && n._year / 100 != 0) || n._year / 400 == 0)
arrm[2] ++;
n._day += arrm[n._month - 1];
if (n._month == 1){
n._month = 12;
n._year--;
if (arrm[2] == 29)
arrm[2]--;
}
n._month--;
}
return;
}
};
int main()
{
Date d1;
d1.menu();
int key;
cin >> key;
while (key){
switch (key){
case 1:
d1.DateDV();
break;
case 2:
d1.GetAddDate();
break;
case 3:
d1.GetSubDate();
break;
case 0:
break;
default:
cout << "error!" << endl;
break;
}
if (key != 0){
cout << "请输入命令序号" << endl;
}
cin >> key;
if (key != 0){
system("cls");
d1.menu();
}
}
system("pause");
return 0;
}
作者目前C++仍处于学习阶段,代码中可能会有一些地方的语法复杂累赘,欢迎大家在评论区给予指正!