两个对象之间的日期比较

问题描述:

我不得不为我的大学实验室编写一个程序。在该程序中,我想比较格式为日/月/年的两个日期。我知道如何做到这一点,但不包括小时。现在我将该日期转换为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; 
} 

还有就是我试图重载> =运营​​商之一。请帮忙。

+0

小时会增加小数天数,所以如果您想保留一个类似的算法,您仍然不能返回'long int'。 – crashmstr 2015-01-20 18:54:27

+0

http://*.com/questions/4196153/find-how-many-seconds-past-since-1-1-1970 – 2015-01-20 18:57:16

count在你的算法是指自0年

虽然,你应该现在已经不是一天最小精度过去的天数,而是一个小时。所以,你应该简单地创建一个变量totalHours正的小时数量自今年通过0

//Calculate number of days since year 0 
count = year * 365 + month_days[month-1] + day + leapyears; 
//Convert to number of HOURS since year 0, and add additional hour 
totalHours = count*24 + hour; 
+0

感谢您的回复。这很有帮助,但现在我还有其他一些问题。我想创建一个反向工作的函数,这意味着从“从0年过去的小时”获得日/月/年和小时。现在我正在努力争取一年,但不知怎的,它不能像它应该那样工作,例如将日期1/1/2016小时1转换为“小时...”,然后返回给我2017年。它是因为使用int值? 'year =((count-leapyears)/ 24)/ 365;' – Bringer 2015-01-20 20:20:29

+0

如果我说得对,现在想做的是从一个单一的组件(从0年开始的小时数),得到年数,几个月,几天还有剩余时间? 这比你到目前为止所做的还要复杂 – Angivare 2015-01-20 20:29:10

+0

我使用这种格式的主要目的是将日期添加/移动x天更简单(至少我认为是这样)。但正如你所说,将其转换回来非常困难。 – Bringer 2015-01-20 20:33:56

countobj.countoperator >=之间的3个可能的关系。 count < obj.countcount == obj.countcount > obj.count。这同样适用于hoursobj.hours。这给出了3 * 3 = 9种可能的组合。写下每个组合的运算符结果,然后找到在代码中表达它的最简单方法。

请注意,您不需要为每个比较运算符执行此操作。通常你执行operator <然后根据那个定义其他的。