| 導購 | 订阅 | 在线投稿
分享
 
 
 

Date類(C++ Programming Language第10章課後題10.6.2的一部分)

來源:互聯網網民  2006-09-16 05:50:36  評論

//Data.h

#include <iostream>

#include <string>

using namespace std;

class Date

{

public:

enum Month { jan = 1, feb, mar, apr, may, jun,

jul, aug, sep, oct, nov, dec };

class BadDate{ };

Date(int dd = 0, Month mm = Month(0), int yy = 0);

int day( ) const;

Month month( ) const;

int year( ) const;

//string string_rep( ) const;

//void char_rep(char s[]) const;

void print_date( );

//使用Date之前應調用set_default函數設置缺省日期

static void set_default(int, Month, int);

Date& add_year(int n);

Date& add_month(int n);

Date& add_day(int n);

private:

int d;

Month m;

int y;

static Date default_date;

int get_days(Month mm);

};

int leapyear(int yy);

//Date.cpp

#include "Date.h"

Date::Date(int dd, Month mm, int yy)

{

if(yy == 0)

y = default_date.year( );

if(mm == Month(0))

m = default_date.month( );

if(dd == 0)

d = default_date.day( );

int max;

switch(mm)

{

case feb:

max = 28 + leapyear(yy);

break;

case apr:

case jun:

case sep:

case nov:

max = 30;

break;

case jan:

case mar:

case may:

case jul:

case aug:

case oct:

case dec:

max = 31;

break;

default:

throw BadDate( );

}

if(dd < 1 || dd > max)

throw BadDate( );

y = yy;

m = mm;

d = dd;

}

int Date::day( ) const

{

return d;

}

enum Date::Month Date::month( ) const

{

return m;

}

int Date::year( ) const

{

return y;

}

void Date::set_default(int d, Month m, int y)

{

default_date = Date(d, m, y);

}

Date& Date::add_year(int n)

{

if(n != 0)

{

if( d == 29 && m == feb && !leapyear(y + n) )

{

d = 28;

}

y = y + n;

}

return *this;

}

Date& Date::add_month(int n)

{

if(n == 0)

return *this;

if(n > 0)

{

int delta_y = n/12;

int mm = m + n%12;

if(mm > 12)

{

delta_y++;

mm -= 12;

}

y += delta_y;

if(mm == 2 && d >28)

{

if( leapyear(y) )

d = 29;

else

d = 28;

}

if(d == 31)

{

if( (mm == apr) || (mm == jun) ||

(mm == sep) || (mm == nov) )

{

d = 30;

}

}

m = Month(mm);

return *this;

}

if(n < 0)

{

n = -n;

int delta_y = n/12;

int mm = m - n%12;

if(mm <= 0)

{

delta_y++;

mm += 12;

}

y -= delta_y;

if(mm == 2 && d >28)

{

if( leapyear(y) )

d = 29;

else

d = 28;

}

if(d == 31)

{

if( (mm == apr) || (mm == jun) ||

(mm == sep) || (mm == nov) )

{

d = 30;

}

}

m = Month(mm);

return *this;

}

return *this;

}

Date& Date::add_day(int n)

{

if( n == 0 )

return *this;

if( n > 0 )

{

//計算本月剩余天數

int totalDays = get_days(m);

int daysLeft = totalDays - d;

if(n <= daysLeft)

{

d = d + n;

}

else if(n > daysLeft)

{

//把日期調整到下個月1號

n = n - (daysLeft + 1);

add_month(1);

d = 1;

//模擬時間按月的流逝,最後得到正確的日期

int daysInMonth = get_days(m);

while( n >= daysInMonth )

{

add_month(1);

n = n - daysInMonth;

daysInMonth = get_days(m);

}

d = d + n;

}

}

else if( n < 0 )

{

//把n變爲正值

n = -n;

//計算本月剩余天數

int totalDays = get_days(m);

int daysLeft = totalDays - d;

//把時間調整到本月的最後一天

n = n + daysLeft;

d = d + daysLeft;

//模擬時間按月的倒流,最後得到正確的日期

int daysInMonth = get_days(m);

while( n >= daysInMonth )

{

add_month(-1);

n = n - daysInMonth;

daysInMonth = get_days(m);

}

d = d - n;

}

return *this;

}

int Date::get_days(Month mm)

{

int days;

if(mm == 0)

return 0;

switch(mm)

{

case jan:

case mar:

case may:

case jul:

case aug:

case oct:

case dec:

days = 31;

break;

case apr:

case jun:

case sep:

case nov:

days = 30;

break;

case feb:

days = 28 + leapyear(y);

}

return days;

}

void Date::print_date( )

{

cout<<y<<'-'<<m<<'-'<<d<<endl;

}

int leapyear(int yy)

{

int iRet = 0;

if( (yy % 4 == 0) && (yy % 100 != 0) )

iRet = 1;

if( yy % 400 == 0 )

iRet = 1;

return iRet;

}

Date Date::default_date(3, feb, 1981);

int main( )

{

//Date::set_default();

Date testDate(29, Date::Month::feb, 2000);

testDate.print_date( );

cout<<"after add -1 years:";

testDate.add_year(-1);

testDate.print_date( );

system("pause");

return 0;

}

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
//Data.h #include <iostream> #include <string> using namespace std; class Date { public: enum Month { jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec }; class BadDate{ }; Date(int dd = 0, Month mm = Month(0), int yy = 0); int day( ) const; Month month( ) const; int year( ) const; //string string_rep( ) const; //void char_rep(char s[]) const; void print_date( ); //使用Date之前應調用set_default函數設置缺省日期 static void set_default(int, Month, int); Date& add_year(int n); Date& add_month(int n); Date& add_day(int n); private: int d; Month m; int y; static Date default_date; int get_days(Month mm); }; int leapyear(int yy); //Date.cpp #include "Date.h" Date::Date(int dd, Month mm, int yy) { if(yy == 0) y = default_date.year( ); if(mm == Month(0)) m = default_date.month( ); if(dd == 0) d = default_date.day( ); int max; switch(mm) { case feb: max = 28 + leapyear(yy); break; case apr: case jun: case sep: case nov: max = 30; break; case jan: case mar: case may: case jul: case aug: case oct: case dec: max = 31; break; default: throw BadDate( ); } if(dd < 1 || dd > max) throw BadDate( ); y = yy; m = mm; d = dd; } int Date::day( ) const { return d; } enum Date::Month Date::month( ) const { return m; } int Date::year( ) const { return y; } void Date::set_default(int d, Month m, int y) { default_date = Date(d, m, y); } Date& Date::add_year(int n) { if(n != 0) { if( d == 29 && m == feb && !leapyear(y + n) ) { d = 28; } y = y + n; } return *this; } Date& Date::add_month(int n) { if(n == 0) return *this; if(n > 0) { int delta_y = n/12; int mm = m + n%12; if(mm > 12) { delta_y++; mm -= 12; } y += delta_y; if(mm == 2 && d >28) { if( leapyear(y) ) d = 29; else d = 28; } if(d == 31) { if( (mm == apr) || (mm == jun) || (mm == sep) || (mm == nov) ) { d = 30; } } m = Month(mm); return *this; } if(n < 0) { n = -n; int delta_y = n/12; int mm = m - n%12; if(mm <= 0) { delta_y++; mm += 12; } y -= delta_y; if(mm == 2 && d >28) { if( leapyear(y) ) d = 29; else d = 28; } if(d == 31) { if( (mm == apr) || (mm == jun) || (mm == sep) || (mm == nov) ) { d = 30; } } m = Month(mm); return *this; } return *this; } Date& Date::add_day(int n) { if( n == 0 ) return *this; if( n > 0 ) { //計算本月剩余天數 int totalDays = get_days(m); int daysLeft = totalDays - d; if(n <= daysLeft) { d = d + n; } else if(n > daysLeft) { //把日期調整到下個月1號 n = n - (daysLeft + 1); add_month(1); d = 1; //模擬時間按月的流逝,最後得到正確的日期 int daysInMonth = get_days(m); while( n >= daysInMonth ) { add_month(1); n = n - daysInMonth; daysInMonth = get_days(m); } d = d + n; } } else if( n < 0 ) { //把n變爲正值 n = -n; //計算本月剩余天數 int totalDays = get_days(m); int daysLeft = totalDays - d; //把時間調整到本月的最後一天 n = n + daysLeft; d = d + daysLeft; //模擬時間按月的倒流,最後得到正確的日期 int daysInMonth = get_days(m); while( n >= daysInMonth ) { add_month(-1); n = n - daysInMonth; daysInMonth = get_days(m); } d = d - n; } return *this; } int Date::get_days(Month mm) { int days; if(mm == 0) return 0; switch(mm) { case jan: case mar: case may: case jul: case aug: case oct: case dec: days = 31; break; case apr: case jun: case sep: case nov: days = 30; break; case feb: days = 28 + leapyear(y); } return days; } void Date::print_date( ) { cout<<y<<'-'<<m<<'-'<<d<<endl; } int leapyear(int yy) { int iRet = 0; if( (yy % 4 == 0) && (yy % 100 != 0) ) iRet = 1; if( yy % 400 == 0 ) iRet = 1; return iRet; } Date Date::default_date(3, feb, 1981); int main( ) { //Date::set_default(); Date testDate(29, Date::Month::feb, 2000); testDate.print_date( ); cout<<"after add -1 years:"; testDate.add_year(-1); testDate.print_date( ); system("pause"); return 0; }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有