两个对象之间的日期比较

Date comparison between two objects

本文关键字:之间 日期 比较 对象 两个      更新时间:2023-10-16

我不得不为我的大学实验室编写一个程序。在程序中,我想以日/月/年格式比较两个日期。我知道该怎么做,但不包括小时。现在,我将日期转换为自 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.count,要么count == obj.count要么count > obj.count.hoursobj.hours也是如此。这给出了 3 * 3 = 9 种可能的组合。写下每个组合的运算符结果,然后找到在代码中表达该结果的最简单方法。

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