Class to implement a simple date calculator

Source: Internet
Author: User
Tags time and seconds

As a programmer, the concept of time has degenerated to a level of three-year-olds, often obsessed with writing a program that forgets time, an afternoon, a day, or even one weeks passed. For a just into the programmer's door to me, the time really is hurriedly slipped away, so often Baidu a date counter, calculate today to those special days still have how many days. After the use of a lot of the knowledge of the current reserve of programming can be implemented to achieve a simple date calculator. So I wrote this blog to share with you.

First of all, to design this date class, a date class should have a private data member for years, months, and days. In this we are not accurate to the time and seconds.

#pragma  once#include<iostream>using namespace std;class date{public:date (int  Year, int month, int day); ~date (); Date (Const date& a); Void ShowDate (); 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);D ate operator+  (int day);D ate&  operator+=  (Int day);D ate operator-  (int day);D ate& operator-=  (int  day);D ate& operator++ ();D ate operator++ (int);D ate& operator--();D ate  operator--(int); int operator-(const date& d);friend ostream* operator<< ( OSTREAM*&NBSP;OUT,&NBSP;CONST&NBSP;DATE&NBSP;&AMP;D);p Rivate:int getmonthday (int year, int  month); Bool isleapyear (int year);p rivate:int _year;int _month;int _day;}; 

As you can see, a date class should have roughly the same functionality. for Getmonthday () and isleapyear () are functions inside the class, they are only allowed within the class and do not want to be used by objects outside the class, so it is set to private member functions.

In fact, most of the member functions are relatively simple to implement, and I'll just pick out the important and error-prone functions to discuss.

(i) constructors

The constructor of a class is important, if the constructor is not written well, the consequences are unimaginable.

Date (int year, int month, int day) {if (Year < 1900| | month>12 | | month < 1| | day<1| | day>getmonthday (Year, Month) {cout << "initialization error, date reset to: 1900-1-1" << endl;_year = 1900;_month = 1;_day = 1;} Else{_year = Year;_month = Month;_day = Day;}}

Design a program to be designed to let users use good, in line with the public perception. The international standard Gregorian calendar has been timed since the beginning of the 1900-1-1. So the initialization should be processed to be less than 199-1-1.

(ii) Getmonthday () and Isleapyear ()

To make a program more readable and robust, we want to write as few duplicate code as possible. We encapsulate the code that we call frequently as a function, which is especially handy when modifying a program.

The Getmonthday () and Isleapyear () functions in this date class are often called functions, because the number of days in a year must be judged by whether they are common year or leap years, then the number of days per month and then the sum. So the two private functions together can get the number of days of a year .

BOOL Date::isleapyear (int year) {if (year% 4 = = 0 && year%! = 0| | year% = = 0) {return true;} Else{return false;}} int Date::getmonthday (int year, int month) {int montharray[13] = {0, A, a, a, a, a, a, a, to, a,, a, a, a};int Day = montharray[month];if (month = = 2 && isleapyear (year)) {Day + = 1;} Return day;}

here Getmonthday () uses a simple method to determine the number of days of a certain month, that is, using an array to save the number of days per month, the array's subscript represents the month, so the array size is 13, the following table is 0, not the 0月 .... . Then the judge is common year or leap year, if it is a leap year as long as the more that day plus on the line.

(iii) >,<,>=,<= can be classified as a category

As long as the implementation of < The other three can be used < to achieve.

BOOL date::operator< (const date& D) {if (this->_year>d._year) return False;else if (This->_year < d._ Year) return true;else{if (This->_month > D._month) return false;else if (This->_month < d._month) return true; Else{if (This->_day > D._day| | This->_day==d._day) return False;elsereturn True;}}}

(iv) +,-,+=,-= classified as a category

achieve + = and-=,+ and-can be implemented according to their

date& date::operator+=  (int day) {if  (day<0) { return  (*this -=  (-day));} else{this->_day += day;while  (This->_day > getmonthday (this->_year,  This->_month)) {this->_day -= getmonthday (this->_year, this->_month);if  (this- >_MONTH&NBSP;<&NBSP;12) {this->_month++;} else{this->_year++;this->_month %= 12;}} Return *this;}} date& date::operator-=  (Int day) {if  (day < 0) {return  (*this +=   (-day));} this->_day -= day;while  (this->_day < 1) {this->_month --;if  ( this->_month < 1) {this->_year--;this->_month += 12;if  (this->_year  < 1) {exit (1);}} This->_day += getmonthday (this->_year, this->_month);} Return *this;} 

In fact, it is very simple to achieve, I hope that friends can achieve one of their own, improve programming skills, attention to detail.

Class to implement a simple date calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.