2012-05-07 13 views
-1

私のコードブロックで何か問題があったことに気付いた人はいませんか?このプログラムは、2つの日付を比較するテストプログラムです。作業中の関数は、呼び出し日が大きい場合は1を、呼び出す日付が-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

なぜいくつかのif条件でanother_date.monthを使用しないのはなぜですか? StackOverflowでの対処にエラーがあります。それ以外の場合はエラーです。 – Aslan986

答えて

2

私はそれをテストすることはできませんので、私は、これが問題であるかどうかわからないんだけど...でも、あなたのcompareTo関数は、このラインを持っています

this->month == month 

は、それはすべきではない:

this->month == another_date.month 

?最初に

+0

うわー、ありがとう。どのように私はそれを逃したのか分からない。それを修理した後に働いた。 –

2

文と、その下の数だけでなく、あなたが持っている場合:これは、それ自体に月を比較している

this->month == month 

、私はあなたが意味を考える:

this->month == another_date.month 

また、あなたはしないでください常にこのポインタを使用する必要があります。

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がそれを指摘した後にエラーを見た。私は短い方法があると思っていたが、答えを見つけるのに十分なほど難しいと思うことができなかった。私はあなたが多く投稿したコードの2番目のブロックが好きです。 –

+0

2番目のブロックは完全に間違っています。 「2012年1月1日」は「2011年2月1日」と同等です。 –

+0

はい、@BenVoigt。ありがとう!私はバギーバージョンを削除しました。 –

0

あなたが本当に要素ごとの比較を行う上で設定している場合を除き、私はへの入力の各セットを置くところa struct tm、次にmktimeを使用して、それらをtime_tに変換し、2つのtime_tを直接比較してください。典型的なケースでは、1970年1月1日午前0時からの秒数の32ビットまたは64ビットの整数なので、変換後は比較が簡単になります。

関連する問題