- 学习完C++入门以及类和对象,我们已经迫不及待的想写一个自己的小项目了,下面这个计算日期的小项目就是运用类和对象写出来的。
Date.h
#pragma once
#include <iostream>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
class Date
{
public:
int GetMonthDay(int year, int month);
Date(int year = 1900, int month = 1, int day = 1);
Date(const Date& d);
Date& operator= (const Date& d);
~Date();
bool operator>(const Date& d);
bool operator==(const Date& d);
bool operator>=(const Date& d);
bool operator<(const Date& d);
bool operator<=(const Date& d);
bool operator!=(const Date& d);
Date& operator += (int day);
Date operator+(int day);
Date operator-(int day);
Date& operator-=(int day);
Date& operator++();
Date operator++(int);
Date operator--(int);
Date& operator--();
int operator-(const Date& d);
void PrintDate();
private:
int _year;
int _month;
int _day;
};
Date.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
int Date::GetMonthDay(int year, int month)
{
static int arr[13] = { 0,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;
}
else
return arr[month];
}
Date::Date(int year, int month, int day)
{
this->_year = year;
this->_month = month;
_day = day;
}
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date:: ~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
return true;
if (_year == d._year && _month > d._month)
return true;
if (_year == d._year && _month == d._month && _day > d._day)
return true;
return false;
}
bool Date::operator==(const Date& d)
{
if (_year == d._year && _month == d._month && _day == d._day)
return true;
return false;
}
bool Date::operator>=(const Date& d)
{
if (*this > d || *this == d)
return true;
return false;
}
bool Date::operator<(const Date& d)
{
if (*this > d || *this == d)
return false;
return true;
}
bool Date::operator<=(const Date& d)
{
if (*this < d || *this == d)
return true;
return false;
}
bool Date::operator!=(const Date& d)
{
if (*this == d)
return false;
return true;
}
Date& Date::operator+=(int day)
{
this->_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date temp(*this);
temp += day;
return temp;
}
Date Date::operator-(int day)
{
Date temp(*this);
while (temp._day<day)
{
temp._month--;
if (temp._month == 0)
{
temp._year--;
temp._month = 12;
}
temp._day += GetMonthDay(_year, _month);
}
temp._day -= day;
return temp;
}
Date& Date::operator-=(int day)
{
*this = *this - day;
return *this;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date temp(*this);
*this += 1;
return temp;
}
Date Date::operator--(int)
{
Date temp(*this);
*this -= 1;
return temp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
int Date::operator-(const Date& d)
{
int year_day = 0;
int month_day = 0;
int day_day = 0;
if (_year != d._year)
{
int a = abs(_year - d._year);
while (a--)
{
if (_year > d._year)
{
if (GetMonthDay(d._year + a, 2) == 29)
year_day += 366;
else
year_day += 365;
}
else
{
if (GetMonthDay(_year + a, 2) == 29)
year_day += 366;
else
year_day += 365;
}
}
}
if (_year < d._year)
year_day = -year_day;
if (1)
{
int a = _month - 1;
int _month_day = 0;
int d_month_day = 0;
while (a)
{
a--;
_month_day += GetMonthDay(_year, a);
}
a = d._month - 1;
while (a)
{
a--;
d_month_day += GetMonthDay(d._year, a);
}
month_day = _month_day - d_month_day;
}
if (_day != d._day);
{
day_day += _day - d._day;
}
return year_day + month_day + day_day;
}
void Date::PrintDate()
{
cout << _year << ' ' << _month << ' ' << _day << endl;
}
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
int main()
{
Date d1(2004, 4, 6);
Date d2(2023, 10, 13);
cout << d2 - d1 << endl;
return 0;
}
- 以上就是我们通过学习类和对象编写的第一个cpp小项目,可以用来比较日期大小、相减得出两个日期间相差多少天、一个日期加上一个天数所得的另一个日期等等。
- 博主长期更新,博主的目标是不断提升阅读体验和内容质量,如果你喜欢博主的文章,请点个赞或者关注博主支持一波,我会更加努力的为你呈现精彩的内容。
🌈专栏推荐
更新不易,希望得到友友的三连支持一波。收藏这篇文章,意味着你将永久拥有它,无论何时何地,都可以立即找到重新阅读;关注博主,意味着无论何时何地,博主将永久和你一起学习进步,为你带来有价值的内容。