Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ program for date calculation with the following Date calculation algorithms

ID: 3763410 • Letter: C

Question

C++ program for date calculation with the following Date calculation algorithms

Calculate day number from date.

/*All division is integer division, operator % is modulus.*/

Given integer y, m, d, calculate day number g as:

function g(y,m,d)

m = (m + 9) % 12

y = y - m/10

return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 )

Calculate date from day number

*/All division is integer division, operator % is modulus./*

Given day number g, calculate year, month, and day:

function d(g)

y = (10000*g + 14780)/3652425

ddd = g - (365*y + y/4 - y/100 + y/400)

if (ddd < 0) then

y = y - 1

ddd = g - (365*y + y/4 - y/100 + y/400)

endif

mi = (100*ddd + 52)/3060

mm = (mi + 2)%12 + 1

y = y + (mi + 2)/12

dd = ddd - (mi*306 + 5)/10 + 1

return y, mm, dd

Applications.

Date differences. The difference in days between two dates:

g(y2,m2,d2) - g(y1,m1,d1)

Day offsets. The date n days from y,m,d:

d(g(y,m,d) + n)

Date legality. To check if a date is on the calendar:

if [y,m,d] = d(g(y,m,d))

Input file:

1.       I have a wedding on June 4, 2016 I need to know how many days I have to plan for the wedding.   Today is Dec. 1, 2015.

2.       Today is Oct 18, 2015. How many shopping days are there till Christmas?

3.       I will received a report and I have been told I have 93 days to respond to the report. If I get the report on May 18, 2016, by what date must I have a response?

4.       I have a class I teach in New Orleans every week and I can get a direct flight from Huntsville to New Orleans. I will need a flight every Wednesday (class that day) for Aug. 24, 2016 through Nov. 16, 2016 I need to know what those dates are for booking purposes.

5.       I work in a hospital and we just received a fancy x-ray machine (computer control, graphics the works). Now to keep the guarantee valid on the machine, we have to perform prevented maintenance every 21 working/operational days. It takes two days to do the maintenance (non working days). I need to set up a schedule showing which dates that maintenance is to be done. (so as to not schedule any patients on those days). We will start the machine on Nov. 10, 2015 and I need a schedule for the next six months showing the maintenance days.

Explanation / Answer

Below is C++ implementation of above idea.

There are three programs of above algorithmso, please choose the easy one which help you

1) // C++ program two find number of days between two given dates

#include<iostream>

using namespace std;

// A date has day 'd', month 'm' and year 'y'

struct Date

{

    int d, m, y;

};

// To store number of days in all months from January to Dec.

const int monthDays[12] = {31, 28, 31, 30, 31, 30,

                           31, 31, 30, 31, 30, 31};

// This function counts number of leap years before the

// given date

int countLeapYears(Date d)

{

    int years = d.y;

    // Check if the current year needs to be considered

    // for the count of leap years or not

    if (d.m <= 2)

        years--;

    // An year is a leap year if it is a multiple of 4,

    // multiple of 400 and not a multiple of 100.

    return years / 4 - years / 100 + years / 400;

}

// This function returns number of days between two given

// dates

int getDifference(Date dt1, Date dt2)

{

    // COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'

    // initialize count using years and day

    long int n1 = dt1.y*365 + dt1.d;

    // Add days for months in given date

    for (int i=0; i<dt1.m - 1; i++)

        n1 += monthDays[i];

    // Since every leap year is of 366 days,

    // Add a day for every leap year

    n1 += countLeapYears(dt1);

    // SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'

    long int n2 = dt2.y*365 + dt2.d;

    for (int i=0; i<dt2.m - 1; i++)

        n2 += monthDays[i];

    n2 += countLeapYears(dt2);

    // return difference between two counts

    return (n2 - n1);

}

// Driver program

int main()

{

    Date dt1 = {1, 2, 2000};

    Date dt2 = {1, 2, 2004};

    cout << "Difference between two dates is " << getDifference(dt1, dt2);

    

    return 0;

}

output

difference between two dates is 1461

Try this one also it might help you

2)

3)

1) // C++ program two find number of days between two given dates

#include<iostream>

using namespace std;

// A date has day 'd', month 'm' and year 'y'

struct Date

{

    int d, m, y;

};

// To store number of days in all months from January to Dec.

const int monthDays[12] = {31, 28, 31, 30, 31, 30,

                           31, 31, 30, 31, 30, 31};

// This function counts number of leap years before the

// given date

int countLeapYears(Date d)

{

    int years = d.y;

    // Check if the current year needs to be considered

    // for the count of leap years or not

    if (d.m <= 2)

        years--;

    // An year is a leap year if it is a multiple of 4,

    // multiple of 400 and not a multiple of 100.

    return years / 4 - years / 100 + years / 400;

}

// This function returns number of days between two given

// dates

int getDifference(Date dt1, Date dt2)

{

    // COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'

    // initialize count using years and day

    long int n1 = dt1.y*365 + dt1.d;

    // Add days for months in given date

    for (int i=0; i<dt1.m - 1; i++)

        n1 += monthDays[i];

    // Since every leap year is of 366 days,

    // Add a day for every leap year

    n1 += countLeapYears(dt1);

    // SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'

    long int n2 = dt2.y*365 + dt2.d;

    for (int i=0; i<dt2.m - 1; i++)

        n2 += monthDays[i];

    n2 += countLeapYears(dt2);

    // return difference between two counts

    return (n2 - n1);

}

// Driver program

int main()

{

    Date dt1 = {1, 2, 2000};

    Date dt2 = {1, 2, 2004};

    cout << "Difference between two dates is " << getDifference(dt1, dt2);

    

    return 0;

}

output

difference between two dates is 1461

Try this one also it might help you

2)

  // Days in-between dates  // Compiled in Visual C++ 2008 Express Edition  // Language: C++/STL      #include<iostream>    using namespace std;          int main()  {            int days_in_months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};            int first_day, second_day;            int first_month, second_month;            int first_year, second_year;            int years_difference, days_difference;            int months_total;            int reg_year = 365;                                cout<<"Program to calculate how many days are in between the day/month/year entered."<<endl;          cout<<endl;            cout<<"Please enter the date by day, month, year."<<endl;          cout<<endl;            cout<<"First date:: "<<endl;          cout<<endl;            cout<<"Day: ";          cin>>first_day;          if(first_day > 31 || first_day <= 0)          {                  cout<<"Incorrect day entered"<<endl;                  cin.ignore();                  return 0;          }          cout<<"Month: ";          cin>>first_month;          if(first_month > 12 || first_month <= 0)          {                  cout<<"Incorrect Month entered"<<endl;                  cin.ignore();                  return 0;          }          cout<<"Year: ";          cin>>first_year;          if(first_year > 9999 || first_year < 0)          {                  cout<<"Incorrect Year Entered"<<endl;                  cin.ignore();                  return 0;          }                    cout<<endl;          cout<<" Second date:: "<<endl;          cout<<endl;            cout<<"Day: ";          cin>>second_day;          if(second_day > 31 || second_day <= 0)          {                  cout<<"Incorrect day entered"<<endl;                  cin.ignore();                  return 0;          }          cout<<"Month: ";          cin>>second_month;          if(second_month > 12 || second_month <= 0)          {                  cout<<"Incorrect Month entered"<<endl;                  cin.ignore();                  return 0;          }          cout<<"Year: ";          cin>>second_year;          if(second_year > 9999 || second_year < 0)          {                  cout<<"Incorrect Year Entered"<<endl;                  cin.ignore();                  return 0;          }              /////////////////////////////Years/////////////////////////////////                      if(first_year == second_year)          {                  years_difference = 0;          }          else          {                  if(first_year % 4 == 0 && first_year % 100 != 0 || first_year % 400 == 0)                  {                          if(second_year % 4 == 0 && second_year % 100 != 0 || second_year % 400 == 0)                          {                                  if(first_year > second_year)                                  {                                          years_difference = (first_year - second_year) * (reg_year) + 2;                                  }                                  else                                  {                                          years_difference = (second_year - first_year) * (reg_year) + 2;                                  }                                  if(second_month > first_month)                                  {                                          if(days_in_months[first_month - 1] > days_in_months[1])                                          {                                                  --years_difference;                                          }                                  }                          }                          else                          {                                  if(first_year > second_year)                                  {                                          years_difference = (first_year - second_year) * (reg_year) + 1;                                  }                                  else                                  {                                          years_difference = (second_year - first_year) * (reg_year) + 1;                                  }                                  if(first_month > second_month)                                  {                                          if(days_in_months[second_month - 1] > days_in_months[1])                                          {                                                  --years_difference;                                          }                                  }                          }                  }                  else                  {                          if(first_year > second_year)                          {                                  years_difference = (first_year - second_year) * (reg_year);                          }                          else                          {                                  years_difference = (second_year - first_year) * (reg_year);                          }                  }          }            /////////////////////////////Months////////////////////////////////////                      if(first_month == second_month)          {                  months_total = 0;          }          else          {                  if(first_month > second_month)                  {                          for(int i = (first_month - 1); i > (second_month - 1); i--)                          {                                  static int months_total_temp = 0;                                  months_total_temp += days_in_months[i];                                  months_total = months_total_temp;                          }                  }                  else                  {                          for(int i = (first_month - 1); i < (second_month - 1); i++)                          {                                  static int months_total_temp = 0;                                  months_total_temp += days_in_months[i];                                  months_total = months_total_temp;                          }                  }          }            ////////////////////////////Days//////////////////////////////////            int days_total;            if (first_day == second_day)          {                  days_difference = 0;                  days_total = (years_difference + months_total) - days_difference;          }          else          {                  if(first_day > second_day)                  {                          days_difference = first_day - second_day;                          days_total = (years_difference + months_total) - days_difference;                  }                  else                  {                          days_difference = second_day - first_day;                          days_total = (years_difference + months_total) + days_difference;                  }          }            //////////////////////////In Between Leap Years///////////////////////////////            if(first_year == second_year)          {          }          else          {                  if(first_year > second_year)                  {                          for(int i = (second_year + 1); i < first_year; i++)                          {                                  if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)                                  {                                          cout<<endl;                                          cout<<i<<endl;                                          ++days_total;                                  }                          }                  }                  else                  {                          for(int i = (first_year + 1); i < second_year; i++)                          {                                  if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)                                  {                                          cout<<endl;                                          cout<<i<<endl;                                          ++days_total;                                  }                          }                  }          }            //////////////////////////Output//////////////////////////////////              cout<<endl;          cout<<" The total days in between your dates are: "<<days_total<<endl;          cout<<endl;            cin.get();          cin.ignore();          return 0;    }  

3)

  #include <iostream>  #include <ctime>    class SimpleDate {  private:          int year, month, day;  public:          SimpleDate();           SimpleDate(int year, int month, int day);          int getYear() const { return year; }          int getMonth() const { return month; }          int getDay() const { return day; }          long getJulianDay() const;          long operator-(const SimpleDate &) const;          void print(std::ostream &) const;  };    SimpleDate::SimpleDate() {           struct tm ts;          time_t t = time(NULL);          localtime_r(&t, &ts);          year = ts.tm_year+1900;          month = ts.tm_mon+1;          day = ts.tm_mday;  }    SimpleDate::SimpleDate(int yy, int mm, int dd) : year(yy), month(mm), day(dd)   { }  long SimpleDate::operator-(const SimpleDate &dt) const { return getJulianDay()-dt.getJulianDay(); }  void SimpleDate::print(std::ostream &out) const { out << year << "-" << month << "-" << day; }    long SimpleDate::getJulianDay() const {          int mon = month - 14;          return ( 1461 * ( year + 4800 + mon / 12 ) ) / 4                   + ( 367 * ( month - 2 - 12 * ( mon / 12 ) ) ) / 12                   - ( 3 * ( ( year + 4900 + mon / 12 ) / 100 ) ) / 4                   + day - 32075;  }    std::ostream &operator <<(std::ostream &out, const SimpleDate &dt) { dt.print(out); return out; }    int main() {          SimpleDate today, electionDay(2010, 11, 2);            std::cout << "Days between " << today                  << " and " << electionDay                  << " is " << (electionDay-today)                  << std::endl;            return 0;  }  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote