自己写的时间功能的小程序~
程序代码:
#include <iostream> using namespace std; enum {YEAR = 2000 , MONTH = 1 , DAY = 1 , WEENDAY = 6}; class CDateTime { int m_iYear; int m_iMonth; int m_iDay; int m_iHour; int m_iMinute; int m_iSecond; public: CDateTime(); CDateTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond); void SetDateTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond); void GetMonthFirstDayWeek(char * szTmp);//某个月第一天是星期几 void DayToDay(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond);//两个时间相隔多少天 CDateTime GetNextDay();//某一天的下一天是几号 void GetWeekDayInfo(int iWeenDay , char * pszInfo);//求某年某月的第几周是哪几天 void GetShowWeekDay(char * pszInfo);//显示那个周有哪几天 void GetShowToDay();//显示私有变量天 private: int DayNum(int iYear=YEAR, int iMonth=MONTH, int iDay=DAY);//与初始值相隔的天数 int GetYearDays();//求年有多少天 int GetMonthDays();//求月份有多少天 bool bRunYear();//计算闰年 int WeenDay();//计算周数 }; CDateTime::CDateTime() { m_iYear = 0; m_iMonth = 0; m_iDay = 0; m_iHour = 0; m_iMinute = 0; m_iSecond = 0; } CDateTime::CDateTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond) { m_iYear = iYear; m_iMonth = iMonth; m_iDay = iDay; m_iHour = iHour; m_iMinute = iMinute; m_iSecond = iSecond; } void CDateTime::SetDateTime(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond) { m_iYear = iYear; m_iMonth = iMonth; m_iDay = iDay; m_iHour = iHour; m_iMinute = iMinute; m_iSecond = iSecond; } bool CDateTime::bRunYear()//判断闰年 { if (m_iYear % 4 == 0 && m_iYear % 100 != 0 || m_iYear % 100 ==0 && m_iYear % 400 ==0) { return true; } else { return false; } } int CDateTime::GetYearDays()//计算一年多少天 { int iTmp; if (bRunYear()) { iTmp = 366; } else { iTmp = 365; } return iTmp; } int CDateTime::GetMonthDays()//计算一个月多少天 { int iMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; if (bRunYear()) { iMonth[1] = 29; } return iMonth[m_iMonth - 1]; } int CDateTime::DayNum(int iYear, int iMonth, int iDay)//判断输入与初始年份相差多少天 { int iTmp = 0 , i; CDateTime cDayNum; for (i = iYear; i < m_iYear; ++i) { cDayNum.SetDateTime(i,0,0,0,0,0); iTmp += cDayNum.GetYearDays(); } for (i = 1; i < m_iMonth; ++i) { cDayNum.SetDateTime(m_iYear,i,0,0,0,0); iTmp += cDayNum.GetMonthDays(); } iTmp += m_iDay; ////////////////开始计算多出来的天数//////////////注:如果起始月日是1月1号的话这段可以不要~//// if (iMonth > 1) { for (i = 1; i < iMonth; ++i) { cDayNum.SetDateTime(iYear,i,0,0,0,0); iTmp -= cDayNum.GetMonthDays(); } } if (iDay > 1) { iTmp -= iDay; } /////////////////减掉多出来的天数///////////////////// return iTmp; } int CDateTime::WeenDay() { int iTmpDay = 0 , iTmpWeenday = WEENDAY - 1 , Tmp; int weenday[7] = {1,2,3,4,5,6,7}; while (iTmpDay < DayNum()) { if (iTmpWeenday > 6)//如果大于周日则返回周一 { iTmpWeenday = 0; } Tmp = iTmpWeenday; ++iTmpWeenday; ++iTmpDay; } return Tmp; } void CDateTime::GetMonthFirstDayWeek(char * szTmp) { switch (WeenDay()) { case 0: strcpy(szTmp , "星期一"); break; case 1: strcpy(szTmp , "星期二"); break; case 2: strcpy(szTmp , "星期三"); break; case 3: strcpy(szTmp , "星期四"); break; case 4: strcpy(szTmp , "星期五"); break; case 5: strcpy(szTmp , "星期六"); break; case 6: strcpy(szTmp , "星期日"); break; } } void CDateTime::DayToDay(int iYear, int iMonth, int iDay, int iHour, int iMinute, int iSecond) { int iTmpDay = DayNum(iYear , iMonth , iDay) - 1;//求出间隔天数 if (m_iSecond < iSecond)//如果结束秒小于起始秒 { m_iSecond = 60 - iSecond + m_iSecond;//减去起始秒多出来的,然后再加结束秒 --m_iMinute;//结束分钟减一 } else { m_iSecond -= iSecond; } //-------------水平分割线------------------- if (m_iMinute < iMinute) { m_iMinute = 60 - iMinute + m_iMinute;//减去起始分多出来的,然后再加结束分 --m_iHour;//结束小时减一 } else { m_iMinute -= iMinute; } //-------------水平分割线------------------- if (m_iHour < iHour) { m_iHour = 24 - iHour + m_iHour;//减去起始时多出来的,然后再加结束时 --iTmpDay;//间隔天数减1 } else { m_iHour -= iHour; } cout << "您输入的这两个日期相差" << iTmpDay << "天" << m_iHour << "时" << m_iMinute << "分" << m_iSecond << "秒" << endl; } CDateTime CDateTime::GetNextDay() { int iMonth = GetMonthDays(); ++m_iDay; if (m_iDay > iMonth) { m_iDay = 1; } CDateTime objTmp; objTmp.m_iDay = m_iDay; return objTmp; } void CDateTime::GetShowToDay() { cout << "下一天是" << m_iDay<< endl; } void CDateTime::GetWeekDayInfo(int iWeenDay , char * pszInfo) { int iTmp = 0 , iTmpDay = 0; CDateTime cWeenDay; for (int i = 0; i < GetMonthDays(); ++i) { cWeenDay.SetDateTime(m_iYear,m_iMonth,i,0,0,0); if (cWeenDay.WeenDay() == 0)//如果等于周一,则第一个周开始 { ++iTmp; } if (iTmp==iWeenDay)//如果等于当前周 { *(pszInfo + iTmpDay) = char(i+48); ++iTmpDay; } } *(pszInfo + iTmpDay)='\0'; } void CDateTime::GetShowWeekDay(char * pszInfo) { for (int i = 0; i < strlen(pszInfo); ++i) { cout << *(pszInfo + i) << endl; } } int main() { int iYear , iMonth , iDay , iweenday; cout << "-------------------黄金分割线----------------------------" << endl; cout << "求某个月的第一天是星期几" << endl; cout << "请输入年:"; cin >> iYear; cout << "请输入月:"; cin >> iMonth; CDateTime Fist(iYear , iMonth , 1 , 0 , 0 , 0); char szTmp[10]; Fist.GetMonthFirstDayWeek(szTmp); cout << iYear << "年" << iMonth << "月" << "的第一天是:" << szTmp << endl; cout << "-------------------黄金分割线----------------------------" << endl; int iStarYear , iStarMonth , iStarDay , iStarHour , iStarMinute , iStarSecond; cout << "求两个时间相隔多少天" << endl; cout << "请输入起始年:"; cin >> iStarYear; cout << "请输入起始月:"; cin >> iStarMonth; cout << "请输入起始日:"; cin >> iStarDay; cout << "请输入起始时:"; cin >> iStarHour; cout << "请输入起始分:"; cin >> iStarMinute; cout << "请输入起始秒:"; cin >> iStarSecond; int iLastYear , iLastMonth , iLastDay , iLastHour , iLastMinute , iLastSecond; cout << "请输入结束年:"; cin >> iLastYear; cout << "请输入结束月:"; cin >> iLastMonth; cout << "请输入结束日:"; cin >> iLastDay; cout << "请输入结束时:"; cin >> iLastHour; cout << "请输入结束分:"; cin >> iLastMinute; cout << "请输入结束秒:"; cin >> iLastSecond; CDateTime Two(iLastYear , iLastMonth , iLastDay , iLastHour , iLastMinute , iLastSecond); Two.DayToDay(iStarYear , iStarMonth , iStarDay , iStarHour , iStarMinute , iStarSecond); cout << "-------------------黄金分割线----------------------------" << endl; cout << "求某天的下一天是几号" << endl; cout << "请输入年:"; cin >> iYear; cout << "请输入月:"; cin >> iMonth; cout << "请输入日:"; cin >> iDay; CDateTime Three(iYear , iMonth , iDay , 0 , 0 , 0); CDateTime objTmp; objTmp = Three.GetNextDay(); objTmp.GetShowToDay(); cout << "-------------------黄金分割线----------------------------" << endl; cout << "求某年某月的第几周是哪几天" << endl; cout << "请输入年:"; cin >> iYear; cout << "请输入月:"; cin >> iMonth; cout << "请输入第几周:"; cin >> iweenday; CDateTime Four(iYear , iMonth , 1 , 0 , 0 , 0); char szT[100]; Four.GetWeekDayInfo(iweenday , szT); Four.GetShowWeekDay(szT); return 0; }