C/C+-日付計算機の実装
4456 ワード
日付計算機の実装
質問:
今日突然1つの問題が面白そうに見えることを見て、みんなと分かち合います.任意の日付に対してその日付を増減する日数を指定し、最後に増減した後に現れる
の日付および
に与える
2つの日付で彼ら2人の間に何日離れているかを得ることができます.(閏年を考慮する必要があります.毎月の日数が異なります.私たちは私たちを書く必要があります.
直接使用可能な日付の増減
)
時間が比較的に急いでいるため、私もインタフェースを書いていないで、その中のいくつかの主要な関数のアーキテクチャの思想と簡単な調子だけあります
試験が出た.
コード実装:
まとめ:
日付類は私たちが対象に向かうことをマスターするのに非常に重要な知識であり、少なくとも自分でこのフレームワーク全体を迅速に書くことができ、それから
一つ一つ
現関数、私はとても重要だとしか言えなくて、とても重要で、とても重要で、みんなは必ず掌握しなければなりません.
質問:
今日突然1つの問題が面白そうに見えることを見て、みんなと分かち合います.任意の日付に対してその日付を増減する日数を指定し、最後に増減した後に現れる
の日付および
に与える
2つの日付で彼ら2人の間に何日離れているかを得ることができます.(閏年を考慮する必要があります.毎月の日数が異なります.私たちは私たちを書く必要があります.
直接使用可能な日付の増減
)
時間が比較的に急いでいるため、私もインタフェースを書いていないで、その中のいくつかの主要な関数のアーキテクチャの思想と簡単な調子だけあります
試験が出た.
コード実装:
#include
#include
#include
using namespace std;
class Date
{
public:
Date(int year = 1997,int month = 1,int day = 1)
:years(year)
, months(month)
, days(day)
{
assert(IScorrect());
}
Date& operator=(const Date& d)
{
if (this != &d)
{
years = d.years;
months = d.months;
days = d.days;
}
return *this;
}
Date& operator + (int day)
{
while (day > 365)
{
if (ISleapyear() && day > 366)
{
years++;
day = day - 366;
}
else
{
years++;
day = day - 365;
}
}
while (day >= Getmonthsday())
{
// , . BUG .
day = day - Getmonthsday();
months++;
if (months > 12)
{
years++;
months = 1;
}
}
while (day > 0)
{
DateAdvance();
day = day - 1;
days++;
}
return *this;
}
Date& operator - (int day) // , , .
{
while (day > 365)
{
if (ISleapyear() && day > 366)
{
day = day - 366;
years--;
}
else
{
day = day - 365;
years--;
}
}
if (ISleapyear())
{
day = 366 - day;
years--;
}
else
{
day = 365 - day;
years--;
}
operator+(day);
return *this;
}
void DateAdvance() //
{
if (days > Getmonthsday())
{
months++;
days = 1;
}
if (months > 12)
{
years++;
months = 1;
}
}
int operator - (Date D)
{
int count = 0;
if (*this > D)
{
while (*this != D)
{
D.operator+(1);
count++;
}
}
else
{
while (*this != D)
{
operator+(1);
count++;
}
}
return count;
}
bool ISleapyear()
{
if ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0))
{
return true;
}
return false;
}
int Getmonthsday()
{
int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (ISleapyear() && months == 2)
{
return 29;
}
return monthDays[months];
}
void print()
{
cout << " ";
cout << years << "." << months << "." < 0 && ((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)) && days < 367)//
{
if (months >0 && months < 13)
{
if (days > 0 && days <= Getmonthsday())
{
return true;
}
}
}
else if (years >0 && days < 366) //
{
if (months >0 && months < 13)
{
if (days > 0 && days <= Getmonthsday())
{
return true;
}
}
}
return false;
}
Date operator += (int day)
{
*this = *this + 100;
return *this;
}
Date operator -= (int day)
{
return *this = *this - day;
}
inline Date& operator++()
{
*this += 1;
return *this;
}
inline Date operator++(int)
{
Date tmp(*this);
*this = *this + 1;
return tmp;
}
bool operator == (const Date& d)
{
return (years == d.years&& months == d.months&&days == d.days);
}
bool operator != (const Date& d)
{
return !(*this == d);
}
bool operator >(const Date& d)
{
if (years > d.years ||
(years == d.years&&months > d.months)
|| (years == d.years&&months == d.months && days > d.days))
{
return true;
}
return false;
}
bool operator < (const Date& d)
{
return !(*this > d);
}
bool operator >= (const Date& d)
{
return (*this == d) && (*this > d);
}
bool operator <= (const Date& d)
{
return (*this == d) && (*this < d);
}
private:
int years;
int months;
int days;
};
void Test()
{
Date d1(2012, 4, 5);
Date d2(2013, 4, 5);
d1.print();
/*d1 = d1 - 400;*/
d1.print();
cout << d1 - d2 << endl;
d1.print();
system("pause");
}
まとめ:
日付類は私たちが対象に向かうことをマスターするのに非常に重要な知識であり、少なくとも自分でこのフレームワーク全体を迅速に書くことができ、それから
一つ一つ
現関数、私はとても重要だとしか言えなくて、とても重要で、とても重要で、みんなは必ず掌握しなければなりません.