如何计算两个给定日期之间的工作日数

时间:2013-07-01 02:35:49

标签: c++

实施例。如果我将2013-7-1提供给2013-7-7,则有5个工作日(周一至周五),因此输出应为5

PS:在这个问题上,假期除外,只考虑周末。

有没有人想过将它实现到c ++中?

2 个答案:

答案 0 :(得分:1)

我不打算为此编写代码,因为我确信这是某种形式的作业(或者你是为这样做而付出的,或其他类似的东西,这意味着你要解决实际问题)。

C(以及C ++)在<ctime>中有一组函数,允许您使用时间。

鉴于这些,可以选择一个任意日期,使用time_t创建一个mktime,然后使用difftime从另一个中减去一个,给出“两个之间的秒数time_t值。如果您将time_t转换回结构tm,它将有一个“工作日”条目,因此您可以告诉您,例如,2013年7月1日是星期一。

考虑到所有这些,应该很有可能计算两个日期之间的绝对天数,如果你知道你开始的那一天,找出这个时期有多少个星期六和星期日。

答案 1 :(得分:1)

对不起它没有评论,我也不是专业的程序员,但是你去了: 它编译,当我运行它并输入1/1/2013/3和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;}