2012-05-07 24 views
-1

我只是想知道是否有人注意到我的代碼塊有問題。這個程序應該是一個比較兩個日期的測試程序。如果調用日期較大,則im正在處理的函數應該返回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 


} 

唯一的問題即時得到是,當我試圖改變天(日期第二個參數)。

+1

你能編輯你的問題,包括一些不能正確比較的日期嗎? –

+0

爲什麼在某些條件下不使用another_date.month?在應付StackOverflow時出錯?否則它是錯誤的。 – Aslan986

回答

2

我不知道這是否是問題,因爲我無法測試它......但是,你compareTo功能有這樣一行:

this->month == month 

它不應該是:

this->month == another_date.month 

+0

哇謝謝,你說得對。我不知道我是如何錯過它的。修復後工作。 –

2

在第一個if語句和它下面的幾個,以及你有:

this->month == month 

這是比較一個月的本身,我認爲你的意思:

this->month == another_date.month 

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

month == another_date.month 

應該就足夠了。

+2

雙「=」在最後一行? – Aslan986

+0

@ Aslan986不錯的地方。 –

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; 
} 

一路上,剪切+粘貼錯誤只是......消失了,因爲測試變得多餘。

+0

謝謝,這比我做這件事的方式要短得多。 –

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; 
} 
+0

謝謝你,我意識到我的代碼搞砸了,看到somone指出它後的錯誤。我以爲有一個更短的方式,但不能夠努力地找到答案。我喜歡你發佈的第二塊代碼。 –

+0

第二塊是完全錯誤的。它說,「2012年1月1日」與2011年2月1日的比較相等。 –

+0

是的,@BenVoigt。謝謝!我已經刪除了越野車版本。 –

0

除非你真的做一個元素逐元素的比較集,我把每一組的投入然後使用mktime將它們轉換爲time_t,並直接比較兩個time_t。在典型情況下,這些將是1970年1月1日午夜以來秒數的32位或64位整數,因此在轉換之後,比較變得微不足道。