/***************************************************************/ * Copyright (c) 2009 eryar All rights reserved. * * * * File : Main.CPP * * Date : 2009-03-17 21:12 * * Author : eryar@163.com * * * * Description: * * 中国有句俗语叫三天打鱼两天晒网. 某人从1990年1月1日开始"三天* * 打鱼两天晒网", 问这个人在以后的某一天中是在打鱼还是在晒网. * * * /***************************************************************/ #include <iomanip> #include <iostream> using namespace std; bool LeapYear(int year); class Date{ public: Date(int y=1990, int m=1, int d=1) : _year(y), _month(m), _day(d) {} int operator-(Date& date); bool isLeapYear(); void ShowDate() const; void SetDate(); ~Date(); protected: private: int _year; int _month; int _day; }; // End class date /* Parameter : Date & * Return : int * Description : Overload operator - to get the difference between two date. */ int Date::operator -(Date & date) { int Year = _year; int Month = _month; int Day = _day; int iTotalDays = 0; int iMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; // Initiate month if ( LeapYear(Year)) iMonth[2] = 29; // Leap year February is 29 days. else iMonth[2] = 28; while (!(Year == date._year && Month == date._month && Day == date._day)) { Day++; if ( Day == iMonth[Month]) { Day = 0; Month++; } if (Month == 13) { Month = 1; Year ++; if ( LeapYear(Year)) iMonth[2] = 29; // Leap year February is 29 days. else iMonth[2] = 28; } iTotalDays++; } return iTotalDays; } // End operator- /* Parameter : NONE * Return : void * Description : Display the date. */ void Date::ShowDate() const { cout<<_year<<"-"<<setw(2)<<setfill('0')<<_month<<"-"<<setw(2)<<setfill('0')<<_day<<endl; } // End ShowDate /* Parameter : NONE * Return : bool * Description : decided a year if a leap year or not. */ inline bool Date::isLeapYear() { if ((_year%4==0 && _year%100) || _year%400 ==0 ) return true; else return false; } // End isLeapYear /* Parameter : NONE * Return : NONE * Description : class Date destructor. */ void Date::SetDate() { int year = 0; int month = 0; int day = 0; cout<<"Input year : "; cin>>year; cout<<"Input month: "; cin>>month; cout<<"Input day : "; cin>>day; _year = year; _month = month; _day = day; } // End destructor /* Parameter : NONE * Return : NONE * Description : class Date destructor. */ Date::~Date() { //cout<<"[class Date destructor ]"<<endl; } // End destructor int main(int argc, char* argv[]) { Date yesterday; yesterday.ShowDate(); Date today(1993,10,25); today.SetDate(); today.ShowDate(); cout<<"Difference : "<<yesterday-today<<" days."<<endl; int isFishing = 0; isFishing = (yesterday - today) % 5; if (isFishing > 0 && isFishing < 4) cout<<"He was fishing at that day."<<endl; else cout<<"He was sleeping at that day."<<endl; return 0; } bool LeapYear(int year) { if ((year%4 ==0 && year%100) || year%400 ==0) return true; else return false; } /* 问题分析与算法设计 ------------------- 根据题意可以将解题过程分为三步: 1)计算从1990年1月1日开始至指定日期共有多少天; 验证网址: http://tool.114la.com/date_different.html 2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除; 3)根据余数判断他是在“打鱼”还是在“晒网”; 若 余数为1,2,3,则他是在“打鱼” 否则 是在“晒网” 在这三步中,关键是第一步。 求从1990年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,二月为29天,平年为28天。 闰年的方法可以用伪语句描述如下: 如果 ((年能被4除尽 且 不能被100除尽)或 能被400除尽)则 该年是闰年; 否则 不是闰年。 C语言中判断能否整除可以使用求余运算(即求模) */