自己写的一个小程序
程序代码:
#include <iostream> using namespace std; int g_Month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};//月份 int g_WeenDay[7] = {1,2,3,4,5,6,7}; //星期 int g_iYear = 2000 , g_iMonth = 1 , g_iDay = 1 , g_iWeenDay = 6;//初始化2000年1月1日为星期六; class cDateTime { private: int M_Year; int M_Month; int M_Day; int M_DayNum;//相隔的天数 int M_YearNum;//一年的天数 public: cDateTime(); void SetInfo(int iYear , int iMonth , int iDay); void bRunYear(int iYear); void iYearCount(int iYear = g_iYear , int iMonth = g_iMonth , int iDay = g_iDay); int WeenDay(int iTmp); void fDay();//某个月第一天是星期几 void sDay();//两个时间相隔多少天 void nDay();//某一天的下一天是几号 void wDay(int iWeenDay);//求某年某月的第几周是哪几天 }; cDateTime::cDateTime() { M_Year = 0; M_Month = 0; M_Day = 0; M_DayNum = 0; } void cDateTime::SetInfo(int iYear , int iMonth , int iDay) { M_Year = iYear; M_Month = iMonth; M_Day = iDay; } void cDateTime::bRunYear(int iYear) { if (iYear % 4 == 0 && iYear % 100 != 0 || iYear % 100 ==0 && iYear % 400 ==0) { g_Month[1] = 29; M_YearNum = 366; } else { g_Month[1] = 28; M_YearNum = 365; } } void cDateTime::iYearCount(int iYear , int iMonth , int iDay)//总共的天数 { //int iDayNum = 0; for (int i = iYear; i < M_Year; ++i) { bRunYear(i); M_DayNum += M_YearNum; } bRunYear(M_Year); for (i = 0; i < M_Month - 1; ++i) { M_DayNum += g_Month[i]; } M_DayNum += M_Day; ////////////////开始计算多出来的天数////////////////// bRunYear(iYear); if (iMonth > 1) { for (i = 0; i < iMonth - 1; ++i) { M_DayNum -= g_Month[i]; } } if (iDay > 1) { M_DayNum -= iDay; ++M_DayNum; } /////////////////减掉多出来的天数///////////////////// } int cDateTime::WeenDay(int iTmp) { int iTmpDay = 1 , iTmpWeenday = g_iWeenDay - 1 , Tmp; while (iTmpDay <= iTmp) { if (iTmpWeenday > 6) { iTmpWeenday = 0; } Tmp = g_WeenDay[iTmpWeenday]; ++iTmpWeenday; ++iTmpDay; } return Tmp; } void cDateTime::fDay() { cout << M_Year << "年" << M_Month << "月的第1天是星期" << WeenDay(M_DayNum) << endl; } void cDateTime::sDay() { cout << "您输入的这两个日期相差" << M_DayNum - 1 << "天" << endl; } void cDateTime::nDay() { bRunYear(M_Year); ++M_Day; if (M_Day > g_Month[M_Month - 1]) { M_Day = 1; } cout << "您输入的日期下一天为:" << M_Day << "号" << endl; } void cDateTime::wDay(int iWeenDay) { int iTmp = 0; for (int i = 0; i < g_Month[M_Month - 1]; ++i) { if (WeenDay(M_DayNum + i) == 1)//第一个周开始 { ++iTmp; } if (iTmp==iWeenDay) { cout << "第" << iWeenDay << "周有:" << i+1 << " " << endl; } } } int main() { int iYear , iMonth; cDateTime Fist; cout << "求某个月的第一天是星期几" << endl; cout << "请输入年:"; cin >> iYear; cout << "请输入月:"; cin >> iMonth; Fist.SetInfo(iYear , iMonth , 1); Fist.iYearCount(); Fist.fDay(); cout << "-------------------黄金分割线----------------------------" << endl; int StarYear , StarMonth , StarDay , LastYear , LastMonth , LastDay; cDateTime Second; cout << "求两个时间相隔多少天" << endl; cout << "请输入起始年:"; cin >> StarYear; cout << "请输入起始月:"; cin >> StarMonth; cout << "请输入起始日:"; cin >> StarDay; cout << "请输入结束年:"; cin >> LastYear; cout << "请输入结束月:"; cin >> LastMonth; cout << "请输入结束日:"; cin >> LastDay; Second.SetInfo(LastYear , LastMonth , LastDay); Second.iYearCount(StarYear , StarMonth , StarDay); Second.sDay(); cout << "-------------------黄金分割线----------------------------" << endl; cDateTime Again; cout << "求某天的下一天是几号" << endl; cout << "请输入年:"; cin >> StarYear; cout << "请输入月:"; cin >> StarMonth; cout << "请输入日:"; cin >> StarDay; Again.SetInfo(StarYear , StarMonth , StarDay); Again.nDay(); cout << "-------------------黄金分割线----------------------------" << endl; cDateTime Four; cout << "求某年某月的第几周是哪几天" << endl; cout << "请输入年:"; cin >> StarYear; cout << "请输入月:"; cin >> StarMonth; cout << "请输入第几周:"; cin >> StarDay; Four.SetInfo(StarYear , StarMonth , 1); Four.iYearCount(); Four.wDay(StarDay); return 0; }