main.cpp主函数代码:
#include <iostream>
#include "Time.h"
using namespace std;
void TimeTest();
int main()
{
TimeTest();
}
void TimeTest()
{
Time time0;
int year1,year2,month1,month2,day1,day2;
cout << "Please input year month day :";
cin >> year1 >> month1 >> day1;
Time time1(year1,month1,day1);
cout << "Please input other year month day :";
cin >> year2 >> month2 >> day2;
Time time2(year2,month2,day2);
time1.Day(time2);
}
Time.h类头文件代码:
#ifndef TIME_H
#define TIME_H
#include <iostream>
using namespace std;
class Time
{
public:
Time();
Time(int year,int month,int day);
virtual ~Time();
void Day(Time & time2);
int GetYear(){return m_year;}
int GetMonth(){return m_month;}
int GetDay(){return m_day;}
void SetYear(int year){m_year = year;}
void SetMonth(int month){m_month = month;}
void SetDay(int day){m_day = day;}
protected:
private:
int m_year;
int m_month;
int m_day;
};
#endif // TIME_H
Time.cpp
#include “Time.h”
#include
#include <math.h>
using namespace std;
Time::Time()
{
SetYear(0);
SetMonth(1);
SetDay(1);
cout << "默认Time(year.month.day):" << "公元" << GetYear() << "."
<< GetMonth() << "." << GetDay() << endl;
}
Time::Time(int year,int month,int day)
{
SetYear(year);
SetMonth(month);
SetDay(day);
cout << "Time(year.month.day):" <<"公元" << GetYear() <<"."
<< GetMonth() <<"." << GetDay() << endl;
}
void Time::Day(Time& time2)
{
long day = 0;
long day1 = 0;//到本月月底的天数
long day2 = 0;//到年底的天数
long day3 = 0;//两个年份之间的天数
int month1[12] = {3,5,7,8,10,12};
int month2[12] = {4,6,9,11};
int runyear= 0;
int pingyear = 0;
int runmonth= 0;
int pingmonth= 0;
if(GetYear() < time2.GetYear()) //当比较的年份大时
{
//GetYear()为闰年,判断月份,算出到年底的天数
if(GetYear() % 400 == 0 ||( GetYear() % 4 == 0 && GetYear() % 100 != 0))
{
if(GetMonth() == 1)
{
day2 = 366 -GetDay(); // 1月份到年底的天数等于366 - day
cout << "1月份到年底的天数" << day2 << endl;
}
else
{
if(GetMonth() == 2)
{
day1 = 29 - GetDay();
}
else
{
for(int i = 0; i < (sizeof(month1) / sizeof(int)); i++)
{
if(GetMonth() == month1[i])
{
day1 = 31 - GetDay();
break;
}
else
day1 = 30 - GetDay();
}
}
for(int i = GetMonth() + 1; i < 12 + 1; i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); j++)
{
if( i == month1[j])
{
runmonth++;
break;
}
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
if(i == month2[k])
{
pingmonth++;
break;
}
}
}
day2 = day1 + (runmonth * 31) + (pingmonth * 30);
}
for(int i = GetYear() + 1; i < time2.GetYear(); i++)
{
if(i % 400 == 0 ||(i % 4 == 0 && i % 100 != 0) )
{
runyear++;
}
else
pingyear++;
}
day3 = (runyear * 366) + (pingyear * 365);
//计算大的年已经过去的天数
long day4 = 0;
if(time2.GetMonth() == 1)
{
day4 = time2.GetDay();
}
else
{
if(time2.GetMonth() == 2)
{
day4 = 31 + time2.GetDay();
}
else
{
for(int i = 3; i < time2.GetMonth(); i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); i++)
{
runmonth++;
break;
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
pingmonth++;
break;
}
}
day4 = runmonth * 31 + pingmonth * 30 + time2.GetDay();
}
}
day = day2 + day3 + day4;
cout << "相差 <" << day << "> 天!" << endl;
}
else//GetYear()为平年,判断月份,算出到年底的天数
{
if(GetMonth() == 1)
{
day2 = 365 -GetDay(); // 1月份到年底的天数等于366 - day
cout << "1月份到年底的天数" << day2 << endl;
}
else
{
if(GetMonth() == 2)
{
day1 = 28 - GetDay();
}
else
{
for(int i = 0; i < (sizeof(month1) / sizeof(int)); i++)
{
if(GetMonth() == month1[i])
{
day1 = 31 - GetDay();
break;
}
else
day1 = 30 - GetDay();
}
}
for(int i = GetMonth() + 1; i < 12 + 1; i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); j++)
{
if( i == month1[j])
{
runmonth++;
break;
}
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
if(i == month2[k])
{
pingmonth++;
break;
}
}
}
day2 = day1 + (runmonth * 31) + (pingmonth * 30);
}
for(int i = GetYear() + 1; i < time2.GetYear(); i++)
{
if(i % 400 == 0 ||(i % 4 == 0 && i % 100 != 0) )
{
runyear++;
}
else
pingyear++;
}
day3 = (runyear * 366) + (pingyear * 365);
//计算大的年已经过去的天数
long day4 = 0;
if(time2.GetMonth() == 1)
{
day4 = time2.GetDay();
}
else
{
if(time2.GetMonth() == 2)
{
day4 = 31 + time2.GetDay();
}
else
{
for(int i = 3; i < time2.GetMonth(); i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); i++)
{
runmonth++;
break;
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
pingmonth++;
break;
}
}
day4 = runmonth * 31 + pingmonth * 30 + time2.GetDay();
}
}
day = day2 + day3 + day4;
cout << "相差 <" << day << "> 天!" << endl;
}
}
//******年份相等时*****
else if(GetYear() == time2.GetYear())
{
long day5 = 0,day6 = 0;
if(GetMonth() < time2.GetMonth())//月份较小
{
for(int i = 3; i < time2.GetMonth(); i++)
{
for(int j = 0; j < sizeof(month1) / sizeof(int); j++)
{
if(i == month1[j])
runmonth++;
break;
}
for(int k = 0; k < sizeof(month2) / sizeof(int); k++)
{
if(i == month2[k])
pingmonth++;
break;
}
}
day5 = 31 * runmonth + pingmonth * 30;
if(GetMonth() == 1)
{
day1 = 31 - GetDay();
}
else
{
if(GetMonth() == 2)
{
if(GetYear() % 400 == 0 ||( GetYear() % 4 == 0 & GetYear() % 400 != 0))
{
day1 = 29 -GetDay();
}
else
{
day1 = 28 - GetDay();
}
}
else
{
for(int i = 3; i < time2.GetMonth(); i++)
{
for(int j = 0; j < sizeof(month1) / sizeof(int); j++)
{
if(i == month1[j])
day1 = 31 - GetDay();
break;
}
for(int k = 0; k < sizeof(month2) / sizeof(int); k++)
{
if(i == month2[k])
day1 = 30 - GetDay();
break;
}
}
}
}
day6 = day5 + day1 + time2.GetDay();
cout << "相差 <" << day6 << "> 天!" << endl;
}
else if(GetMonth() == time2.GetMonth())//月份相同
{
day1 = abs(GetDay() - time2.GetDay());
cout << "相差 <" << day1 << "> 天" <<endl;
}
else
{
for(int i = 3; i < GetMonth(); i++)
{
for(int j = 0; j < sizeof(month1) / sizeof(int); j++)
{
if(i == month1[j])
runmonth++;
break;
}
for(int k = 0; k < sizeof(month2) / sizeof(int); k++)
{
if(i == month2[k])
pingmonth++;
break;
}
}
day5 = 31 * runmonth + pingmonth * 30;
if(time2.GetMonth() == 1)
{
day1 = 31 - GetDay();
}
else
{
if(time2.GetMonth() == 2)
{
if(GetYear() % 400 == 0 ||( GetYear() % 4 == 0 & GetYear() % 400 != 0))
{
day1 = 29 - time2.GetDay()
;
}
else
{
day1 = 28 - time2.GetDay();
}
}
else
{
for(int i = 3; i < GetMonth(); i++)
{
for(int j = 0; j < sizeof(month1) / sizeof(int); j++)
{
if(i == month1[j])
day1 = 31 - time2.GetDay();
break;
}
for(int k = 0; k < sizeof(month2) / sizeof(int); k++)
{
if(i == month2[k])
day1 = 30 - time2.GetDay();
break;
}
}
}
}
day6 = day5 + day1 + GetDay();
cout << "相差 <" << day6 << "> 天!" << endl;
}
}
//当比较的年份小时
else
{
if(time2.GetYear() % 400 == 0 ||(time2.GetYear() % 4 == 0 && time2.GetYear() % 100 != 0))
{
//GetYear()为闰年,判断月份,算出到年底的天数
if(time2.GetMonth() == 1)
{
day2 = 366 - time2.GetDay(); // 1月份到年底的天数等于366 - day
}
else
{
if(time2.GetMonth() == 2)
{
day1 = 29 - time2.GetDay();
}
else
{
for(int i = 0; i < (sizeof(month1) / sizeof(int)); i++)
{
if(time2.GetMonth() == month1[i])
{
day1 = 31 - time2.GetDay();
break;
}
else
day1 = 30 - time2.GetDay();
}
}
for(int i = time2.GetMonth() + 1; i < 12 + 1; i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); j++)
{
if( i == month1[j])
{
runmonth++;
break;
}
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
if(i == month2[k])
{
pingmonth++;
break;
}
}
}
day2 = day1 + (runmonth * 31) + (pingmonth * 30);
}
for(int i = time2.GetYear() + 1; i < GetYear(); i++)
{
if(i % 400 == 0 ||(i % 4 == 0 && i % 100 != 0) )
{
runyear++;
}
else
pingyear++;
}
day3 = (runyear * 366) + (pingyear * 365);
//计算大的年已经过去的天数
long day4 = 0;
if(GetMonth() == 1)
{
day4 = GetDay();
}
else
{
if(GetMonth() == 2)
{
day4 = 31 + GetDay();
}
else
{
for(int i = 3; i < GetMonth(); i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); i++)
{
runmonth++;
break;
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
pingmonth++;
break;
}
}
day4 = runmonth * 31 + pingmonth * 30 + time2.GetDay();
}
}
day = day2 + day3 + day4;
cout << "相差 <" << day << "> 天!" << endl;
}
else //为平年算出天数
{
//GetYear()为闰年,判断月份,算出到年底的天数
if(time2.GetMonth() == 1)
{
day2 = 365 - time2.GetDay(); // 1月份到年底的天数等于366 - day
}
else
{
if(time2.GetMonth() == 2)
{
day1 = 28 - time2.GetDay();
}
else
{
for(int i = 0; i < (sizeof(month1) / sizeof(int)); i++)
{
if(time2.GetMonth() == month1[i])
{
day1 = 31 - time2.GetDay();
break;
}
else
day1 = 30 - time2.GetDay();
}
}
for(int i = time2.GetMonth() + 1; i < 12 + 1; i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); j++)
{
if( i == month1[j])
{
runmonth++;
break;
}
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
if(i == month2[k])
{
pingmonth++;
break;
}
}
}
day2 = day1 + (runmonth * 31) + (pingmonth * 30);
}
for(int i = time2.GetYear() + 1; i < GetYear(); i++)
{
if(i % 400 == 0 ||(i % 4 == 0 && i % 100 != 0) )
{
runyear++;
}
else
pingyear++;
}
day3 = (runyear * 366) + (pingyear * 365);
//计算大的年已经过去的天数
long day4;
if(GetMonth() == 1)
{
day4 = GetDay();
}
else
{
if(GetMonth() == 2)
{
day4 = 31 + GetDay();
}
else
{
for(int i = 3; i < GetMonth(); i++)
{
for(int j = 0; j < (sizeof(month1) / sizeof(int)); i++)
{
runmonth++;
break;
}
for(int k = 0; k < (sizeof(month2) / sizeof(int)); k++)
{
pingmonth++;
break;
}
}
day4 = runmonth * 31 + pingmonth * 30 + time2.GetDay();
}
}
day = day2 + day3 + day4;
cout << "相差 <" << day << "> 天!" << endl;
}
}
}
Time::~Time()
{
//dtor
}