两个对象之间的日期比较

时间:2015-01-20 18:52:26

标签: c++ calendar comparison

我必须为我在大学的实验室写一个程序。在程序中,我想以日/月/年的格式比较两个日期。我知道怎么做,但不包括小时。现在我将日期转换为自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;
}

我尝试重载&gt; =运算符。请帮忙。

2 个答案:

答案 0 :(得分:0)

算法中的

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;

答案 1 :(得分:0)

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

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

相关问题