考试
1.
date.cpp
#include "date.h" #include "utils.h" #include <iostream> using std::cout; using std::endl; // 补足程序,实现Date类中定义的成员函数 Date::Date(){ year=1970; month=1; day=1; } Date::Date(int y,int m,int d):year(y),month(m),day(d){} void Date::display(){ cout<<year<<"-"<<month<<"-"<<day<<endl; } int Date::getYear() const{ return year; } int Date::getMonth()const{ return month; } int Date::getDay()const{ return day; } int Date::dayOfYear(){ while(isLeap(year)) { switch(month) { case 1:return day;break; case 2:return day+31;break; case 3:return day+60;break; case 4:return day+91;break; case 5:return day+121;break; case 6:return day+152;break; case 7:return day+182;break; case 8:return day+213;break; case 9:return day+244;break; case 10:return day+274;break; case 11:return day+305;break; case 12:return day+335;break; } } while(!(isLeap(year))) { switch(month) { case 1:return day;break; case 2:return day+31;break; case 3:return day+59;break; case 4:return day+90;break; case 5:return day+120;break; case 6:return day+151;break; case 7:return day+181;break; case 8:return day+212;break; case 9:return day+243;break; case 10:return day+273;break; case 11:return day+304;break; case 12:return day+334;break; } } }
utils.h
#include <date.h> #include <string> #include <iostream> using namespace std; // 工具包头文件,用于存放函数声明 // 函数声明 bool isLeap(int);
utils.cpp
#include "date.h" #include "string" #include <iostream> using namespace std;// 功能描述: // 判断year是否是闰年, 如果是,返回true; 否则,返回false bool isLeap(int year) { if( (year % 4 == 0 && year % 100 !=0) || (year % 400 == 0) ) return true; else return false; }