#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<ctime>
using namespace std;
// check if the year is a leap year
bool is_leap_year(int year)
{
if (year%4==0 && year%100!=0 || year%400==0)
return true;
else
return false;
}
// check if the day is valid
bool is_birthday_valid(int year, int month, int day)
{
switch(month)
{
case 1:
if (day>0 && day<32)
return true;
else
return false;
break;
case 2:
if (is_leap_year(year))
{
if (day>0 && day<30)
return true;
else
return false;
}
else
{
if (day>0 && day<29)
return true;
else
return false;
}
break;
case 3:
if (day>0 && day<32)
return true;
else
return false;
break;
case 4:
if (day>0 && day<31)
return true;
else
return false;
break;
case 5:
if (day>0 && day<32)
return true;
else
return false;
break;
case 6:
if (day>0 && day<31)
return true;
else
return false;
break;
case 7:
if (day>0 && day<32)
return true;
else
return false;
break;
case 8:
if (day>0 && day<32)
return true;
else
return false;
break;
case 9:
if (day>0 && day<31)
return true;
else
return false;
break;
case 10:
if (day>0 && day<32)
return true;
else
return false;
break;
case 11:
if (day>0 && day<31)
return true;
else
return false;
break;
case 12:
if (day>0 && day<32)
return true;
else
return false;
break;
}
return true;
}
// calculate days after the given day, not including the given day (return 0 when given 12/31)
int calculate_days_after(bool leap, int month, int day)
{
int days=0;
int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (leap)
{
days_of_month[1]=29;
}
if (month<12)
for (int i=month;i<12;i++)
{
days+=days_of_month[i];
}
days+=(days_of_month[month-1]-day);
return days;
}
// calculate days before the given day, including the given day (return 366 when given a leap year's 12/31)
int calculate_days_before(bool leap, int month, int day)
{
int days=0;
int days_of_month[]={31,28,31,30,31,30,31,31,30,31,30,31};
if (leap)
{
days_of_month[1]=29;
}
for (int i=0;i<month-1;i++)
{
days+=days_of_month[i];
}
days+=day;
return days;
}
// calculate the number of years that are leap years between the startyear and endyear (including the startyear and endyear)
int calculate_leap_year(int startyear, int endyear)
{
if (startyear<1904)
{
if (endyear>1903)
return (endyear-1904)/4+1;
else
return 0;
}
else if (is_leap_year(startyear))
{
return (endyear-startyear)/4+1;
}
else if (is_leap_year(startyear+1))
{
return (endyear-startyear-1)/4+1;
}
else if (is_leap_year(startyear+2))
{
return (endyear-startyear-2)/4+1;
}
else if (is_leap_year(startyear+3))
{
return (endyear-startyear-3)/4+1;
}
return 0;
}
// calculate the total days in n years
int calculate_total_days(int startyear, int n, int month, int day)
{
return (n-1)*365+calculate_leap_year(startyear+1, startyear+n-1)+
calculate_days_after(is_leap_year(startyear),month,day)+calculate_days_before(is_leap_year(startyear+n),month,day);
}
int calculate_current_days(int startyear, int startmonth, int startday, int year, int month, int day)
{
return (year-startyear-1)*365+calculate_leap_year(startyear+1, year-1)+
calculate_days_after(is_leap_year(startyear),startmonth,startday)+calculate_days_before(is_leap_year(year),month,day);
}
int main()
{
// declaration
struct tm *date;
time_t now;
int year, month, day; // current time
int birthyear,birthmonth,birthday; // birthday
float total_days; // total days of n years
float current_days; // total days since birthday
int current_seconds; // time in 24 hours
int current_minutes;
int current_hours;
int n; // life span
// get current time
time(&now);
date = localtime(&now);
year = date->tm_year + 1900;
month = date->tm_mon + 1;
day = date->tm_mday;
// input data
int problemnum;
cout<<"Please input problem number(1-2):"<<endl;
do
cin>>problemnum; // select problem number
while(!(problemnum==1 || problemnum==2));
if (problemnum==1)
{
n=80; // set life span
cout<<"Please input the year you birth(1990-2000):"<<endl;
do
cin>>birthyear; // get birthyear
while(birthyear<1990 || birthyear>2000);
}
else
{
n=200; // set life span
cout<<"Please input the year you birth(1900-2000):"<<endl;
do
cin>>birthyear; // get birthyear
while(birthyear<1900 || birthyear>2000);
}
cout<<"Please input the month you birth:"<<endl;
do
cin>>birthmonth; // get birthmonth
while(birthmonth<1 || birthmonth>12);
cout<<"Please input the day you birth:"<<endl;
do
cin>>birthday; // get birthday
while(!is_birthday_valid(birthyear,birthmonth,birthday)); // check if the birthday is valid
// calculate the result
current_days=calculate_current_days(birthyear,birthmonth,birthday,year,month,day); // calculate total days since birthday
total_days=calculate_total_days(birthyear,n,birthmonth,birthday); // calculate total days of n years since birthday
current_seconds=(int)(current_days/total_days*3600*24); // calculate the time in 24 hours
current_minutes=current_seconds/60;
current_hours=current_minutes/60;
current_seconds=current_seconds%60;
current_minutes=current_minutes%60;
// output the result
cout<<"あなたの一生を24時間にたとえると今日は "<<current_hours<<":"<<current_minutes<<":"<<current_seconds<<endl<<endl;
system("PAUSE");
return 0;
}