代码改变世界

期中考试2

2019-04-30 15:13  孙乾(小U)  阅读(123)  评论(0编辑  收藏  举报
#include<iostream>
using namespace std;
class
Date { public: Date(int year=1970,int month=1,int day=1); // 默认构造函数,将日期初始化为1970年1月1日 Date(const Date& d); void display(); // 显示日期 int getMonth(int year,int month) const; // 返回日期中的年,月,日 int dayOfYear(); // 返回这是一年中的第多少天 private: int _year; int _month; int _day; }; Date::Date(int y,int m,int d){ if(_year>=1970&&_month>0&&_month<13&&_day>0&&_day<getMonth(year,month)) { this->year=year; this->month=month; this->day=day; } } int Date::getMonth(int year,int month){ int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31} if(month==2&&year%400==0||(year%4==0&&year%100!=0)){ ++a[2]; } return a[month] } votd Date::display(){ cout<<_year<<"-"<<_month<<"-"<<_day<<endl; } Date::Date(const Date& d) { _year=d._year; _month=d._month; _day=d._day; } int Date::dayOfYear() { int total=0; int db[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}}; cin>>_year;cout<<"-";cin>>_month;cout<<"-";cin>>_day; if(year%400==0||(year%4==0&&year%100!=0) { for(int i=0;i<m-1;i++){ total+=db[1][i]; } } else { for(int i=0;i<m-1;i++) { total+=db[0][i]; } } total+=d; cout<<_year<<"-"<<_month<<"-"<<_day<<endl; return 0; } int main() { Date(1970,1,1); Date(2019,4,30); Date(2019,5,1); return 0; }