首页 > 学院 > 开发设计 > 正文

日期类

2019-11-10 18:17:37
字体:
来源:转载
供稿:网友
#include<Windows.h>#include<iostream>typedef unsigned int uint;using namespace std;class Date{PRivate: uint DaysInThisYear; uint DaysInThisMonth; uint Year; uint Month; uint Day; ~Date();//防止对象在栈中初始化,在栈中初始化会导致变量无法销毁。 void DataCopy(Date& CopySource);public: Date(uint year, uint month, uint day); void OutPutDate()const; friend int GetDaysOfAMonth(int year, int month); friend int GetDaysOfAYear(int year); uint GetDaysInThisMonth()const; uint GetDaysInThisYear()const; void ChangeDay(uint day); void ChangeMonth(uint month); void ChangeYear(uint year);};/*私有函数列表*/void Date::DataCopy(Date& CopySource){ DaysInThisMonth = CopySource.DaysInThisMonth; DaysInThisYear = CopySource.DaysInThisYear; Year = CopySource.Year; Month = CopySource.Month; Day = CopySource.Day;}/*公有函数列表*/Date::Date(uint year, uint month, uint day){ Year = year; DaysInThisYear = GetDaysOfAYear(year); if (month > 0 && month < 13) { Month = month; } DaysInThisMonth = GetDaysOfAMonth(year,month); if (day > DaysInThisMonth) { abort(); } Day = day;}void Date::OutPutDate()const{ cout << Year << "年" << Month << "月" << Day << "日";}int GetDaysOfAMonth(int year,int month) { { switch (month) { case 2: { if (year == 366) { return 29; } else { return 28; } break; } case 1: case 3: case 5: case 7: case 8: case 10: case 12:return 31; break; default: return 30; } }}int GetDaysOfAYear(int year){ if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { return 366; } else { return 365; }}uint Date::GetDaysInThisMonth()const{ return DaysInThisMonth;}uint Date::GetDaysInThisYear()const{ return DaysInThisYear;}void Date::ChangeDay(uint day){ if (day < DaysInThisMonth && day>0) { Day = day; } else { abort(); }}void Date::ChangeMonth(uint month){ if (month > 0 && month < 13) { Month = month; } else { abort(); }}void Date::ChangeYear(uint year){ if (year > 0) { Year = year; } else { abort(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表