#include <iostream>
#include "snaps.h"
#include "mkisosu.h"
#include "mfile_open.h"
#include "mcntl.h"
#include "mecon.h"
#include "mkiso.h"
#include "option.h"
#include <cmath>

void printout_kokunen_balance();

void printout_2()
{
	int nendo;
	int seido;
	int goubetu;
	int sotai_nendo;
	int shinkyu;
	int kubun;
	int tokubetu_shurui;
	double tumitateunyou;

	for( shinkyu = SUM ; shinkyu <= OLD ; shinkyu++ )
	{
		switch ( shinkyu )
		{
			case SUM :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（新法＋旧法）\n" );
				break;
			case NEW :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（新法）\n" );
				break;
			case OLD :
				fprintf( fp_out[KEKKA_a] , "基礎年金交付金\n" );
				break;
		}
		fprintf( fp_out[KEKKA_a] , ",合計,国年,厚年,国共,地共,私学\n");
		for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			sotai_nendo = nendo - SHONENDO;
			fprintf( fp_out[KEKKA_a] , "%15d," , nendo );
			for( seido = SUM ; seido <= CHIKYO ; seido++ )
			{
				fprintf( fp_out[KEKKA_a] , "%20.14le," , Kyufu[seido][sotai_nendo][NENREI_SUM - NENREI_SUM][shinkyu][SUM][SUM][SUM][SUM] );
			}
			fprintf( fp_out[KEKKA_a] , "%20.14le\n" , Kyufu[SHIGAKU][sotai_nendo][NENREI_SUM - NENREI_SUM][shinkyu][SUM][SUM][SUM][SUM] );
		}
	}
	
	for( kubun = SUM ; kubun <= IZOKU ; kubun++ )
	{
		switch ( kubun )
		{
			case SUM :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（合計）\n" );
				break;
			case ROREI :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（老齢）\n" );
				break;
			case SHOGAI :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（障害）\n" );
				break;
			case IZOKU :
				fprintf( fp_out[KEKKA_a] , "基礎年金給付費（遺族）\n" );
			break;
		}
		for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
		{
			sotai_nendo = nendo - SHONENDO;
			fprintf( fp_out[KEKKA_a] , "%15d," , nendo);
			for( seido = SUM ; seido <= CHIKYO ; seido++ )
			{
				fprintf( fp_out[KEKKA_a] , "%20.14le," , Kyufu[seido][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][kubun][SUM][SUM][SUM] );
			}
			fprintf( fp_out[KEKKA_a] , "%20.14le\n" , Kyufu[SHIGAKU][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][kubun][SUM][SUM][SUM] );
		}
	}	
	
	fprintf( fp_out[KEKKA_a] , "基礎年金拠出金\n");
	fprintf( fp_out[KEKKA_a] , ",単価,合計,国年,厚年,国共,地共,私学\n");
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		fprintf( fp_out[KEKKA_a] , "%15d," , nendo );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Tanka[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		for( seido = SUM ; seido <= SHIGAKU ; seido++ )
		{
			fprintf( fp_out[KEKKA_a] , "%20.14le," , Kyoshutukin[seido][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		}
		fprintf( fp_out[KEKKA_a] , "\n" );
	}	
	
	fprintf( fp_out[KEKKA_a] , "基礎年金拠出金（国庫）\n" );
	fprintf( fp_out[KEKKA_a] , ",単価,国庫負担割合(年度末),合計,国年,厚年,国共,地共,私学\n" );
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		fprintf( fp_out[KEKKA_a] , "%15d," , nendo );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Tanka_Kokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kokko_Wariai[nendo - ( SHONENDO - 1 )] );
		for( seido = SUM ; seido <= SHIGAKU ; seido++ )
		{
			fprintf( fp_out[KEKKA_a] , "%20.14le," , Kyoshutukin_Kokko[seido][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		}
		fprintf( fp_out[KEKKA_a] , "\n" );
	}
	
	fprintf( fp_out[KEKKA_a] , "拠出金算定対象者\n" );
	fprintf( fp_out[KEKKA_a] , ",合計,１号,厚年２号,厚年３号,国共２号,国共３号,地共２号,地共３号,私学２号,私学３号,産休免除者(1号再掲)\n" );

	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		fprintf( fp_out[KEKKA_a] , "%15d," , nendo );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , SanteiTaishou[SUM][sotai_nendo][SUM] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , SanteiTaishou[KOKUNEN][sotai_nendo][1] );
		for( seido = KOUNEN ; seido <= SHIGAKU ; seido++ )
		{
			for( goubetu = 2 ; goubetu <= 3 ; goubetu++ )
			{
				fprintf( fp_out[KEKKA_a] , "%20.14le," , SanteiTaishou[seido][sotai_nendo][goubetu] );
			}
		}
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Sankyu_Taishou[nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "\n" );
	}
	
	fprintf( fp_out[KEKKA_a] , "特別国庫負担内訳\n" );
	fprintf( fp_out[KEKKA_a] , ",合計,免除,嵩上げ（納付）,嵩上げ（免除）,老福下支え,５年年金,２０歳前\n" );
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		fprintf( fp_out[KEKKA_a] , "%15d,", nendo );
		for( tokubetu_shurui = SUM ; tokubetu_shurui <= TOKUBETU_20MAE ; tokubetu_shurui++ )
		{
			fprintf( fp_out[KEKKA_a] , "%20.14le,", Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][tokubetu_shurui][SUM][SUM] );
		}
		fprintf( fp_out[KEKKA_a] , "\n");
	}
	
	fprintf( fp_out[KEKKA_a] , "独自給付費等\n" );
	fprintf( fp_out[KEKKA_a] , ",一時金納付分,一時金付加分,新法寡婦年金,旧法寡婦年金免除分以外," );
	fprintf( fp_out[KEKKA_a] , "旧法寡婦年金免除分,付加年金,付加年金（人数）,１号被保険者数,住宅融資債権,業務勘定への繰入\n" );
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		fprintf( fp_out[KEKKA_a] , "%d," , nendo );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Ichijikin_Cut[nendo - SHONENDO][NOUFU] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Ichijikin_Cut[nendo - SHONENDO][FUKA] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][NEW] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][OLD_NOUFU] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][OLD_MENJO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Fuka[nendo - SHONENDO][SUM] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Fuka_Ninzu[nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Hiho_Kokunen[nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Yuushi_Saiken[nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le\n" , Fukushi[nendo - SHONENDO] );
	}
	
	fprintf( fp_out[KEKKA_a] , "経済前提\n" );
	fprintf( fp_out[KEKKA_a] , ",物価上昇率,賃金上昇率,運用利回り,改定率（マクロ込み、67歳）,改定率（マクロ込み、68歳）,改定率（マクロ込み、88歳）,特別調整率（67歳）,特別調整率（68歳）,特別調整率（88歳）\n" );
	for( nendo = ECON_SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		fprintf( fp_out[KEKKA_a] , "%d," , nendo );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , cpi_up[nendo - ECON_SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , base_up[nendo - ECON_SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , interest_rate[nendo - ECON_SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , kaiteiritu[nendo - ECON_SHONENDO][68 - UNDER_67] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , kaiteiritu[nendo - ECON_SHONENDO][88 - UNDER_67] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , T[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , T[nendo - ECON_SHONENDO][68 - UNDER_67] );
		fprintf( fp_out[KEKKA_a] , "%20.14le\n" , T[nendo - ECON_SHONENDO][88 - UNDER_67] );
	}
	
	fprintf( fp_out[KEKKA_a] , "妻積（各制度への分配額）\n" );
	fprintf( fp_out[KEKKA_a] , ",国年,厚年,国共,地共,私学,妻積残額,国年積立金,国年保険料\n" );
	
	fprintf( fp_out[KEKKA_a] , "%d（原価）," , TUMATUMI_NENDO - 1 );
	fprintf( fp_out[KEKKA_a] , "%20.14e," , 0 );
	for( seido = KOUNEN ; seido <= SHIGAKU ; seido++ )
	{
		fprintf( fp_out[KEKKA_a] , "%20.14e," , 0 );
	}
	fprintf( fp_out[KEKKA_a] , "%20.14e\n" , Tumatumi_2014[SUM] );
	
	for( nendo = TUMATUMI_NENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		fprintf( fp_out[KEKKA_a] , "%d," , nendo );
		for( seido = KOKUNEN ; seido <= SHIGAKU ; seido++ )
		{
			fprintf( fp_out[KEKKA_a] , "%20.14e," , Tumatumi[seido][nendo - SHONENDO] );
		}
		fprintf( fp_out[KEKKA_a] , "%20.14e," , Tumatumi[SUM][nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14e," , Tumitate[nendo - SHONENDO] );
		fprintf( fp_out[KEKKA_a] , "%20.14e\n" , Hokenryou_y[nendo - SHONENDO] );
	}

	fprintf( fp_out[KEKKA_a] , "\n");
	fprintf( fp_out[KEKKA_a] , "試算番号," );
	fprintf( fp_out[KEKKA_a] , "%s-%sa," , Version, Version_cut );
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO - 5 ; nendo++ )
	{
		if( Tumitate[nendo - SHONENDO] < 0.)
		{
			printf("国年が%d年度に枯渇しました\n",nendo);
			fprintf( fp_out[KEKKA_a] , "国年が%d年度に枯渇しました" , nendo );

			fprintf( fp_out[OUTPUT] , "%d," , nendo );
			break;
		}
	}
	fprintf( fp_out[KEKKA_a] , "\n" );
	fprintf( fp_out[OUTPUT] , "\n" );
	
	fprintf( fp_out[KEKKA_a] , "カット率," );
	fprintf( fp_out[KEKKA_a] , "%20.14e\n" , cut_ruiseki[S_C_NENDO - ECON_SHONENDO][S_C_NENDO - ECON_SHONENDO][UNDER_63 - NENREI_SUM] );
	
	fprintf( fp_out[KEKKA_a] , "カット終了年度," );
	fprintf( fp_out[KEKKA_a] , "%d\n" ,S_C_NENDO );
	
	fprintf( fp_out[KEKKA_KOKKO_a] , "年度,変更月,拠出金算定対象者数,年度末国庫負担割合,拠出金（計）,拠出金（前）,拠出金（後）,拠出金国庫（計）,");
	fprintf( fp_out[KEKKA_KOKKO_a] , "拠出金国庫（前）,拠出金国庫（後）,特別国庫（計）,特別国庫（前）,特別国庫（後）,２０前障害（計）（再）,");
	fprintf( fp_out[KEKKA_KOKKO_a] , "２０前障害（前）（再）,２０前障害（後）（再）\n");
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		fprintf( fp_out[KEKKA_KOKKO_a] , "%15d," , nendo );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%15d," , tuki[sotai_nendo] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , SanteiTaishou[SUM][sotai_nendo][SUM] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kokko_Wariai[nendo - ( SHONENDO - 1 )] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][ZENHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][KOUHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin_Kokko[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin_Kokko[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][ZENHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Kyoshutukin_Kokko[SUM][sotai_nendo][NENREI_SUM - NENREI_SUM][KOUHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][SUM] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][ZENHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][KOUHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][TOKUBETU_20MAE][SUM][SUM] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][TOKUBETU_20MAE][SUM][ZENHAN] );
		fprintf( fp_out[KEKKA_KOKKO_a] , "%20.14le\n" , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][TOKUBETU_20MAE][SUM][KOUHAN] );
	}

	fprintf(fp_out[KEKKA_a],"\n");
	fprintf(fp_out[KEKKA_a],"shu-shi shuturyoku,\n");

	int shunnyu_cnt=12;
	int shishutu_cnt=10 ;
	int sonota_cnt=1;

	int i=0;

	char *head[]
		={"保険料月額","保険料収入（国年）","保険料収入（付加年金）","運用収入"
		,"国庫（基礎年金）","国庫（特別国庫）","国庫(一時金付加金）","国庫（付加年金）","国庫（旧法寡婦年金免除分）"
		,"住宅融資債権","妻積み"," ",
		"支出合計","一時金","一時金付加金","新法寡婦年金","旧法寡婦年金免除分以外","旧法寡婦年金免除分","付加年金"
		,"基礎年金拠出金", "基礎年金拠出金（特別国庫）","業務勘定への繰入"
		,"年度末積立金"
		};
	
	fprintf(fp_out[KEKKA_a],"年度,");
	for(i = 0 ; i < shunnyu_cnt + shishutu_cnt + sonota_cnt ; i++)
	{
		fprintf(fp_out[KEKKA_a],"%s,",head[i]);
	}
	fprintf(fp_out[KEKKA_a],"\n");

	for(nendo=SHONENDO ; nendo <=SAISHUNENDO ; nendo++)
	{
		sotai_nendo = nendo - SHONENDO;

		fprintf(fp_out[KEKKA_a],"%d,%20.14le,%20.14le,%20.14le,"
				,nendo,Hokenryou_m[sotai_nendo][0] ,Hokenryou_y[sotai_nendo],Fuka_Hokenryou_y[sotai_nendo]);

		tumitateunyou
		 = Tumitate[nendo - 1 - SHONENDO] * (interest_rate[nendo - ECON_SHONENDO]-1) +
		         + ( Hokenryou_y[nendo - SHONENDO] + Fuka_Hokenryou_y[nendo - SHONENDO] - Kyoshutukin[KOKUNEN][nendo - SHONENDO][NENREI_SUM - NENREI_SUM][SUM]
		         + Kyoshutukin_Kokko[KOKUNEN][nendo - SHONENDO][NENREI_SUM - NENREI_SUM][SUM] - Fukushi[nendo - SHONENDO]
		         - Ichijikin_Cut[nendo - SHONENDO][NOUFU] - Ichijikin_Cut[nendo - SHONENDO][FUKA] * 3. / 4. - Kafu_Cut[nendo - SHONENDO][NEW]
		         - Kafu_Cut[nendo - SHONENDO][OLD_NOUFU] - Fuka[nendo - SHONENDO][SUM] * 3. / 4. + Yuushi_Saiken[nendo - SHONENDO])
                         * (pow( interest_rate[nendo - ECON_SHONENDO] , 1. / 2. ) -1 );

		if( nendo >= TUMATUMI_NENDO )
		{
			tumitateunyou += Tumatumi[KOKUNEN][nendo - SHONENDO] * (pow( interest_rate[nendo - ECON_SHONENDO] , ( nendo == TUMATUMI_NENDO ? 2. / 12. : 1. / 2.) ) -1);
		} 
		fprintf(fp_out[KEKKA_a],"%20.14le,",tumitateunyou);

		fprintf(fp_out[KEKKA_a],"%20.14le,%20.14le,%20.14le,%20.14le,%20.14le,"
			,Kyoshutukin_Kokko[KOKUNEN][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM], Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][SUM],
			Ichijikin_Cut[nendo - SHONENDO][FUKA]/4.0, Fuka[nendo - SHONENDO][SUM]/4.0, Kafu_Cut[nendo - SHONENDO][OLD_MENJO]);
			
		fprintf(fp_out[KEKKA_a],"%20.14le,%20.14le,"
				,Yuushi_Saiken[sotai_nendo] ,Tumatumi[KOKUNEN][sotai_nendo]);

		fprintf(fp_out[KEKKA_a]," ," );

		fprintf( fp_out[KEKKA_a] , "%20.14le," , 
			Ichijikin_Cut[nendo - SHONENDO][NOUFU] + Ichijikin_Cut[nendo - SHONENDO][FUKA] + Kafu_Cut[nendo - SHONENDO][NEW] + Kafu_Cut[nendo - SHONENDO][OLD_NOUFU]
			+Kafu_Cut[nendo - SHONENDO][OLD_MENJO] + Fuka[nendo - SHONENDO][SUM] + Kyoshutukin[KOKUNEN][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM]
			+Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][SUM] + Fukushi[nendo - SHONENDO]);

		fprintf( fp_out[KEKKA_a] , "%20.14le," , Ichijikin_Cut[nendo - SHONENDO][NOUFU] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Ichijikin_Cut[nendo - SHONENDO][FUKA] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][NEW] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][OLD_NOUFU] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kafu_Cut[nendo - SHONENDO][OLD_MENJO] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Fuka[nendo - SHONENDO][SUM] );

		fprintf( fp_out[KEKKA_a] , "%20.14le," , Kyoshutukin[KOKUNEN][sotai_nendo][NENREI_SUM - NENREI_SUM][SUM] );
		fprintf( fp_out[KEKKA_a] , "%20.14le," , Tokubetukokko[sotai_nendo][NENREI_SUM - NENREI_SUM][SUM][SUM][SUM] );

		fprintf( fp_out[KEKKA_a] , "%20.14le," , Fukushi[nendo - SHONENDO] );

		fprintf( fp_out[KEKKA_a] , "%20.14e," , Tumitate[nendo - SHONENDO] );

		fprintf(fp_out[KEKKA_a],"%20.14le,", kakaku[nendo-ECON_SHONENDO]);

		fprintf(fp_out[KEKKA_a],",\n");

	}
}


