In: Computer Science
#include<iostream>
using namespace std;
class Elapsed_Days
{
// members to store the current date, month, year and total days elapsed since 1900
private:
long curr_year, curr_month, curr_day, total_elapsed_day;
public:
// constructor, checking whether the data entered is correct or not
Elapsed_Days(long year, long month, long day)
{
// if the data is correct then setting that data.
if(is_year_valid(year) && is_month_valid(month))
{
curr_year = year;
curr_month = month;
curr_day = day;
}
// if the date is not valid
else
{
cout<<"Date is not valid"<<endl;
exit(1);
}
}
// destructor
~Elapsed_Days()
{
cout<<"In deconstructor";
}
// checking if the year passed is valid or not
bool is_year_valid(long year)
{
if(year >= 1900)
return true;
return false;
}
// checking if the month passed is valid or not
bool is_month_valid(long month)
{
if(month <= 12 && month >=1)
return true;
return false;
}
// checking if the given year is a leap year or not
bool is_leap_year(long year)
{
if(year%100 == 0)
{
if(year%400 == 0)
return true;
return false;
}
else
{
if(year%4 == 0)
return true;
return false;
}
}
// calculating the number of days in a month
long return_days_in_month(long year, long month)
{
bool is_leap = is_leap_year(year);
if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
return 31;
}
else if(month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
else
{
if(is_leap)
return 29;
return 28;
}
}
//calculating the number of days in a given year
long return_days_in_year(long year)
{
if(year == curr_year)
{
long days = 0;
for(int i=1; i<curr_month; i++)
{
days += return_days_in_month(year,i);
}
// counting till last day
days += curr_day-1;
return days;
}
else
{
if(is_leap_year(year))
{
return 366;
}
return 365;
}
}
// setting the field value
void set_elapsed_days(long elapsed_days)
{
total_elapsed_day = elapsed_days;
}
// computing the total number of days elapsed
void compute_elapsed_days()
{
long days = 0;
for(int i=1900 ; i<=curr_year; i++)
{
days += return_days_in_year(i);
}
set_elapsed_days(days);
}
// showing the number of days elpased
void display_elapsed_days()
{
compute_elapsed_days();
cout<<"The number of days elapsed since Jan 1, 1900 is "<<total_elapsed_day<<endl;
}
long get_elapsed_days()
{
return total_elapsed_day;
}
};
int main()
{
long day, month, year;
cout<<"Enter the day:"<<endl;
cin>>day;
cout<<"Enter the month:"<<endl;
cin>>month;
cout<<"Enter the year:"<<endl;
cin>>year;
Elapsed_Days d1(year, month,day);
d1.display_elapsed_days();
}