2015-01-20 55 views
0

我不得不爲我的大學實驗室編寫一個程序。在該程序中,我想比較格式爲日/月/年的兩個日期。我知道如何做到這一點,但不包括小時。現在我將該日期轉換爲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; 
} 

還有就是我試圖重載> =運營​​商之一。請幫忙。

+0

小時會增加小數天數,所以如果您想保留一個類似的算法,您仍然不能返回'long int'。 – crashmstr 2015-01-20 18:54:27

+0

http://stackoverflow.com/questions/4196153/find-how-many-seconds-past-since-1-1-1970 – 2015-01-20 18:57:16

回答

0

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

感謝您的回覆。這很有幫助,但現在我還有其他一些問題。我想創建一個反向工作的函數,這意味着從「從0年過去的小時」獲得日/月/年和小時。現在我正在努力爭取一年,但不知怎的,它不能像它應該那樣工作,例如將日期1/1/2016小時1轉換爲「小時...」,然後返回給我2017年。它是因爲使用int值? 'year =((count-leapyears)/ 24)/ 365;' – Bringer 2015-01-20 20:20:29

+0

如果我說得對,現在想做的是從一個單一的組件(從0年開始的小時數),得到年數,幾個月,幾天還有剩餘時間? 這比你到目前爲止所做的還要複雜 – Angivare 2015-01-20 20:29:10

+0

我使用這種格式的主要目的是將日期添加/移動x天更簡單(至少我認爲是這樣)。但正如你所說,將其轉換回來非常困難。 – Bringer 2015-01-20 20:33:56

0

countobj.countoperator >=之間的3個可能的關係。 count < obj.countcount == obj.countcount > obj.count。這同樣適用於hoursobj.hours。這給出了3 * 3 = 9種可能的組合。寫下每個組合的運算符結果,然後找到在代碼中表達它的最簡單方法。

請注意,您不需要爲每個比較運算符執行此操作。通常你執行operator <然後根據那個定義其他的。