⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 date.cpp

📁 C++程序设计教程(第二版)课件以及源代码 是初学者接触并能很好理解的参考书
💻 CPP
字号:
//=====================================
// date.cpp
//=====================================
#include"date.h"
#include<iostream>
#include<iomanip>
using namespace std;
//-------------------------------------
const int Date::tians[]={0, 31, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334};
//-------------------------------------
Date::Date(const string s){
  year = atoi(s.substr(0,4).c_str());
  month = atoi(s.substr(5,2).c_str());
  day = atoi(s.substr(8,2).c_str());
}//------------------------------------
void Date::i2ymd(int absDay){
  absDay = absDay>0 && absDay<3650000 ? absDay : 1;
  int n = absDay;
  for(year=1; n>isLeapYear()+365; n-=isLeapYear()+365, year++);
  for(month=1;(month<12&& n>(isLeapYear()&& month>2)+tians[month]); month++);
  day = n-(isLeapYear()&& month>2)-tians[month-1];
}//------------------------------------
int Date::ymd2i()const{
  int absDay =(year-1)*365 + (year-1)/4 - (year-1)/100 + (year-1)/400;
  return absDay += tians[month-1] + (isLeapYear()&& month>2) + day;
}//------------------------------------
void Date::print(std::ostream& o)const{
  o<<setfill('0')<<setw(4)<<year<<'-'<<setw(2)<<month<<'-'
   <<setw(2)<<day<<"\n"<<setfill(' ');
}//------------------------------------
IDate& createDate(int y, int m, int d){
  return *new Date(y,m,d);
}//------------------------------------
IDate& createDate(int n){
  return *new Date(n);
}//------------------------------------
IDate& createDate(const string s){
  return *new Date(s);
}//------------------------------------

 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -