2013-07-01 53 views
-5

Ex。如果我給2013-7-12013-7-7,有5個工作日(週一至週五),所以輸出應該是5如何計算兩個給定日期之間的工作日數

PS:在這個問題中,假期除外,只考慮週末。

有沒有人有一個實現這個到c + +的想法?

+7

你需要一個非常複雜的算法,因爲「工作日」需要考慮到節假日等 –

+1

此外,沒有算法,因爲假期可以移動,添加/刪除等,你需要一個完整的歷史列表在特定的國家度假,那麼它變得微不足道。 – sashkello

+0

哦,如果假期也被考慮,這可能太複雜了......讓我們把週末考慮在內。 –

回答

1

對不起,它沒有評論,我也不是一個專業的程序員,但在這裏你去: 它編譯,當我運行它,並輸入在2013年1月1日和12/31/2013/3我得到全年261個工作日。如果你乘以365 *(5/7),你得到260.7,所以它似乎工作。當我做1/1/2013/3和12/31/2015/5時,我得到了783.我還將閏年編程到了90線以內。此外,我的命名約定可能不那麼一致。另外我知道使用嵌套的if語句可能是不好的風格,但無論這只是一個快速和骯髒的事情。

編輯:我決定讓這更精細來練習我自己的C++技能,我已經提供了更多的功能。

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> 
using namespace std; 
class date{ 
public: 
unsigned days_per_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 
unsigned month; 
unsigned day; 
int year; 
unsigned week_day; 
constexpr static int week_day_callibrator[4] = {1,1,2013,3}; 
unsigned get_days(){return days_per_month[(month - 1)];}; 
unsigned get_days(unsigned n){return days_per_month[(n - 1)];}; 
void leap_year(){ 
bool temp = false; 
if(year%4 == 0) 
temp = true; 
if(year%100 == 0) 
temp = false; 
if(year%400 == 0) 
temp = true; 
if(temp == true) 
days_per_month[1] = 29; 
else days_per_month[1] = 28; 
}; 
void truncate() 
{ 
if(month > 12) 
month = 12; 
if(month < 1) 
month = 1; 
if(day > get_days()) 
day = get_days(); 
if(day < 1) 
day = 1; 
} 
date(unsigned m, unsigned d, int y, unsigned wd) : month(m), day(d), year(y), week_day(wd) {leap_year();truncate();} 
date(unsigned m, unsigned d, int y) : month(m), day(d), year(y) 
{ 
leap_year(); 
truncate(); 
int wdc[4] = {week_day_callibrator[0], week_day_callibrator[1], week_day_callibrator[2], week_day_callibrator[3]}; 
while(wdc[0] < month || wdc[1] < day || wdc[2] < year) 
{ 
wdc[3] == 7? wdc[3] = 1: ++wdc[3]; 
if(wdc[1] == get_days(wdc[0])) 
{ 
wdc[1] = 1; 
if(wdc[0] == 12) 
{ 
wdc[0] = 1; 
++wdc[2]; 
leap_year(); 
} 
else{++wdc[0];} 
} 
else{++wdc[1];} 
} 
while(wdc[0] > month || wdc[1] > day || wdc[2] > year) 
{ 
wdc[3] == 1? wdc[3] = 7: --wdc[3]; 
if(wdc[1] == 1) 
{ 
if(wdc[0] == 1) 
{ 
wdc[0] = 12; 
--wdc[2]; 
leap_year(); 
} 
else{--wdc[0];} 
wdc[1] = get_days(wdc[0] - 1); 
} 
else{--wdc[1];} 
} 
week_day = wdc[3]; 
} 

date& operator++(){ 
week_day == 7? week_day = 1: ++week_day; 
if(day == get_days()) 
{ 
day = 1; 
if(month == 12) 
{ 
month = 1; 
++year; 
leap_year(); 
} 
else{++month;} 
} 
else{++day;} 
} 

date& operator--() 
{ 
week_day == 1? week_day = 7: --week_day; 
if(day == 1) 
{ 
if(month == 1) 
{ 
month = 12; 
--year; 
leap_year(); 
} 
else{--month;} 
day = get_days(month - 1); 
} 
else{--day;} 
} 
inline bool operator==(const date& rhs) 
{ 
if(year == rhs.year && month == rhs.month && day == rhs.day) 
return true; 
else 
return false; 
} 
inline bool operator!=(const date& rhs){return !operator==(rhs);} 
inline bool operator< (const date& rhs) 
{ 
if(year < rhs.year) 
return true; 
else if(month < rhs.month) 
return true; 
else if(day < rhs.day) 
return true; 
else 
return false; 
} 
inline bool operator> (const date& rhs){return operator< (rhs);} 
inline bool operator<=(const date& rhs){return !operator> (rhs);} 
inline bool operator>=(const date& rhs){return !operator< (rhs);} 
}; 

unsigned count_work_days(date & a, date & b) 
{ 
unsigned counter = 0; 
while(a < b) 
{ 
if(a.week_day != 1 && a.week_day != 7) 
{ 
++counter; 
} 
++a; 
} 
// makes it inclusive 
if(b.week_day != 1 && b.week_day != 7) 
++counter; 
return counter; 
} 

int main() { 
// initializes variables, calls cin to ask the user to input them, varifies the validity of the values and calls the compare function 
string temp; 
char temp2; 
unsigned beginmonth, begindayofmonth, beginyear; 
unsigned endmonth, enddayofmonth, endyear; 
cout << "enter start date: mm/dd/yyyy" << endl; 
cin >> temp; 
stringstream stemp(temp); 
stemp >> beginmonth >> temp2 >> begindayofmonth >> temp2 >> beginyear; 
cout << "enter end date: mm/dd/yyyy" << endl; 
cin >> temp; 
stemp.clear(); 
stemp.str(temp); 
stemp >> endmonth >> temp2 >> enddayofmonth >> temp2 >> endyear; 
date b(beginmonth,begindayofmonth,beginyear); 
date e(endmonth,enddayofmonth,endyear); 
cout << count_work_days(b,e) << endl; 
return 0;} 
+0

謝謝!我會檢查這個! –

0

我不會爲此編寫代碼,因爲我確信這是某種形式的作業(或者您正在爲此付費或者其他類似的東西,這意味着您的任務是解決實際問題問題)。

C(以及C++)在<ctime>中有一組函數,它們允許您使用時間。

考慮到這些,可以採用任意日期,使一個time_t使用mktime,並與difftime減去從另一個,給你們兩個time_t值之間的「秒數。如果轉換一個time_t回到結構tm,它將有一個「工作日」條目,所以你可以說,例如,2013年7月1日是星期一。

鑑於所有這一切,應該很可能計算絕對天數兩個日期,如果你知道你在哪一天開始,找出這個時期有多少個星期六和星期天。

相關問題