/***************************************
*                                      *
*        給付水準調整用頭割り計算      *
*                                      *
*                                      *
***************************************/

#include <iostream>
#include "snaps.h"
#include "cntl.h"
#include "kisosu.h"
#include "kiso.h"
#include "econ.h"


void Atamawari_cut( int c_nendo )
{
	// 変数宣言
	int nendo;
	int nenrei;
	int sotai_nendo;
	int counter;
	int shikyu_keitai;
	int kikan;
	double Kyufu_Cut[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kyufu_Cut_Nendomatu[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kyufu_Cut_Nendomatu_P[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kokko_Cut[SAISHUNENDO - SHONENDO + 1][3];
	double Tokubetukokko_Cut_Nendomatu[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Tokubetukokko_Cut_Nendomatu_P[SAISHUNENDO - SHONENDO + 1][MAX_JUKYU - NENREI_SUM + 1][3][3];
	double Kafu_Cut_Nendomatu[SAISHUNENDO - SHONENDO + 1][4];
	
	#define SHIHARAIOKURE 2
	
	// ゼロクリア
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		for( nenrei = NENREI_SUM ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = SUM ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = SUM ; kikan <= KOUHAN ; kikan++ )
				{
					Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Kyufu_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
					Tokubetukokko_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] = 0.;
				}
			}
			Kyoshutukin_Cut[sotai_nendo][nenrei - NENREI_SUM] = 0.;
			Kyoshutukin_Kokko_Cut[sotai_nendo][nenrei - NENREI_SUM] = 0.;
		}
		for( kikan = SUM ; kikan <= KOUHAN ; kikan++ )
		{
			Kokko_Cut[sotai_nendo][kikan] = 0.;
		}
	}
	
	// 給付水準調整
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		for( nenrei = UNDER_63 ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = KIHON ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = ZENHAN ; kikan <= KOUHAN ; kikan++ )
				{
					Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Kyufu_Nendomatu[SUM][sotai_nendo][nenrei - NENREI_SUM][SUM][SUM][KYOSHUTU][shikyu_keitai][kikan]
					    * cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63  - NENREI_SUM : nenrei  - NENREI_SUM )];
					
					Kyufu_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Kyufu_Nendomatu_P[SUM][sotai_nendo][nenrei - NENREI_SUM][SUM][SUM][KYOSHUTU][shikyu_keitai][kikan]
					    * cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63  - NENREI_SUM : nenrei  - NENREI_SUM )];
					
					Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Tokubetukokko_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][SUM][shikyu_keitai][kikan]
					    * cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63  - NENREI_SUM : nenrei  - NENREI_SUM )];
					
					Tokubetukokko_Cut_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
					 = Tokubetukokko_Nendomatu_P[sotai_nendo][nenrei - NENREI_SUM][SUM][shikyu_keitai][kikan]
					    * cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_63  - NENREI_SUM : nenrei  - NENREI_SUM )];
				}
			}
		}
	}
	
	
	// 年度間値
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		for( nenrei = UNDER_64 ; nenrei <= MAX_JUKYU ; nenrei++ )
		{
			for( shikyu_keitai = KIHON ; shikyu_keitai <= KAKYU ; shikyu_keitai++ )
			{
				for( kikan = ZENHAN ; kikan <= KOUHAN ; kikan++ )
				{
					if( nenrei == UNDER_64 )
					{
						if( nendo == 2013 )
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6. ) / 12.
							 + ( Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] ) * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6. ) / 12.
							 + ( Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] ) * ( 6. - SHIHARAIOKURE ) / 12.;
						}
						else
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
							 + ( Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] ) * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
							 + ( Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] ) * ( 6. - SHIHARAIOKURE ) / 12.;
						}
					}
					else if( nenrei <= 67 )
					{
						if( nendo == 2013 )
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6. ) / 12.
							 + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6. ) / 12.
							 + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
						}
						else
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
							 + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
							 + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
						}
					}
					else
					{
						if( nendo == 2013 )
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_67 - UNDER_67 : nenrei - UNDER_67 )] + 1.0 / 100 ) * 6. ) / 12.
							 + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_67 - UNDER_67 : nenrei - UNDER_67 )] + 1.0 / 100 ) * 6. ) / 12.
							 + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
						}
						else
						{
							Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Kyufu_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_67 - UNDER_67 : nenrei - UNDER_67 )] * 6. ) / 12.
							 + Kyufu_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
							
							Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan]
							 = Tokubetukokko_Cut_Nendomatu_P[sotai_nendo - 1][nenrei - 1 - NENREI_SUM][shikyu_keitai][kikan]
							   * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][( shikyu_keitai == KAKYU ? UNDER_67 - UNDER_67 : nenrei - UNDER_67 )] * 6. ) / 12.
							 + Tokubetukokko_Cut_Nendomatu[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan] * ( 6. - SHIHARAIOKURE ) / 12.;
						}
					}
					Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][kikan] += Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
					Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM] += Kyufu_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
					Tokubetukokko_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][kikan] += Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
					Tokubetukokko_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM] += Tokubetukokko_Cut[sotai_nendo][nenrei - NENREI_SUM][shikyu_keitai][kikan];
				}
			}
		}
		Kokko_Cut[sotai_nendo][ZENHAN]
		 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][ZENHAN] * K_Wariai[KOKUNEN][nendo - 1 - ( SHONENDO - 1 )];
		
		Kokko_Cut[sotai_nendo][KOUHAN]
		 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][KOUHAN] * K_Wariai[KOKUNEN][nendo - ( SHONENDO - 1 )];
		
		Kokko_Cut[sotai_nendo][SUM]
		 = Kokko_Cut[sotai_nendo][ZENHAN] + Kokko_Cut[sotai_nendo][KOUHAN];
	}
	
	// 拠出金の按分
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		if( SanteiTaishou[SUM][sotai_nendo][SUM] > 0. )
		{
			Kyoshutukin_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM]
			 = Kyufu_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM] * SanteiTaishou[KOKUNEN][sotai_nendo][SUM] / SanteiTaishou[SUM][sotai_nendo][SUM];
			Kyoshutukin_Kokko_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM]
			 = Kokko_Cut[sotai_nendo][SUM] * SanteiTaishou[KOKUNEN][sotai_nendo][SUM] / SanteiTaishou[SUM][sotai_nendo][SUM];
		}
		else
		{
			Kyoshutukin_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM] = 0.;
			Kyoshutukin_Kokko_Cut[sotai_nendo][NENREI_SUM - NENREI_SUM] = 0.;
		}
	}
	
	// 寡婦年金（給付水準調整後）
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		for( counter = SUM ; counter <= OLD_MENJO ; counter++ )
		{
			Kafu_Cut_Nendomatu[nendo - SHONENDO][counter]
			 = Kafu_Nendomatu[sotai_nendo][counter] * cut_ruiseki[c_nendo - ECON_SHONENDO][nendo - ECON_SHONENDO][UNDER_64 - NENREI_SUM];
		}
	}
	
	// 年度間値
	for( nendo = SHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		for( counter = SUM ; counter <= OLD_MENJO ; counter++ )
		{
			if( nendo == 2013 )
			{
				Kafu_Cut[sotai_nendo][counter]
				 = Kafu_Cut_Nendomatu[sotai_nendo - 1][counter]
				 * ( SHIHARAIOKURE + ( kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + 1.0 / 100 ) * 6. ) / 12.
				 + Kafu_Cut_Nendomatu[sotai_nendo][counter] * ( 6. - SHIHARAIOKURE ) / 12.;
			}
			else
			{
				Kafu_Cut[sotai_nendo][counter]
				 = Kafu_Cut_Nendomatu[sotai_nendo - 1][counter]
				 * ( SHIHARAIOKURE + kaiteiritu_cut[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * 6. ) / 12.
				 + Kafu_Cut_Nendomatu[sotai_nendo][counter] * ( 6. - SHIHARAIOKURE ) / 12.;
			}
		}
	}
}
