两个对象之间的日期比较

Date comparison between two objects

我在大学时不得不为我的实验室编写一个程序。在程序中,我想比较 day/month/year 格式的两个日期。我知道该怎么做,但在不包括 hour.For 的情况下,我现在将日期转换为自 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;
}

我曾尝试重载 >= 运算符。请帮忙。

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;

operator >=countobj.count之间有3种可能的关系。 count < obj.countcount == obj.countcount > obj.counthoursobj.hours 也是如此。这给出了 3 * 3 = 9 种可能的组合。写下每个组合的运算符结果应该是什么,然后找到最简单的方式在您的代码中表达它。

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