I've been trying to write a program(in c++) for the past couple of hours that is supposed to calculate the number of work days between two, user created, dates.
The code calculates the number of days(with weekends) between dates perfectly, each time. But the problem occurs when calculating the work(business)days.
On some dates it runs fine, on others it outputs one day extra/less (ie. 16/10/'16 - 14/12/'16).
I'll paste the code here, and I'm already looking forward to seeing how would you guys solve this !
Final notes : This code is not designed to calculate dates from one year to another(yet), it can only work(somewhat) within 12 months of the same year.
The formula I used to calculate work days from total number of days is :
totalDays - (totalDays / 7) * 2 which is by no means perfect, because a hack must be implemented to deal with rounding up the number gotten by doing totalDays/7 division.
Code is written in Visual Studio 15, my platform of choice. I've used this online calculator to check my output :
https://www.timeanddate.com/date/workdays.html
Source code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
struct Date{
int d,m,y;
void Input(){
cout<<"Input Date in format : D/M/Y "<<endl;
cin>>d>>m>>y;
}
void Output(){
cout<<d<<"."<<m<<"."<<y<<". year."<<endl;
}//this function will be implemented later on
};
int calcWorkDays(Date ,Date);
void main()
{
Date Date_start,Date_end;
float budget;
cout<<"Insert how much money you have in your budget : ";
cin>>budget;
cout<<"Insert Dates (FROM-TO) to calculate your maximum spending money per workday : "<<endl;
Date_start.Input(); Date_end.Input();
cout<<"Workdays : "<<calcWorkDays(Date_start,Date_end)<<"\t budget : "<<budget<<endl;
//cout<<"You can spend(on workdays) on average : "<<budget/calcWorkDays(Date_start,Date_end)<<" $."<<endl;
system("PAUSE>NULL");
}
int calcWorkDays(Date From, Date To){
int days = 0; //total number of days, including weekends
int counter = 0; //checks if its the first month that is being counted
int month =From.m;
while(month <=To.m){
if(month < To.m){
switch(month){
case 2 :
if(From.y % 4 == 0 && From.y % 100 && From.y %400)
{
if(counter == 0)
days+=29- From.d;
else
days+=29;
}
else{
if(counter == 0)
days+=28 -From.d;
else
days+=28;
}
break;
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 :
if(counter == 0)
days+=31 - From.d;
else
days+=31;
break;
case 4 :
case 6 :
case 9 :
case 11 :
if(counter == 0)
days+=30-From.d;
else
days+=30;
break;
} //END OF SWITCH STATEMENT
}//End of condition that checks if the last month is being counted,
// if it is, it doesnt subtract anything, it just adds the days of
//that month to the total sum of days.
else{
days+=To.d;
}
month++;
counter++;
};
days+=1; //Include the last day in the count
cout<<"DAYS(inc. weekends) : "<<days<<endl;
double x = (double)days/7;
int IntPartX = (int)x;
float decPart = x - IntPartX;
if(decPart>=0.5 || decPart *2 >=0.5) // this if-else statement is supposed to serve a purpose
// of rounding up numbers (threshold at 0.5) to the closest larger integer
{
days-=IntPartX * 2;
if(decPart *2 >=0.5)
days-=2;
else
days-=1;
return days;
}
else
return days - IntPartX *2;
}
|