开发工具:
文件大小: 8kb
下载次数: 0
上传时间: 2009-12-28
详细说明: #include using namespace std; #include "MyDate.h" #include bool MYDATE::dowFlag = false; void MYDATE::Input() { int year, month, day; char c1, c2; while(!(cin >> year >> c1 >> month >> c2 >> day) || ! IsValid(year, month, day) || c1 != '-' || c2 != '-') { cout << "不正确的日期! 请重新输入: "; if(! cin.good()) cin.clear(); while(cin.get() != '\n') ; } this->year = year; this->month = month; this->day = day; } void MYDATE::Outpu t() { char *adow[] = {"日", "一", "二", "三", "四", "五", "六"}; int w = Dow(); cout << setfill('0') << setw(2) << year << '-' << setw(2) << month << '-' << setw(2) << day << setfill(' '); if(dowFlag) { cout << "(星期" << adow[w] << ")"; } } void MYDATE::Set(int year, int month, int day) { if(IsValid(year, month, day)) { this->year = year; this->month = month; this->day = day; } else { cout << "不正确的时间,设置失败!\n"; } } void MYDATE::Get(int &year, int &month, int &day) { year = this->year; month = this->month; day = this->day; } int MYDATE::Year() { return year; } void MYDATE::Year(int year) { if(year > 0) { this->year = year; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Month() { return month; } void MYDATE::Month(int month) { if(month > 0 && month <= 12) { this->month = month; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Day() { return day; } void MYDATE::Day(int day) { int n = Dom(year, month); if(day <= 0) day = 1; else if(day > n) day = n; this->day = day; } MYDATE MYDATE::Add(int x) { int y = 1,i = 1,m = 1; MYDATE c; double s = this->Tod() + x; if(s >= 0) { y = 1; while(s >= 365 + IsLeap(y)) { s = s - (365 + IsLeap(y++)); i ++; } c.year = i; while(s >= Dom(c.year,m)) { s = s - Dom(c.year,m); m ++; } c.month = m; if(s == 0) c.day = 1; else c.day = s; } else cout << "Error!\n"; return c; } MYDATE MYDATE::Sub(int x) { MYDATE c; c.Set(year, month, day); c = c.Add(-x); return c; } bool MYDATE::IsValid(int year, int month, int day) { return year > 0 && month > 0 && month <= 12 && day > 0 && day <= Dom(year, month); } int MYDATE::Dom(int year, int month) { int n = 0; const static char adom[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month > 0 && month <= 12) { n = adom[month - 1]; if(month == 2 && IsLeap(year)) n ++; } return n; } int MYDATE::Doy() { const static short adoy[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int n = adoy[month - 1] + day; if(month > 2 && IsLeap(year)) n ++; return n; } bool MYDATE::IsLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } int MYDATE::Nol(int year) { return year / 4 - year / 100 + year / 400; } int MYDATE::Tod() { int t = year - 1; return t * 365 + Nol(t) + Doy(); } int MYDATE::Dow() { return Tod() % 7; } int MYDATE::Sub(MYDATE &x) { return this->Tod() - x.Tod(); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.