/***************************************
*                                      *
*            経済要素の設定            *
*                                      *
*                                      *
***************************************/

#include <iostream>
#include <cstdlib>
#include <cmath>
#include "snaps.h"
#include "cntl.h"
#include "file_open.h"
#include "econ.h"

double round( double a , int n );

void econ()
{
	// 変数宣言
	double buffer[100];
	int data_number;
	int nendo;
	int counter;
	int nenrei;
	int c_nendo;
	double shinsai_ruiseki_kaiteiritu[SAISHUNENDO - ECON_SHONENDO + 1];
	double previous_buffer;
	double max_cut_rate[SAISHUNENDO - ECON_SHONENDO + 1];
	double base_up_real[SAISHUNENDO - ECON_SHONENDO + 1];
	double y[SAISHUNENDO - ECON_SHONENDO + 1];
	
	// 初期化
	previous_buffer = -1000.;
	
	// ファイルから読み込み
	while( ( read_data( buffer , fp_econ , &data_number ) != EOF ) && ( buffer[0] > previous_buffer ) )
	{
		nendo = (int)(buffer[0]) + 2000;
		cpi_up[nendo - ECON_SHONENDO] = 1. + buffer[6] / 100.;
		interest_rate[nendo - ECON_SHONENDO] = 1. + buffer[2] / 100. /* + buffer[6] / 100. （実質ベース）*/;
		base_up[nendo - ECON_SHONENDO] = 1. + buffer[4] / 100. /* + buffer[6] / 100. （実質ベース）*/;
		previous_buffer = buffer[0];
	}
	
	for( counter = nendo + 1 ; counter <= SAISHUNENDO ; counter++ )
	{
		cpi_up[counter - ECON_SHONENDO] = cpi_up[nendo - ECON_SHONENDO];
		base_up[counter - ECON_SHONENDO] = base_up[nendo - ECON_SHONENDO];
		interest_rate[counter - ECON_SHONENDO] = interest_rate[nendo - ECON_SHONENDO];
	}
	
	for( nendo = ECON_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		base_up_real[nendo - ECON_SHONENDO] = base_up[nendo - ECON_SHONENDO] / cpi_up[nendo - ECON_SHONENDO];
	}
	
	// 価格の改定
	base_up_k[2005 - ECON_SHONENDO] = 1.;
	for( nendo = 2006 ; nendo <= 2007 ; nendo++ )
	{
		base_up_k[nendo - ECON_SHONENDO] = cpi_up[nendo - 2 - ECON_SHONENDO];
	}
	for( nendo = 2008 ; nendo <= 2016 ; nendo++ )
	{
		y[nendo - ECON_SHONENDO]
		 = round( pow( base_up_real[nendo - 5 - ECON_SHONENDO] * base_up_real[nendo - 4 - ECON_SHONENDO] * base_up_real[nendo - 3 - ECON_SHONENDO] , 1. / 3. ) , 3 );
		base_up_k[nendo - ECON_SHONENDO] = round( cpi_up[nendo - 2 - ECON_SHONENDO] * y[nendo - ECON_SHONENDO] , 3 );
	}
	for( nendo = 2017 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		y[nendo - ECON_SHONENDO]
		 = pow( base_up_real[nendo - 5 - ECON_SHONENDO] * base_up_real[nendo - 4 - ECON_SHONENDO] * base_up_real[nendo - 3 - ECON_SHONENDO] , 1. / 3. );
		base_up_k[nendo - ECON_SHONENDO] = cpi_up[nendo - 2 - ECON_SHONENDO] * y[nendo - ECON_SHONENDO];
	}
	
	// 給付の改定
	// 初期化
	previous_buffer = -1000.;
	
	// 改定率ファイルの読込	
	while( ( read_data( buffer, fp_kaitei , &data_number ) != EOF ) && ( buffer[0] > previous_buffer ) )
	{
		nendo = (int)(buffer[0]) + 2000;
		for( nenrei = 1 ; nenrei <= MAX_JUKYU - UNDER_67 + 1 ; nenrei++ )
		{
			kaiteiritu[nendo - ECON_SHONENDO][nenrei - 1] = buffer[nenrei];
		}
		previous_buffer = buffer[0];
	}
	
	// 価格の設定
	shinsai_ruiseki_kaiteiritu[KAKAKU_NENDO - ECON_SHONENDO] = 1.;
	
	for( nendo = KAKAKU_NENDO + 1 ; nendo <= 2016 ; nendo++ )
	{
		shinsai_ruiseki_kaiteiritu[nendo - ECON_SHONENDO]
		 = round( shinsai_ruiseki_kaiteiritu[nendo - 1 - ECON_SHONENDO] * base_up_k[nendo - ECON_SHONENDO] , 3 );
	}
	for( nendo = 2017 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		shinsai_ruiseki_kaiteiritu[nendo - ECON_SHONENDO]
		 = shinsai_ruiseki_kaiteiritu[nendo - 1 - ECON_SHONENDO] * base_up_k[nendo - ECON_SHONENDO];
	}
	for( nendo = KAKAKU_NENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		kakaku[nendo - ECON_SHONENDO]
		 = shinsai_ruiseki_kaiteiritu[nendo - ECON_SHONENDO] / shinsai_ruiseki_kaiteiritu[KAKAKU_NENDO - ECON_SHONENDO];
	}
	for( nendo = ECON_SHONENDO ; nendo < KAKAKU_NENDO ; nendo++ )
	{
		kakaku[nendo - ECON_SHONENDO] = 1.;
	}
	
	
	if( suikei == 1 )
	{
		// 各年給付水準調整率の設定
		for( nendo = ECON_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			max_cut_rate[nendo - ECON_SHONENDO] = 1.;
		}
		
		// 単年最大スライド調整率ファイルの読み込み
		previous_buffer = -1000.;
		
		while( ( read_data( buffer , fp_cut_rate , &data_number ) != EOF ) && ( buffer[0] > previous_buffer ) )
		{
			nendo = (int)(buffer[0]);
			if( nendo >= ECON_SHONENDO )
			{
				max_cut_rate[nendo - ECON_SHONENDO] = 1. + buffer[1];
			}
			previous_buffer = buffer[0];
		}
		for( counter = nendo ; counter <= SAISHUNENDO ; counter++ )
		{
			max_cut_rate[counter - ECON_SHONENDO] = max_cut_rate[nendo - ECON_SHONENDO];
		}
		
		// 各年給付水準調整率の設定
		for( nendo = C_NENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
		{
			for( nenrei = UNDER_67 ; nenrei <= MAX_JUKYU ; nenrei++ )
			{
				pre_cut[nendo - ECON_SHONENDO][nenrei - UNDER_67]
				 = max( 1.0 , min( max_cut_rate[nendo - ECON_SHONENDO] , kaiteiritu[nendo - ECON_SHONENDO][nenrei - UNDER_67] ) );
			}
		}
		if( Option1 == 1 )
		{
			for( nendo = 2015 ; nendo <= SAISHUNENDO ; nendo++ )
			{
				for( nenrei = UNDER_67 ; nenrei <= MAX_JUKYU ; nenrei++ )
				{
					pre_cut[nendo - ECON_SHONENDO][nenrei - UNDER_67] = max( 1.0 , max_cut_rate[nendo - ECON_SHONENDO] );
				}
			}
		}
		
		// 累積給付水準調整率の設定
		for( counter = C_NENDO + 1 ; counter <= SAISHUNENDO ; counter++ )
		{
			for( nendo = counter ; nendo <= SAISHUNENDO ; nendo++ )
			{
				for( nenrei = UNDER_63 ; nenrei <= MAX_JUKYU ; nenrei++ )
				{
					for( c_nendo = counter ; c_nendo <= SAISHUNENDO ; c_nendo++ )
					{
						if( nenrei <= (int)( min( ( nendo + 67 - counter ) , MAX_JUKYU ) ) )
						{
							cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][nenrei - NENREI_SUM] /= pre_cut[counter - ECON_SHONENDO][UNDER_67 - UNDER_67];
						}
						else
						{
							cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][nenrei - NENREI_SUM]
							 /= pre_cut[counter - ECON_SHONENDO][nenrei - (int)min( ( nendo + 67 - counter ) , MAX_JUKYU ) ];
						}
					}
				}
			}
		}
	}
}

// 小数第 n + 1 位で四捨五入
double round( double a , int n )
{
 double  b;
 char    buf[256] = {'\0'};
 char    *p;
	
	sprintf( buf , "%.*f" , n , a );
	b = strtod( buf , &p );
	
	return b;
}

