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
|
#include <iostream>
#include <cmath>
using namespace std;
class date
{
private:
int m;
int d;
int y;
public:
date();
date(int, int, int);
int countLeapYears(date&);
int getDifference1(date&);
int getDifference2(date&);
};
int main()
{
int day, month, year;
char c;
cout << "Enter a start date: " << endl;
cin >> month >> c >> day >> c >> year;
date start = date(month, day, year);
cout << "Enter an end date: " << endl;
cin >> month >> c >> day >> c >> year;
date end = date(month, day, year);
int duration = end - start;
cout << "The number of days between those two dates are: " << duration << endl;
return 0;
}
date::date()
{
m = 0;
d = 0;
y = 0;
}
date::date(int a, int b, int c)
{
m = a;
d = b;
y = c;
}
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int date::countLeapYears(date& d)
{
int years = d.y;
if (d.m <= 2)
years--;
return years / 4 - years / 100 + years / 400;
}
int date::getDifference1(date& start)
{
int n1 = start.y*365 + start.d;
for (int i=0; i<start.m - 1; i++)
{
n1 += monthDays[i];
n1 += countLeapYears(start);
}
return n1;
}
int date::getDifference2(date& end)
{
int n2 = end.y*365 + end.d;
for (int i=0; i<end.m - 1; i++)
{
n2 += monthDays[i];
n2 += countLeapYears(end);
}
return n2;
}
| |