Into the Horizon

programming, photography, and daily log

"GO FOR IT" 問題(1)「人生の時計」

#include <iostream>
#include <time.h>
using namespace std;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;


static int GetDays(int y, int m, int d)
{
	//1年1月1日からの日数を返す
	// 1・2月 → 前年の13・14月
	if (m <= 2)
	{
		--y;
		m += 12;
	}
	int dy = 365 * (y - 1); // 経過年数×365日
	int c = y / 100;
	int dl = (y >> 2) - c + (c >> 2); // うるう年分
	int dm = (m * 979 - 1033) >> 5; // 1月1日から m 月1日までの日数
	return dy + dl + dm + d - 1;
}

int main () {
	//input
	int a,b,c,n,year,month,day;
	cin>>a>>b>>c>>n;
	time_t now = time(NULL);
	struct tm *pnow = localtime(&now);
	year = pnow->tm_year+1900;
	month = pnow->tm_mon + 1;
	day = pnow->tm_mday;
	
	int x,y,z;
	x=GetDays(a,b,c); y=GetDays(a+n,b,c); z=GetDays(year, month, day); //1年1月1日からの x)誕生日までの日数 y)死亡する日までの日数 z)今日までの日数
	double ratio = (double) (z-x)/(y-x);
	int hour,min,second = (int) 24*60*60*ratio;	
	hour = second/(60*60); second%=60*60;
	min = second/60; second%=60;
	
	//output
	cout<<"birthday:\t"<<a<<"/"<<b<<"/"<<c<<endl;
	cout<<"today:  \t"<<year<<"/"<<month<<"/"<<day<<endl;
	cout<<"life span:\t"<<n<<endl;
	cout<<"answer: \t"<<hour<<":"<<min<<":"<<second<<endl;
}