Code:
#include <iostream>
using namespace std;
class Date
{
public:
Date() {month=1; day=1; year=1900;};
//default constructor; initializes the date to 1/1/1900
Date(int m, int d, int y);
//constructor; initializes the date that is entered in
void set_date(int m, int d, int y);
//allows the user to enter in the date
void display_date();
//prints the date with the month in word format; i.e. 1/1/2000 will print out January 1, 2000
void advance_date();
//advances the date by 1 day
int get_day();
//returns the value of the invoking day
int get_month();
//returns the value of the invoking month
int get_year();
//returns the value of the invoking year
bool is_less_than(Date&);
//returns true if the original invoking date is less than the one passed in, and false otherwise
bool is_equal(Date&);
//returns true if the original invoking date is equal to the one passed in, and false otherwise
private:
int month;
int day;
int year;
};
int difference(int earlier_day, int earlier_month, int earlier_year, int later_day, int later_month, int later_year,
Date date1, Date date2);
int main()
{
Date first_date;
Date second_date;
Date earlier_date;
Date later_date;
int month1, day1, year1, month2, day2, year2;
int m1, d1, y1, m2, d2, y2;
int days_apart;
cout << "Please enter a Date in the following format: xx/xx/xxxx\n";
month1 = first_date.get_month();
day1 = first_date.get_day();
year1 = first_date.get_year();
first_date.set_date(month1, day1, year1);
cout << "First Date is: ";
first_date.display_date();
cout << "Please enter a second Date in tbe following format: xx/xx/xxxx\n";
month2 = second_date.get_month();
day2 = second_date.get_day();
year2 = second_date.get_year();
second_date.set_date(month2, day2, year2);
cout << "Second Date is: ";
second_date.display_date();
if (first_date.is_equal(second_date))
{
cout << "The first date is the same as the second date.\n";
}
else if (first_date.is_less_than(second_date))
{
earlier_date = first_date;
later_date = second_date;
cout << "The first date is earlier than the second date.\n";
d1 = day1;
m1 = month1;
y1 = year1;
d2 = day2;
m2 = month2;
y2 = year2;
}
else
{
earlier_date = second_date;
later_date = first_date;
cout << "The first date is later than the second date.\n";
d1 = day2;
m1 = month2;
y1 = year2;
d2 = day1;
m2 = month1;
y2 = year1;
}
days_apart = difference(d1, m1, y1, d2, m2, y2, earlier_date, later_date);
cout << "The two days are " << days_apart << "days apart.\n";
return 0;
}
int Date::get_month()
{
char slash; //reads in the forward slash between mm/dd/yyyy
int month;
cin >> month >> slash;
return month;
}
int Date::get_day()
{
char slash;
int day;
cin >> day >> slash;
return day;
}
int Date::get_year()
{
int year;
cin >> year;
return year;
}
void Date::set_date(int m, int d, int y)
{
month = m;
day = d;
year = y;
return;
}
void Date::display_date()
{
switch(month)
{
case 1: cout << "January ";
break;
case 2: cout << "February ";
break;
case 3: cout << "March ";
break;
case 4: cout << "April ";
break;
case 5: cout << "May ";
break;
case 6: cout << "June ";
break;
case 7: cout << "July ";
break;
case 8: cout << "August ";
break;
case 9: cout << "September ";
break;
case 10: cout << "October ";
break;
case 11: cout << "November ";
break;
case 12: cout << "December ";
break;
}
cout << day << ", " << year << endl;
return;
}
bool Date::is_equal(Date& second_date)
{
if ((month == second_date.month) && (day == second_date.day) && (year == second_date.year))
return true;
else
return false;
}
bool Date::is_less_than(Date& second_date)
{
if (year < second_date.year)
{
return true;
}
else if (month < second_date.month)
{
return true;
}
else if (day < second_date.day)
{
return true;
}
else
{
return false;
}
}
int difference(int earlier_day, int earlier_month, int earlier_year, int later_day, int later_month,
int later_year, Date date1, Date date2)
{
int diff_days = 0;
while ((earlier_day != later_day) || (earlier_month != later_month) || (earlier_year != later_year))
{
date1.advance_date();
diff_days++;
}
return diff_days;
}
void Date::advance_date()
{
int months[12];
int max_no_months = 12;
int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
}