首页 > 编程语言 > 详细

C++学习之路 : class练习

时间:2014-09-22 16:35:53      阅读:189      评论:0      收藏:0      [点我收藏+]

需求:

1.作业:写一个Date类

a)      数据成员:year/month/day   ok

b)     提供两个构造函数   ok

c)      实现功能setDate设置日期   ok

d)     把时间设定为今天 setToday();   ok

e)      toString()  打印格式:2009/7/31    ok

f)       toFormatString:对于月份采用英文 September 19 2014 ok

g)      isLeapYear() 判断是否是闰年  ok

h)     calDayOfYear() 计算该日期是当年的第几天(注意闰年)   ok

i)        写一个全局的函数int differenceDate(const Date &d1, const Date &d2);比较两个日期的差异(d1 - d2), 可以写为static函数 ok

j)        写一个static函数today(),返回代表今天的Date对象。

  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 #include <time.h>
  5 #include <stdlib.h>
  6 #include <stdio.h>
  7 
  8 #define ERR_EXIT(m)   9     do {  10         perror(m); 11         exit(EXIT_FAILURE); 12     }while(0)
 13 
 14 using namespace std;
 15 
 16 
 17 
 18 
 19 class Date
 20 {
 21     private :
 22         int year_;
 23         int month_ ;
 24         int day_;
 25     public :
 26         
 27 
 28             Date() :
 29                  year_(2014), month_(1), day_(1)  
 30              {}
 31         
 32              Date(int year, int month,  int day) :
 33                  year_(year), month_(month),  day_(day) 
 34 
 35              {
 36              }
 37 
 38         void setDate(int year, int  month,int day)
 39         {
 40             year_ = year ;
 41             month_ = month ;
 42             day_ = day ;
 43         } ;
 44         
 45         int getYear() const
 46         {
 47             return year_;
 48         }
 49         
 50         int getmonth() const
 51         {
 52             return month_ ;
 53         }
 54 
 55         int getDay() const
 56         {
 57             return day_ ;
 58         }
 59 
 60         int whatDay()
 61         {
 62             time_t t;
 63             t = time(NULL) ;
 64             struct tm* now = localtime(&t) ;
 65             int day = now->tm_yday;
 66 
 67             return day ;
 68         }
 69 
 70         void setToday()
 71         {
 72             time_t t;
 73             t = time(NULL);
 74             struct tm* now = localtime(&t) ;
 75             
 76             year_ = 1900 + now->tm_year;
 77             
 78             month_ = 1 + now->tm_mon ;
 79             day_ = now->tm_mday ;
 80 
 81            printf("local time is %s", asctime(now));
 82         }
 83         
 84         void toString()
 85         {
 86             cout << year_ << "/" << month_ << "/" << day_ << endl;
 87         } 
 88 
 89         void toFormatString()
 90         {
 91             switch (month_)
 92             {
 93                 case 1: cout << "January " ; break ;
 94                 case 2: cout << "February " ; break;
 95                 case 3: cout << "March " ; break;
 96                 case 4: cout << "April " ; break;
 97                 case 5: cout << "May " ; break;
 98                 case 6: cout << "June " ; break ;
 99                 case 7: cout << "July" ; break; 
100                 case 8: cout << "August" ; break ;
101                 case 9: cout << "September" ;break;
102                 case 10: cout << "October" ;break;
103                 case 11: cout << "November" ; break;
104                 case 12: cout << "December" ;break;
105                 default :  ERR_EXIT("月份输入错误") ;break;
106             }
107             cout << day_ << " " << year_ <<endl;
108 
109         }
110         bool isLeapYear() const 
111         {
112            return (year_ % 400 == 0) || (year_ % 4 == 0 && year_ % 100 != 0) ;
113         }
114 
115         int calDayOfYear() const
116         {
117             int sum = 0;
118             if(isLeapYear())
119             {
120                 int sign[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
121                 for(int i = 1; i < month_; i++)
122                 {
123                     cout << "进入闰年循环" << endl;
124                     sum += sign[i] ;
125                 }
126 
127             }else
128             {
129                 int sign[13] = {0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
130                 for(int i = 1; i < month_; i++)
131                 {
132                     cout << "进入非闰年循环" << endl;
133                     sum += sign[i] ;
134                 }
135                 
136             }
137             sum += day_ ;
138             
139             return sum ;
140         }
141 } ;
142 
143 
144 bool isLeapYear(int year)
145 {
146     return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ;
147 }
148 
149 static int differenceDate(const Date &d1, const Date &d2)
150 {
151     int temp(0);
152     int sum(0) ;
153     int year1 = d1.getYear() ;
154     int year2 = d2.getYear() ;
155 
156     if( year1 > year2)
157     {
158         for (int i = year2; i < year1; i++) {
159              if(isLeapYear(i))
160              {
161                 sum += 366 ;
162              }else
163              {
164                 sum += 365 ;
165              }
166         }
167         sum += d2.calDayOfYear() ;
168         sum -= d1.calDayOfYear() ;
169 
170     }else if(year1 == year2)
171     {
172         sum = d1.calDayOfYear() - d2.calDayOfYear() ;
173         if(sum < 0)
174         {
175             sum = -sum;
176         }
177     }else
178     {
179         for (int i = year1; i < year2; i++) {
180             if(isLeapYear(i))
181             {
182                 sum += 366;
183             }else
184             {
185                 sum += 365;
186             }
187             sum += d2.calDayOfYear() ;
188             sum -= d1.calDayOfYear() ;
189         }
190     }
191 
192     return sum ;
193 }
194 
195 static Date toDay(Date &kong)
196 {
197     kong.setToday() ;
198     return kong;
199 }
200 
201 int main(int argc, const char *argv[])
202 {
203     Date test;    
204     //test.setToday() ;
205     cout << "设置前-----------------" << endl;
206     test.toString() ;
207     test.setDate(2014, 9, 20) ;
208     cout << "设置后-----------------" << endl;
209     test.toString() ;
210     cout << "toFormatString" << endl;
211     test.toFormatString() ;
212     cout << "isLeapYear" << endl;
213     if(test.isLeapYear()) 
214         cout << "是闰年" << endl ;
215     else
216         cout << "不是闰年" << endl;
217     cout << "calDayOfYear " << endl;
218     int num = test.calDayOfYear() ;
219     cout << ""<< num << "" <<endl;
220     
221     cout<< "----whatDay()----" << endl;
222     int day = test.whatDay() ;
223     cout << day << endl;
224     
225     Date d1(2002, 1, 1) ;
226     Date d2(2001, 1, 1) ;
227     int dif;
228     dif = differenceDate(d1, d2) ;
229     
230     cout << "-----differenceDate--------" << endl ;
231     cout << dif << endl;
232     return 0;
233 }

 

缺少一个 判断函数检验  输入的年月日是否非法。   其他功能均已实现

C++学习之路 : class练习

原文:http://www.cnblogs.com/DLzhang/p/3986155.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!