成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

用c++實現(xiàn)的簡單的日期類-創(chuàng)新互聯(lián)

 自己寫的這個日期類實現(xiàn)了簡單的一些日期可能會用到的功能,

前鋒ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

比如加減某一個日期等等,詳細的已在代碼里面標注出來了。

#include <iostream>
using namespace std;

class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
  :_year(year)   //初始化列表
    ,_month(month)
    ,_day(day)
 {
  if (year < 1900
   ||month < 1 || month > 12
   || day < 1 || day > GetMonthDay(year,month))
  {
   cout << "Invalid Date" << endl;
  }
 }

 int GetMonthDay(int year, int month)
 {
  int monthArray[12] = { 31, 28, 31, 30, 31,
   30, 31, 31, 30, 31, 30, 31 };
  if (IsLeapYear(year)&&month==2)
  {
   return monthArray[month - 1] + 1;
  }
  return monthArray[month - 1];
 }

 bool IsLeapYear(int year)
 {
  return year % 400 == 0 || year % 4 == 0
   && year % 100 != 0;
 }

 Date operator+(int day)     //加一個天數(shù)
 {
  Date tmp(*this);
  tmp._day += day;
  while (tmp._day > GetMonthDay(tmp._year,tmp._month))
  {
   tmp._day -= GetMonthDay(tmp._year, tmp._month);

   if (tmp._month == 12)
   {
    tmp._year += 1;
    tmp._month = 1;
   }
   else
   {
    tmp._month += 1;
   }

  }
  return tmp;
 }

 Date& operator+=(int day)
 {
  this->_day += day;
  while (this->_day > GetMonthDay(this->_year, this->_month))
  {
   this->_day -= GetMonthDay(this->_year, this->_month);

   if (this->_month == 12)
   {
    this->_year += 1;
    this->_month = 1;
   }
   else
   {
    this->_month += 1;
   }

  }
  return *this;
 }

 Date operator-(int day)   //減一個天數(shù)
 {
  Date tmp(*this);
  tmp._day -= day;
  while (tmp._day < 0)
  {
   if (tmp._month == 1)
   {
    tmp._year -= 1;
    tmp._month = 12;
    tmp._day += GetMonthDay(tmp._year, tmp._month);
   }
   else
   {
    tmp._month -= 1;
    tmp._day += GetMonthDay(tmp._year, tmp._month);
   }

  }
  return tmp;
 }

 Date operator++() //前置自增
 {
  *this += 1;
  return *this;
 }

 bool operator==(const Date& d)  //測試日期相等
 {
  return _year == d._year&&
   _month == d._month&&
   _day == d._day;
 }

 bool operator!=(const Date& d)
 {
  return !(_year == d._year&&
   _month == d._month&&
   _day == d._day);
 }

 bool operator<(const Date& d)
 {
  return !(_year < d._year&&
   _month < d._month&&
   _day < d._day);
 }

 int operator-(const Date& d)  //一個日期減一個日期
 {
  Date tmp1 = d;
  Date tmp2 = *this;

  int count = 0;
  if (tmp2 < tmp1)
  {
   Date rev;
   rev = tmp2;
   tmp2 = tmp1;
   tmp1 = rev;
  }

  while (tmp2 != tmp1)
  {
   count++;
   ++tmp1;
  }

  return count;
 }

 void Display()
 {
  cout << _year << "-" << _month << "-" << _day << endl;
 }

private:
 int _year;
 int _month;
 int _day;
};

void Test1() //測試加減某一個天數(shù)
{
 int day;
 Date d1(2015,3,1);
 d1.Display();
 cin >> day;
 //(d1 + day).Display();
 (d1 - day).Display();
}

void Test2()
{
 Date d1(2015, 2, 28);
 //d1.Display();

 Date d2(2015, 2, 28);
 cout << (d1 != d2) << endl;
 //(d1++).Display();

}

void Test3()
{
 Date d1(2004, 2, 1);
 Date d2(2004, 3, 1);

 int ret = d1 - d2;
 cout << ret << endl;
}

int main()
{
 //Test1();
 //Test2();
 Test3();
 return 0;
}

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文名稱:用c++實現(xiàn)的簡單的日期類-創(chuàng)新互聯(lián)
文章出自:http://jinyejixie.com/article48/ccjhhp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供云服務器、網(wǎng)站制作、網(wǎng)站維護、服務器托管、做網(wǎng)站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設
长葛市| 登封市| 榆中县| 台中县| 万山特区| 教育| 泸州市| 北安市| 韩城市| 淮南市| 开江县| 陵川县| 社旗县| 北碚区| 中超| 辽中县| 商城县| 三原县| 乐清市| 长垣县| 兴安县| 天峨县| 昌江| 和静县| 靖远县| 尼木县| 遵义市| 儋州市| 嵊州市| 海兴县| 腾冲县| 衡阳县| 澳门| 阿勒泰市| 嘉兴市| 防城港市| 盱眙县| 丰都县| 鄯善县| 余姚市| 博白县|