期中测试

1. 补足日期类实现,使得能够根据日期获取这是一年中第多少天。

 1 #include "date.h"
 2 #include "utils.h" 
 3 #include <iostream>
 4 using std::cout;
 5 using std::endl;
 6 
 7 Date::Date(){
 8     year=1970;
 9     month=1;
10     day=1;
11 }
12 Date::Date(int y, int m, int d):year(y),month(m),day(d){
13 }
14 
15 void Date::display(){
16 cout<<year<<"-"<<month<<"-"<<day<<endl;
17 }
18 
19 int Date::getYear() const{
20 return year;
21 }
22 
23 int Date::getMonth() const{
24 return month;
25 }
26 
27 int Date::getDay() const{
28 return day;
29 }
30 
31 int Date::dayOfYear(){
32     int i,j,sum=0;
33     if(month>=1) {
34     for(i=1;i<month;i++){
35     if(month==3||month==5||month==7||month==8||month==10||month==12)
36         sum=sum+30;
37     else if(month==2)
38          {
39                 if(((year%4==0&&year%100!=0)||year%400==0))
40                 sum=sum+29;
41                 else
42                 sum=sum+28;
43             }
44     else sum=sum+31;}}
45     sum=sum+day;
46     return sum;
47 }

 

posted @ 2019-04-30 15:13  糕点点  阅读(114)  评论(0编辑  收藏  举报