前任。如果我给2013-7-1
,2013-7-7
有 5 个工作日(周一至周五),所以输出应该是 5
PS:在这个问题中节假日除外,只考虑周末。
有没有人知道将它实现到 C++ 中?
我不打算为此编写代码,因为我确信这是某种形式的作业(或者您正在为此获得报酬,或者其他类似的东西,这意味着解决实际问题是您的任务)。
C(以及因此 C++)在 中具有一组函数<ctime>
,可让您使用时间。
鉴于这些,一个人可以取任意日期,time_t
使用a mktime
,然后用 减去一个difftime
,给你一个“两个值之间的秒数time_t
。如果你将 a 转换time_t
回 struct tm
,它将有一个“工作日”条目,因此您可以知道,例如,2013 年 7 月 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;}