-1

我只是想知道是否有人注意到我的代码块有问题。这个程序应该是一个比较 2 个日期的测试程序。如果调用日期更大,我正在处理的函数应该返回 1,如果调用日期小于,则返回 -1,如果调用日期等于参数中的日期,则返回 0。我的测试程序:

#include <cstdlib>
#include <iostream>
#include <string>

#include "date.h"

using namespace std;

//date is initialized in a month/day/year format.

int main(int argc, char* argv[])
{
    string* d;

    date d1(4,1,4);
    date d4(4,4,4);

    int greaterTest = d4.compareTo(d1);
    int lessTest = d1.compareTo(d4);


    cout << greaterTest << endl;               //i believe these two lines are printing out a
    cout << lessTest << endl;                  //location in memory
    cout<<&d <<endl;


    system("pause");
    return EXIT_SUCCESS;
}

巨大的 compareTo() 函数:

    int date::compareTo (date another_date)
{

    if (this->year == another_date.year && this->month == month && this->day < another_date.day)    //if both year and month are the same, test to see if day is less
    {

        return -1;
    }

    else if (this->year == another_date.year && this->month == month && this->day > another_date.day)   //if both year and month are the same, test to see if day is greater
    {

        return 1;
    }


    else if (this->year == another_date.year && this->month > month)                            //if the years are the same, test to see if the invoking month is greater
    {

        return 1;
    }

    else if (this->year == another_date.year && this->month < month)                            //if the years are the same, test to see if the invoking month is less
    {

        return -1;
    }


    else if (this->year > another_date.year)                                                    //test to see if the invoking year is greater
    {

        return 1;
    }

    else if (this->year < another_date.year)                                                    //test to see if the invoking year is less
    {

        return -1;
    }

    else if(this-> year == another_date.year && this-> month == another_date.month                  //test if the dates are exactly the same
        && this-> day == another_date.day)
    {

        return 0;
    } 


    //else{ return 15;}                                                                             //if none are true, return 15


}

我得到的唯一问题是当我尝试更改日期(日期的第二个参数)时。

4

5 回答 5

2

我不确定这是否是问题,因为我无法测试它......但是,你的compareTo函数有这一行:

this->month == month

不应该是:

this->month == another_date.month

?

于 2012-05-07T21:13:21.800 回答
2

在第一个 if 语句和它下面的几个语句中,你有:

 this->month == month

这是将月份与自身进行比较,我认为您的意思是:

 this->month == another_date.month

你也不需要一直使用'this'指针,

month == another_date.month 

应该足够了。

于 2012-05-07T21:13:46.930 回答
0

这可能会受益于一些提前退出:

int date::compareTo (date another_date)
{
    if (year > another_date.year) {
       //the invoking year is greater
       return 1;
    }

    if (year < another_date.year) {
       //the invoking year is less
       return -1;
    }

    // if we reached here, the years are the same.  Don't need to compare them for the other cases

    if (month > another_date.month) {
       return 1;
    }

    if (month < another_date.month) {
       return -1;
    }

    // if we reached here, the year and month are the same

    if (day > another_date.day) {
       return 1;
    }

    if (day < another_date.day) {
       return -1;
    }

    // if we reached here, the year and month and day are the same
    return 0;
}

一路上,剪切+粘贴错误只是......消失了,因为那个测试变得多余了。

于 2012-05-07T21:14:32.157 回答
0

我没有在您的原始代码中找到您的错误,因为它对我来说太难阅读了。我想这也是你没有找到的原因。

这种替代方案可能更容易阅读,也更容易证明是正确的:

// untested
int date::compareTo (date another_date)
{

    if (year < another_date.year) return -1;
    if (year > another_date.year) return 1;
    if (month < another_date.month) return -1;
    if (month > another_date.month) return 1;
    if (day < another_date.day) return -1;
    if (day > another_date.day) return 1;
    return 0;
}
于 2012-05-07T21:16:21.957 回答
0

除非您真的打算进行逐个元素的比较,否则我会将每组输入放入 astruct tm中,然后使用mktime将它们转换为 a ,然后直接time_t比较这两个time_ts。在典型情况下,这些将是自 1970 年 1 月 1 日午夜以来的秒数的 32 位或 64 位整数,因此在转换之后,比较变得微不足道。

于 2012-05-07T21:16:30.413 回答