1 //Date.cpp
  2 #include<iostream>
  3 #include<time.h>
  4 #include<cmath>
  5 #include<cstring>
  6 using namespace std;
  7 //Date_class
  8 class Date
  9 {
 10 private:
 11     int year, month, day;    
 12     int week;            //存放星期 
 13     void IncDay();       //日期增加一天函数 
 14     int distance();        //设成私有成员,由公有函数调用,距基准日期的天数,设基准日期为1900-1-1---B
 15     int Week() {         //计算星期
 16         return week;
 17     }           
 18 public:
 19     Date(int y = 1900,int m = 1,int d = 1);   //构造函数(default)
 20     void SetSystemDate();         //取得系统日期 
 21     void SetDate(int yy,int mm,int dd);       //日期设置
 22 
 23     bool IsLeapYear();                        //是否闰年
 24 
 25     void print_ymd() const;                   //年月日输出法 
 26     void print_mdy() const;                    //月日年输出法
 27 
 28     void AddDay(int days);                    //A:增加任意天数
 29     bool IsEndofMonth() ;                     //判断是否到了月末--A
 30     int Daysof2Date(Date ymd) ;               //B:2个日期之间的天数---区别查表法 
 31 
 32     void ShowWeek(ostream &out);              //显示星期 
 33     int operator - (Date ymd);                //重载 —运算符 
 34     friend ostream& operator << (ostream &out, Date& ymd);   //输出日期 
 35 };
 36 
 37 Date::Date(int y,int m,int d){
 38     SetDate(y,m,d);
 39 } 
 40 
 41 void Date::SetSystemDate()         //取得系统日期 
 42 {
 43     tm *gm;                     //tm 是时间(含年月日分秒的结构体) 
 44     time_t t = time(NULL);      //time_t 是长整型,,,t是总秒数;
 45     gm = gmtime(&t);            //函数返回给定的统一世界时间(通常是格林威治时间),如果系统不支持统一世界时间系统返回NULL 
 46     year = 1900 + gm->tm_year;     //现在的年月日分秒 
 47     month = gm->tm_mon + 1;
 48     day = gm->tm_mday;
 49 }
 50 
 51 void Date::SetDate(int yy,int mm,int dd) {    //附带合法性检查,错误返回系统当前日期 
 52     //进行日期合法性检查 
 53     if (yy >= 1900 && yy <= 9999)
 54         year = yy;
 55     else {
 56         cout << "年设置错误:1900~9999之间!(默认设置成系统当前日期)" << endl;
 57         SetSystemDate();
 58         return ;
 59     }
 60     if (mm >= 1 && mm <= 12)
 61         month = mm;
 62     else {
 63         cout << "月设置错误:1900~9999之间!(默认设置成系统当前日期)" << endl;
 64         SetSystemDate();
 65         return;
 66     }
 67     
 68     switch(month)
 69     {
 70         //小月天数
 71         case 4:
 72         case 6:
 73         case 9:
 74         case 11: 
 75         if (dd >= 1 && dd <= 30)
 76             day = dd;
 77         else {
 78                 cout << "日设置错误:1900~9999之间!(默认设置成系统当前日期)" << endl;
 79                 SetSystemDate();
 80                 return ;
 81             }
 82         case 2:  //2月
 83             if ( (IsLeapYear() && dd >= 1 && dd <= 29) || (!IsLeapYear() && dd >= 1 && dd <= 28) )
 84                 day = dd;
 85             else {
 86                 cout << "日设置错误:1900~9999之间!(默认设置成系统当前日期)" << endl;
 87                 SetSystemDate();
 88                 return ;
 89             }
 90         default : //大月
 91             if (dd >= 1 && dd <= 31)
 92                 day = dd;
 93             else {
 94                 cout << "年设置错误:1900~9999之间!(默认设置成系统当前日期)" << endl;
 95                 SetSystemDate();
 96                 return ;
 97             }
 98     }
 99 }
100 
101 bool Date::IsLeapYear()
102 {
103     return (year%4 == 0 && year%100 != 0) || (year%400 == 0);
104 }
105 
106 void Date::print_ymd() const   //年月日输出法
107 {
108     cout << year << "-" << month << "-" << day; 
109 }
110 
111 void Date::print_mdy() const
112 {
113     const char *MonthName[12] = {"Jau","Feb","Mar","Apri","May","June","July","Aug","Sept",
114                                 "Oct","Nov","Dec"};
115     cout << MonthName[month-1] << " " << day << "," << year ; 
116 }
117 
118 void Date::IncDay()             //增加一天
119 {
120     if(IsEndofMonth())         //增加天数,记得要判断是否是年末,月末 
121     {
122         if(month == 12)
123         {
124             day = 1; month = 1; year++;
125         }
126         else               //已经是IsEndMonth 
127             day = 1; month++;
128     } 
129     else            
130         day++; 
131 }
132 
133 bool Date::IsEndofMonth()
134 {
135     switch(month)
136     {
137         case 4:
138         case 6:
139         case 9:
140         case 11: return day == 30;
141         case 2:
142             if (IsLeapYear())
143                 return day == 29;
144             else
145                 return day == 28;
146         default:
147             return day == 31;
148     }
149 }
150 void Date::AddDay(int days)
151 {
152     for (int i = 0; i <= days; i++)    //增加多少天--day1->day2 = day+1 
153     {
154         IncDay(); 
155     }
156 }
157 
158 /*int years = year - 1;    //相隔多少年,,用其算的才是前几年应该有的天数
159     totalDays = years*365 + years/4 - years/100 + years/400 //因为DAY...是按闰年算的年份包含闰年的已经算好
160         + DAYS_BEFORE_MONTH[month-1] + day;
161     if (IsLeapYear() && month > 2) totalDays++;   
162 //(闰年)当大于2月时,天数要加1;(注:该法查表代码简洁,但是需要记住表)*/ 
163 
164 int Date::distance()       //距离基准日期的天数, 先算到指定年的天数+一月到指定月份天数+指定天数 
165 {                         
166     int days;
167     int years = year-1900;
168     days = years*365 + years/4 - years/100 + years/400;      //不计闰年的天数
169 //    if(years)                  // 
170 //        days += (years-1)/4;   //逢闰年+1天 
171     switch(month)              //此处方法不错,从12月份算起,输入多少月份就能加到哪个月 
172     {                           //注意,无break; 
173         case 12: days = days + 30;
174         case 11: days = days + 31;
175         case 10: days = days + 30;
176         case 9: days = days + 31;
177         case 8: days = days + 31;
178         case 7: days = days + 30;
179         case 6: days = days + 31;
180         case 5: days = days + 30;
181         case 4: days = days + 31;
182         case 3: 
183             if (IsLeapYear())
184                 days = days + 29;
185             else
186                 days = days + 28;
187         case 2: days = days + 31;
188         default : break; 
189     } 
190     days = days + day;         //多少天只要加多少天就可以了,(年月已经知道过了多少天)
191     return days;
192 }
193 
194 int Date::Daysof2Date(Date ymd) //两个日期的天数 
195 {
196     int days;
197     days = abs(distance()-ymd.distance()); //distance()返回值是指定日期到基准类的天数(本类日期-指定日期) 
198     return days;               //2个日期之间的天数 
199 }
200 
201 void Date::ShowWeek(ostream &out)  //显示该日期是星期几 
202 {
203     week = distance() % 7;
204     const char w[7][3] = {"", "", "", "", "", "", ""};
205     out << year << "-" << month << "-" << day << "--星期" << w[week] << endl; 
206 }
207 
208 int Date::operator - (Date ymd)
209 {
210     return Daysof2Date(ymd);
211 }
212 
213 ostream& operator << (ostream& out, Date& ymd)
214 {
215     ymd.ShowWeek(out);
216     return out;
217 }
218 
219 int main(void)
220 {
221     Date date1, date2;
222     int year, month, day; 
223     cout << "please input year, month, day: " << endl;
224     cin >> year >> month >> day;
225 //    date1.SetDate(2013,1,27);
226 //    cout << "the current date is : " << endl;
227 //    date1.print_ymd();
228 //    date1.print_mdy();
229 //    date1.AddDay(100);
230 //    cout << "After 100 days , the date is :";
231 //    date1.print_ymd();
232     date1.SetDate(year, month, day);
233     cout << date1;        
234     
235     cout << "please input the date(year,month,day) in the future : " << endl;
236     cin >> year >> month >> day;
237     date2.SetDate(year,month,day);
238     date2.print_mdy();
239     cout << " 距离 :";
240     date1.print_mdy();
241     cout << " have "<< date1-date2 << " days" << endl;     
242     
243     cout << "输入(想要经历的)天数(将显示当前日期后(N天)的日期: " << endl;
244     cin >> day;
245     date1.print_ymd();
246     date1.AddDay(day);
247     cout << ", After " << day << " days, the date is : " << date1;
248     
249     return 0; 
250 }

 

版权声明:本文为douzujun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/douzujun/p/5540105.html