我不得不爲我的大學實驗室編寫一個程序。在該程序中,我想比較格式爲日/月/年的兩個日期。我知道如何做到這一點,但不包括小時。現在我將該日期轉換爲0000年以來的日期,並簡單地比較這兩個值。問題是我的老師告訴我要添加幾小時,現在我不知道如何比較這一點。有什麼建議麼?本次代碼波紋管兩個對象之間的日期比較
.h文件中
class timee
{
int day;
int month;
int year;
int hour;
long int count;
public:
timee();
timee(int,int,int,int);
long int daysCount();
bool operator>(const timee &);
bool operator>=(const timee &);
bool operator<=(const timee &);
bool operator==(const timee &);
timee & operator=(const timee &);
timee & operator+=(int);
timee & operator-=(int);
long int operator-(timee &);
friend ostream & operator<<(ostream &, const timee &);
friend istream & operator>>(istream &, timee &);
};
這裏.cpp文件
timee::timee():day(0),month(0),year(0),hour(0),count(0){}
timee::timee(int day,int month,int year,int hour):day(day),month(month),year(year),hour(hour)
{
count = daysCount();
}
/*calculating the number of days that have passed since year 0000*/
long int timee::daysCount()
{
int month_days[] = {0,31,59,90,120,151,181,212,243,273,304,334};
// calculate number of leap years.
int leapyears = year/4;
if (isLeapYear(year) && month < 3)
{
// If this is a leap year
// And we have not passed Feburary then it does
// not count.....
leapyears --;
}
// convert year/month/day into a day count
count = year * 365 + month_days[month-1] + day + leapyears;
return count;
}
/*convering the date from days since year 0000 to year/month/day format */
timee timee::dateConversion()
{
int month_days[] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
//calculate number of leap year
int leapyears = year/4;
if (isLeapYear(year) && month < 3)
{
// If this is a leap year
// And we have not passed Feburary then it does
// not count.....
leapyears --;
}
//calculating year
year = (count-leapyears)/365;
for(unsigned int i = 0; i <= 12; i++)
{
if((count-leapyears)%365 > month_days[i])
{
month = i+1;
}
}
day = ((count-leapyears)%365)-month_days[month-1];
return *this;
}
bool timee::operator>(const timee &obj)
{
return count>obj.count;
}
bool timee::operator>=(const timee &obj)
{
//if((count>=obj.count) && (hour>=obj.hour)) return true;
//else if((count<=obj.count) && (hour>obj.hour))return false;
}
bool timee::operator<=(const timee &obj)
{
return count<=obj.count;
}
bool timee::operator==(const timee &obj)
{
return count==obj.count;
}
timee & timee::operator=(const timee &obj)
{
day=obj.day;
month=obj.month;
year=obj.year;
hour=obj.hour;
count=obj.count;
return *this;
}
timee & timee::operator+=(int value)
{
count+=value;
this->dateConversion();
return *this;
}
timee & timee::operator-=(int value)
{
count-=value;
this->dateConversion();
return *this;
}
long int timee::operator-(timee &obj)
{
return count - obj.count;
}
ostream & operator<<(ostream &os, const timee &obj)
{
os << "Date: " << obj.day << "." << obj.month << "." << obj.year << " Hour: " << obj.hour << " " << obj.count << endl;
return os;
}
istream & operator>>(istream &is, timee &obj)
{
cout << "Type day, month and year" << endl;
is >> obj.day >> obj.month >> obj.year >> obj.hour;
obj.daysCount();
return is;
}
還有就是我試圖重載> =運營商之一。請幫忙。
小時會增加小數天數,所以如果您想保留一個類似的算法,您仍然不能返回'long int'。 – crashmstr 2015-01-20 18:54:27
http://stackoverflow.com/questions/4196153/find-how-many-seconds-past-since-1-1-1970 – 2015-01-20 18:57:16