两个对象之间的日期比较
Date comparison between two objects
我不得不为我的大学实验室编写一个程序。在程序中,我想以日/月/年格式比较两个日期。我知道该怎么做,但不包括小时。现在,我将日期转换为自 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 >=
内部的 count
和 obj.count
之间有 3 种可能的关系。要么count < obj.count
,要么count == obj.count
要么count > obj.count
.hours
和obj.hours
也是如此。这给出了 3 * 3 = 9 种可能的组合。写下每个组合的运算符结果,然后找到在代码中表达该结果的最简单方法。
请注意,您不需要为每个比较运算符执行此操作。通常,您实现operator <
然后根据该定义其他。
相关文章:
- 如何找到两个日期之间的时间差异(以秒和纳秒为单位)?
- 日期和当前日期之间的差异
- CPP 程序二查找两个给定日期之间的天数,conol CMD
- Qml - c++ 类和 QmlRegisterType 创建的类之间的交换日期
- 十进制格式的日期之间的差异
- 解决方案在两个日期之间的天数内不起作用
- 使用 c++ 获取两个日期与时间字符串之间的差异(以秒为单位)
- 计算当前日期和用户输入日期问题之间的天数
- C 的两个日期之间的差异
- 如何使用C Win32 API计算两个日期之间的天数
- C++计算日期之间的差异
- 两个日期之间的 C++ 日期差异
- v8::值到日期之间的转换
- 两个对象之间的日期比较
- 确定日期之间的差异
- C++ 给定日期和今天日期之间的天数
- 找出日期之间的差异,并将其归类
- c++获取用户选择的日期与实际日期之间的年份(计算日、月、年)
- 两个日期之间的总天数
- 设计一个结构来存储时间和日期.写一个函数来计算两个时间之间的差(以分钟为单位)