营业日的计算方法
Method to calculate business days
我有一个练习,有点小麻烦。
我必须创建一个带有两个参数的计算器:开始日期和要添加的天数(周六和周日除外,只有工作日,从周一到周五)。另一件事是总和必须包括开始日期。
例如以2016年7月12日为起始日,再加8天,对应2016年7月21日(周六日除外,2016年7月21日周二算1天)。
我希望我已经清楚了。
我尝试编写一些代码,但它不起作用。
// rStringGridEd1->IntCells[3][row] is a custom stringgrid
// and correspond to the number of days to add, j is the
// counter for the loop
while (j < rStringGridEd1->IntCells[3][row])
{
if (DayOfWeek(date) != 1 || DayOfWeek(date) !=7)
{
// if current date (TDate date = "12/07/16") is not Saturday or Sunday increment date by one day
date++;
}
else if(DayOfWeek(date) == 1)
{
//If date correspond to sunday increment the date by one and j the counter by one
date=date+1;
j++;
}
else if(DayOfWeek(date) == 7)
{
//If date correspond to saturday increment the date by two days and j the counter by one
date=date+2;
j++;
}
j++;
}
有人能帮帮我吗?
如果您不需要使用循环,那么您可能需要考虑使用更简单的计算来重构您的解决方案。例如,考虑每五个工作日自动将日期增加 7 天。因此,使用商和剩余的天数来添加应该告诉你有多少天要添加到你的 date
变量而不诉诸蛮力循环。
由于这是一个练习,我不会深入探讨代码的细节,但需要考虑的几件事可能是您如何在知道开始日期的情况下计算出一周中的哪一天结束。另外,如果你在星期五结束,紧随其后的周末会发生什么。
这是建立在 <chrono>
之上的 Lee Painton's excellent (and up-voted) answer would look like using this free, open-source C++11/14 date library:
#include "date.h"
#include <iostream>
date::year_month_day
get_end_job_date(date::year_month_day start, date::days length)
{
using namespace date;
--length;
auto w = weeks{length / days{5}};
length %= 5;
auto end = sys_days{start} + w + length;
auto wd = weekday{end};
if (wd == sat)
end += days{2};
else if (wd == sun)
end += days{1};
return end;
}
你可以这样练习:
int
main()
{
using namespace date::literals;
std::cout << get_end_job_date(12_d/jul/2016, date::days{8}) << '\n';
}
输出:
2016-07-21
这个简单的计算器有一个前提条件,即 start
不是周末。如果这不是理想的先决条件,那么您可以在计算之前检测到这一点,并在一两天内在内部增加 start
。
date library takes care of things like the relationship between days
and weeks
, and how to add days
to a date. It is based on very efficient (non-iterative) algorithms shown and described here.
我有一个练习,有点小麻烦。
我必须创建一个带有两个参数的计算器:开始日期和要添加的天数(周六和周日除外,只有工作日,从周一到周五)。另一件事是总和必须包括开始日期。
例如以2016年7月12日为起始日,再加8天,对应2016年7月21日(周六日除外,2016年7月21日周二算1天)。
我希望我已经清楚了。
我尝试编写一些代码,但它不起作用。
// rStringGridEd1->IntCells[3][row] is a custom stringgrid
// and correspond to the number of days to add, j is the
// counter for the loop
while (j < rStringGridEd1->IntCells[3][row])
{
if (DayOfWeek(date) != 1 || DayOfWeek(date) !=7)
{
// if current date (TDate date = "12/07/16") is not Saturday or Sunday increment date by one day
date++;
}
else if(DayOfWeek(date) == 1)
{
//If date correspond to sunday increment the date by one and j the counter by one
date=date+1;
j++;
}
else if(DayOfWeek(date) == 7)
{
//If date correspond to saturday increment the date by two days and j the counter by one
date=date+2;
j++;
}
j++;
}
有人能帮帮我吗?
如果您不需要使用循环,那么您可能需要考虑使用更简单的计算来重构您的解决方案。例如,考虑每五个工作日自动将日期增加 7 天。因此,使用商和剩余的天数来添加应该告诉你有多少天要添加到你的 date
变量而不诉诸蛮力循环。
由于这是一个练习,我不会深入探讨代码的细节,但需要考虑的几件事可能是您如何在知道开始日期的情况下计算出一周中的哪一天结束。另外,如果你在星期五结束,紧随其后的周末会发生什么。
这是建立在 <chrono>
之上的 Lee Painton's excellent (and up-voted) answer would look like using this free, open-source C++11/14 date library:
#include "date.h"
#include <iostream>
date::year_month_day
get_end_job_date(date::year_month_day start, date::days length)
{
using namespace date;
--length;
auto w = weeks{length / days{5}};
length %= 5;
auto end = sys_days{start} + w + length;
auto wd = weekday{end};
if (wd == sat)
end += days{2};
else if (wd == sun)
end += days{1};
return end;
}
你可以这样练习:
int
main()
{
using namespace date::literals;
std::cout << get_end_job_date(12_d/jul/2016, date::days{8}) << '\n';
}
输出:
2016-07-21
这个简单的计算器有一个前提条件,即 start
不是周末。如果这不是理想的先决条件,那么您可以在计算之前检测到这一点,并在一两天内在内部增加 start
。
date library takes care of things like the relationship between days
and weeks
, and how to add days
to a date. It is based on very efficient (non-iterative) algorithms shown and described here.