C++ achieve perpetual calendar function
- 2020-06-19 11:21:46
- OfStack
Example of this article for you to share C++ to achieve the specific code of perpetual calendar, for your reference, the specific content is as follows
1. This perpetual calendar function
1 > Date plus or minus days
2 > The difference between dates
3 > Enter the month to display the calendar month
2. Code implementation
#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1) // The constructor
:_year(year), _month(month), _day(day)
{
if (JudgeRightDate()) // Determines whether the value passed in is valid or not 1990 years 1 month 1 day
{
_year = 1990;
_month = 1;
_day = 1;
}
}
bool JudgeRightDate() // Determine if the value is a valid function
{
if (_year < 1 || ((_month< 1)||_month>12) ||
(_day<1)||_day>GetMonthDay(_year,_month))
{
return true;
}
else
{
return false;
}
}
int JudgeYear(int year) // Whether it's a function of leap years
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
return 1;
}
else
return 0;
}
int GetMonthDay(int year, int month) // Get the number of days by year and month
{
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = arr[month];
if (month == 2)
{
days += JudgeYear(year); // If it's a leap year 2 The months are days plus 1
}
return days;
}
Date operator +(int days) // Date plus days function, overload." + "Implementation
{
_day += days; // The number of days is first added to the "day" of the given date
GetRightDate(_year, _month, _day); // And then calculate the correct date.
return *this;
}
void GetRightDate(int &year, int &month, int &day) // Calculate the correct date
{
if (day <= 0)
{
while (day <= 0)
{
month--;
day += GetMonthDay(year, month);
if (month < 1)
{
year--;
month = 13;
}
}
}
else
{
while (day>GetMonthDay(year, month))
{
day -= GetMonthDay(year, month);
month++;
if (month > 12)
{
year++;
month = 1;
}
}
}
}
Date operator -(int days) // Overloading" - "Implementation date minus days
{
_day -= days;
GetRightDate(_year, _month, _day);
return *this;
}
bool operator >(const Date &d) // Determine the size of the two dates
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month==d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
bool operator ==(const Date &d) // Determine if two dates are equal
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
return false;
}
int operator -( Date &d) // Calculate the date difference function, overload" - "Implementation
{
int count = 0;
Date tmp(*this);
if (*this > d)
{
tmp = d;
d = *this;
*this = tmp;
}
while (!(*this==d))
{
count++;
*this =*this+1;
}
return count;
}
void print() // Print function
{
cout << _year << "-" << _month << "-" << _day;
}
int week() // Find the day of the week function for the date
{
int w = 0;
int y = _year;
int m = _month;
if (m == 1 || m == 2)
{
m = _month + 12;
y = _year - 1;
}
w = _day + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400;
w = w % 7 + 1;
return w;
}
void print_week()
{
cout << " Sunday week 1 week 2 week 3 week 4 week 5 week 6" << endl;
}
void print_day() // According to the date and the week, the correct output calendar
{
int line = 1;
int days = GetMonthDay(_year,_day);
int w = week();
if (w != 7)
{
for (int blank = w - 1; blank; --blank, ++line)
{
cout << setw(7) << "";
}
}
for (int d = 1; d <= days; ++d, ++line)
{
cout << setw(7) << d;
if (line % 7 == 0)
{
cout << endl;
}
}
cout << endl;
}
private:
int _year;
int _month;
int _day;
};
void menu()
{
cout << setw(40) <<" calendar "<< endl;
cout << "1. Date plus or minus days " << endl;
cout << "2. Date minus date " << endl;
cout << "3. Enter the month to display the calendar month " << endl;
}
void choice()
{
int num = 0;
int year, month, day, days;
char ch = '+';
cin >> num;
if (num == 1)
{
cout << " Please enter the date: " << endl;
cin >> year >> month >> day;
cout << " Please enter days :" << endl;
cin >> days;
cout << " Please enter the '+' or '-':" << endl;
cin >> ch;
Date d1(year, month, day);
Date d2;
if (ch == '+')
{
d2 = d1 + days;
}
else if (ch == '-')
{
d2 = d1 - days;
}
else
{
cout << " Invalid input !" << endl;
}
cout << " The calculated date is: ";
d2.print();
cout << endl;
}
else if (num==2)
{
cout << " Please enter the date: " << endl;
cin >> year >> month >> day;
Date d3(year, month, day);
cout << " Please enter the date: " << endl;
cin >> year >> month >> day;
Date d4(year, month, day);
int ret = d3 - d4;
cout << " Period difference: " << ret << endl;
}
else if (num == 3)
{
cout << " Please enter date :" << endl;
cin >> year >> month;
Date d5(year, month);
d5.print_week();
d5.print_day();
}
}
int main()
{
menu();
choice();
system("pause");
return 0;
}