c++ - How to calculate number of workdays between two given date -
ex. if give 2013-7-1
2013-7-7
, there 5 workdays (mon-fri) , output should 5
ps: in problem holidays excepted, consider weekends.
does have idea of implementing c++?
sorry isn't commented , i'm not professional programmer here go: compiles , when run , type in 1/1/2013/3 , 12/31/2013/3 261 work days year. if multiply 365*(5/7) 260.7 seems work. when 1/1/2013/3 , 12/31/2015/5 783. programmed leap year in less 90 lines. naming conventions might not consistent. know it's bad style use nested if statements whatever quick , dirty thing.
edit: decided make more ellaborate practice own c++ skills, i've provided more functionality it.
#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 inclusive if(b.week_day != 1 && b.week_day != 7) ++counter; return counter; } int main() { // initializes variables, calls cin ask user input them, varifies validity of values , calls 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;}
Comments
Post a Comment