简体   繁体   English

如何比较C或C++中的日/月/年?

[英]How to compare day/month/year in C or C++?

I have a program that asks the user to input to dates then it displays which one is more recent I've done it like this我有一个程序要求用户输入日期然后它显示哪个是最近的我已经这样做了

if (year1>year2 || month1>month2 || day1>day2)
    return -1;

if (year1<year2 || month1<month2 || day1<day2)
    return +1;

but the output is not quite correct.但是 output 不太正确。

Here's a clean way to do it: 这是一个干净的方法:

#include <tuple> // for std::tie

auto date1 = std::tie(year1, month1, day1);
auto date2 = std::tie(year2, month2, day2);

if (date1 == date2)
  return 0;

return (date1 < date2) ? -1 : 1; 

The comparisons of std::tie objects are lexicographical, so this returns -1 if date1 is less than date2 , 0 if they are the same, and 1 if date1 is greater than date2 . 的比较std::tie对象是词典编纂,所以这个返回-1如果date1小于date20如果它们是相同的,和1如果date1大于date2

You might be better off defining your own date type (or use boost::datetime ). 您可能最好定义自己的date类型(或使用boost::datetime )。

struct Date
{
  unsigned year;
  unsigned month;
  unsigned day;
};

bool operator<(const Date& lhs, const Date& rhs)
{
  return std::tie(lhs.year, lhs.month, lhs.day) < 
         std::tie(rhs.year, rhs.month, rhs.day);
}

bool operator>(const Date& lhs, const Date& rhs) { .... }

bool operator==(const Date& lhs, const Date& rhs) { .... }

int date_cmp(const Date& lhs, const Date& rhs) 
{
  // use operators above to return -1, 0, 1 accordingly
}

You need a much more complicated check than that: 你需要一个比这更复杂的检查:

if (year1 > year2)
    return -1;
else if (year1 < year2)
    return +1;

if (month1 > month2)
    return -1;
else if (month1 < month2)
    return +1;

if (day1 > day2)
    return -1;
else if (day1 < day2)
    return +1;

return 0;

NOTE: Returning -1 for first is greater than second seems counter-intuititive to me, however I have followed the semantics provided by the OP. 注意: 首先返回-1 大于第二个似乎是反直觉的,但是我遵循了OP提供的语义。

This statement 这个说法

if (year1>year2 || month1>month2 || day1>day2)
    return -1;

tests if any one of the three conditions is true. 测试三个条件中的任何一个是否为真。 So, if year1 is higher than year 2, or month1 is higher than month2. 因此,如果year1高于第2年,或者month1高于month2。 Lets stop there. 让我们停在那里。 Consider 考虑

year1 = 2013, month1 = 12, day1 = 31;
year2 = 2014, month2 = 1, day1 = 1;

We know that, infact, year2 is a higher value, but what happens is 我们知道,事实上,第二年是一个更高的价值,但会发生什么

is year1 > year2? no
ok, but is month1 > month2? yes

This makes it look like the first year is a higher value, but it's not, it just a higher month value. 这使得它看起来第一年的价值更高,但事实并非如此,它只是一个更高的月值。

As you get further into C++ you'll find that it's a good idea to try and adopt a convention of making all your comparisons use a single operator (< or >), when you reach a point where you are working with operators you'll understand why. 当你进一步学习C ++时,你会发现尝试采用一种使所有比较使用单个运算符(<或>)的约定是个好主意,当你达到你正在使用运算符的点时你会明白为什么。

if (year2 < year1)
    return 1;
// we reach this line when year1 <= year2
if (year1 < year2) // elimnate the < case
    return -1;

// having eliminated both non-matches,
// we know that by reaching point that both
// dates have the same year. Now repeat for
// the month value.
if (month2 < month1)
    return 1;
if (month1 < month2)
    return -1;

// year and month must be the same, repeat for day.
if (day2 < day1)
    return 1;
if (day1 < day2)
    return -1;
return 0; // exact match

//You can try this //你可以试试这个

int lday,lmonth,lyear;
int nday,nmonth,nyear;
int lhour,lminute;
int nhour,nminute;
sscanf(New_Time,"%d-%d",&nhour,&nminute); //reads the numbers
sscanf(Last_Time,"%d-%d",&lhour,&lminute); //from the string
sscanf(New_Date,"%d-%d-%d",&nday,&nmonth,&nyear);
sscanf(Last_Date,"%d-%d-%d",&lday,&lmonth,&lyear);
//cout << "Last date: " << lday << "-" << lmonth << "-" << lyear  <<endl;
//cout << "New date: " << nday << "-" << nmonth << "-" << nyear  <<endl;

if(nyear>lyear)
    return 0;
if(nyear==lyear) {
    if(nmonth > lmonth)
        return 0;
    if (nmonth == lmonth) {
        if(nday > lday)
            return 0;
        if (nday == lday) {
            if( nhour > lhour)
                return 0;
            if( nhour == lhour) {
                if(nminute>lminute) {
                    //cout << "new time >= last time" << endl <<endl;
                    return 0;
                }
                else return 1;
            }
            else return 1;
        }
        else return 1;
    }
    else return 1;
}
else return 1;
struct Day
{
    int value;

    explicit Day(int value)
    {
        this->value = value;
    }
};

struct Month
{
    int value;

    explicit Month(int value)
    {
        this->value = value;
    }
};

struct Year
{
    int value;

    explicit Year(int value)
    {
        this->value = value;
    }
};

class Date {
public:
    Date(Day newDay, Month newMonth, Year newYear)
    {
        _day = newDay.value;
        _month = newMonth.value;
        _year = newYear.value;
    }

    int GetYear() const {
        return _year;
    };
    int GetMonth() const {
        return _month;
    };
    int GetDay() const {
        return _day;
    };

private:
    int _year;
    int _month;
    int _day;
};    

bool operator < (const Date& lhs, const Date& rhs)
    {
        if (lhs.GetYear() == rhs.GetYear()) {
            if (lhs.GetMonth() == rhs.GetMonth()) {
                if (lhs.GetDay() == rhs.GetDay()) {
                    return false;
                }
    
                return lhs.GetDay() < rhs.GetDay();
            }
    
            return lhs.GetMonth() < rhs.GetMonth();
        }
    
        return lhs.GetYear() < rhs.GetYear();
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM