回复 9楼 rjsp
这是程序显示的错误!
回复 11楼 鱼游海底
不好意思,能贴文字就贴文字,不能贴文字就贴图。你放在rar中我不想打开,我也没有MS Word软件。再说看到你出错了有什么用呢,又不知道你代码是怎么写的。
main.cpp::: #include <iostream> #include <string> #include "Date.h" using namespace std; int main() { Date d(2008, 8, 8); d.Print(); cout << d.Date2Days() << endl; d = Date::Days2Date(d.Date2Days()); d.Print(); d = Date::Days2Date(2000); d.Print(); cout << d.toString("MM/dd/yyyy") <<endl; cout << d.nthDay() << endl; cout << d.nthWeek() <<endl; d.NextDate().Print(); d.PrevDate().Print(); cout << d.DiffDate(Date::Today()) << endl; d.AfterNDays(100).Print(); cout << d.Equal(Date::Today()) << endl; cout << d.Earlier(Date::Today()) << endl; }Date.h:::
Date.cpp::: #include <iostream> #include <stdio.h> #include <cassert> #include <time.h> #include "Date.h" using namespace std; int Date::nDaysofMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; Date::Date(int y, int m, int d) { SetDate(y, m, d); } Date::Date(const Date& d) { if(this != &d) { year = d.year; month = d.month; day = d.day; } } Date& Date::operator = (const Date& rhs) { if(this != &rhs) { year = this->year; month = this->month; day = this->day; } return *this; } void Date::SetDate(int y, int m, int d) { year = y >= YearBase ? y : YearBase; month = m >= 1 && m <= 12 ? m : MonthBase; this->LeapYearEnabled(); day = d >= 1 && d <= nDaysofMonth[month] ? d : DayBase; } void Date::LeapYearEnabled() const { if(IsLeapYear(year)) nDaysofMonth[2] = 29; else nDaysofMonth[2] = 28; } void Date::Print() const { cout << this->year << "-" << this->month << "-" << this->day << endl; } bool Date::IsLeapYear(int y) { return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0); } int Date::DaysOfThisYear(int y) { if(IsLeapYear(y)) return 366; else return 365; } int Date::Date2Days() const { this->LeapYearEnabled(); int s = 0; s += day; for(int m = 1; m < month; ++m) s += nDaysofMonth[m]; for(int y = YearBase; y < year; ++y) s += DaysOfThisYear(y); return s; } Date Date::Days2Date(int n) { Date d; int y = YearBase; while(n >= DaysOfThisYear(y)) { n -= DaysOfThisYear(y); ++y; if(n == DaysOfThisYear(y)) break; } d.year = y; d.LeapYearEnabled(); int m = 1; while(n >= nDaysofMonth[m]) { n -= nDaysofMonth[m]; ++m; if(n == nDaysofMonth[m]) break; } d.month = m; d.day = n; return d; } string Date::toString(string format_flag) const { int length = format_flag.size(); char *a = new char; if(length == 6) {sprintf(a, "%s/%s/%s", month, day, year);} if(length == 10) {sprintf(a, "%02s/%02s/%04s", month, day, year);} if(length == 12) { switch(month) { case 1 : sprintf(a, "January %02s,%04s", day, year); break; case 2 : sprintf(a, "Febrary %02s,%04s", day, year); break; case 3 : sprintf(a, "March %02s,%04s", day, year); break; case 4 : sprintf(a, "April %02s,%04s", day, year); break; case 5 : sprintf(a, "May %02s,%04s", day, year); break; case 6 : sprintf(a, "June %02s,%04s", day, year); break; case 7 : sprintf(a, "July %02s,%04s", day, year); break; case 8 : sprintf(a, "Augest %02s,%04s", day, year); break; case 9 : sprintf(a, "September %02s,%04s", day, year); break; case 10 : sprintf(a, "October %02s,%04s", day, year); break; case 11 : sprintf(a, "November %02s,%04s", day, year); break; case 12 : sprintf(a, "December %02s,%04s", day, year); break; } } return string((string)a); } Date Date::Today() { time_t timer; time(&timer); tm* t_tm = localtime(&timer); return Date(t_tm->tm_year, t_tm->tm_mon, t_tm->tm_mday); } int Date::nthDay() const { Date now; now = now.Today(); LeapYearEnabled(); int sum = 0; sum += day; for(int m = 1; m <= month; m++) sum += nDaysofMonth[m]; return sum; } int Date::nthWeek() const { int sum(nthDay()); return (sum / 7 + 1); } Date Date::NextDate() const { int sum(Date2Days() + 1); return Days2Date(sum); } Date Date::PrevDate() const { int sum(Date2Days() - 1); return Days2Date(sum); } int Date::DiffDate(const Date& d) const { int sum1(Date2Days()); int sum2(d.Date2Days()); return (sum1 - sum2) > 0 ? (sum1 - sum2) : (sum2 - sum1); } Date Date::AfterNDays(int n) const { int sum(Date2Days() + n); return Days2Date(sum); } bool Date::Equal(const Date& d) const { if(year == d.day && month == d.month && day == d.day) return true; else return false; } bool Date::Earlier(const Date& d) const { if(year < d.year || (year == d.year && month < d.month) || (year == d.year && month == d.month && day < d.day)) return true; else return false; }