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

void read_file()
{
	#define SHIHARAIOKURE 2
	
	double buffer[DATA_MAX];
	double previous_buffer0;
	int nendo;
	int data_number;
	int counter;
	int sotai_nendo;
	
	using namespace std;
	
	for( nendo = FUKUSHI_NENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fukushi[nendo - SHONENDO] = Fukushi[FUKUSHI_NENDO - SHONENDO];
	}

	for( nendo = FUKUSHI_NENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fukushi[nendo - SHONENDO] *= ( Hiho_Kokunen[nendo - SHONENDO] / Hiho_Kokunen[FUKUSHI_NENDO - SHONENDO] );
	}

	previous_buffer0 = -1000.;

	read_headder( fp_in[DOKUZI] );
	
	while( ( read_data( buffer , fp_in[DOKUZI] , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO && nendo <= SAISHUNENDO )
		{
			Fuka_Ninzu[nendo - SHONENDO] = buffer[1];
			Fuka_Nendomatu[nendo - SHONENDO][NEW] = buffer[2];
			Fuka_Nendomatu[nendo - SHONENDO][OLD_ROREI] = buffer[3];
			Fuka_Nendomatu[nendo - SHONENDO][OLD_TURO] = buffer[4];

			Kafu_Nendomatu[nendo - SHONENDO][NEW] = buffer[5];
			Kafu_Nendomatu[nendo - SHONENDO][OLD_NOUFU] = buffer[6];
			Kafu_Nendomatu[nendo - SHONENDO][OLD_MENJO] = buffer[7];

			Ichijikin_Nendomatu[nendo - SHONENDO][NOUFU] = buffer[8];
			Ichijikin_Nendomatu[nendo - SHONENDO][FUKA] = buffer[9];
		}
		previous_buffer0 = buffer[0];
	}
	for( nendo = SHONENDO ; nendo <= SAISHUNENDO ; nendo++ )
	{
		Fuka_Nendomatu[nendo - SHONENDO][SUM]
		 = Fuka_Nendomatu[nendo - SHONENDO][NEW] + Fuka_Nendomatu[nendo - SHONENDO][OLD_ROREI] + Fuka_Nendomatu[nendo - SHONENDO][OLD_TURO];
		Kafu_Nendomatu[nendo - SHONENDO][SUM]
		 = Kafu_Nendomatu[nendo - SHONENDO][NEW] + Kafu_Nendomatu[nendo - SHONENDO][OLD_NOUFU] + Kafu_Nendomatu[nendo - SHONENDO][OLD_MENJO];
		Ichijikin_Nendomatu[nendo - SHONENDO][SUM] = Ichijikin_Nendomatu[nendo - SHONENDO][NOUFU] + Ichijikin_Nendomatu[nendo - SHONENDO][FUKA];
	}
	fclose(fp_in[DOKUZI]);
	
	previous_buffer0 = -1000.;
	
	while( ( read_data( buffer , fp_in[YUUSHI] , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO && nendo <= SAISHUNENDO )
		{
			Yuushi_Saiken[nendo - SHONENDO] = buffer[1] * 1000000.;

		}
		previous_buffer0 = buffer[0];
	}
	fclose( fp_in[YUUSHI] );
	
	previous_buffer0 = -1000.;
	
	while( ( read_data( buffer , fp_in[HOKEN] , &data_number ) != EOF ) && ( buffer[0] > previous_buffer0 ) )
	{
		nendo = (int)(buffer[0]);
		if( nendo >= SHONENDO )
		{
			Hokenryou_m[nendo - SHONENDO][0] = buffer[1];
			Hokenryou_m[nendo - SHONENDO][1] = buffer[2];
			Fuka_Hokenryou_m[nendo - SHONENDO] = buffer[3];
		}
		previous_buffer0 = buffer[0];
		
		if( Hokenryou_m[nendo - SHONENDO - 1][0] < Hokenryou_m[nendo - SHONENDO][0] )
		{
			h_nendo = nendo;
		}
	}
	cout << "保険料引き上げ最終年度 " << h_nendo << endl;
	fclose( fp_in[HOKEN] );
	
	for( nendo = SUIKEISHONENDO + 1 ; nendo <= SAISHUNENDO ; nendo++ )
	{
		sotai_nendo = nendo - SHONENDO;
		
		if( nendo == 2013 )
		{
			Ichijikin[sotai_nendo][NOUFU]
			 = Ichijikin_Nendomatu[sotai_nendo - 1][NOUFU] * ( SHIHARAIOKURE + ( ICHIJIKIN_KAITEI == 1 ? kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + zoubun : 1. ) * 6. ) / 12.
			     + Ichijikin_Nendomatu[sotai_nendo][NOUFU] * ( 6 - SHIHARAIOKURE ) / 12.;
			Ichijikin[sotai_nendo][FUKA]
			 = Ichijikin_Nendomatu[sotai_nendo - 1][FUKA] * ( SHIHARAIOKURE + 6 ) / 12. + Ichijikin_Nendomatu[sotai_nendo][FUKA] * ( 6 - SHIHARAIOKURE ) / 12.;
		}
		else
		{
			Ichijikin[sotai_nendo][NOUFU]
			 = Ichijikin_Nendomatu[sotai_nendo - 1][NOUFU] * ( SHIHARAIOKURE + ( ICHIJIKIN_KAITEI == 1 ? kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] : 1. ) * 6. ) / 12.
			     + Ichijikin_Nendomatu[sotai_nendo][NOUFU] * ( 6 - SHIHARAIOKURE ) / 12.;
			Ichijikin[sotai_nendo][FUKA]
			 = Ichijikin_Nendomatu[sotai_nendo - 1][FUKA] * ( SHIHARAIOKURE + 6 ) / 12. + Ichijikin_Nendomatu[sotai_nendo][FUKA] * ( 6 - SHIHARAIOKURE ) / 12.;
		}
		Ichijikin[sotai_nendo][SUM] = Ichijikin[sotai_nendo][NOUFU] + Ichijikin[sotai_nendo][FUKA];
		
		for( counter = SUM ; counter <= OLD_MENJO ; counter++ )
		{
			if( nendo == 2013 )
			{
				Kafu[sotai_nendo][counter]
				 = Kafu_Nendomatu[sotai_nendo - 1][counter] * ( SHIHARAIOKURE + ( kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] + zoubun ) * 6 ) / 12.
				    + Kafu_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
			}
			else
			{
				Kafu[sotai_nendo][counter]
				 = Kafu_Nendomatu[sotai_nendo - 1][counter] * ( SHIHARAIOKURE + kaiteiritu[nendo - ECON_SHONENDO][UNDER_67 - UNDER_67] * ( nendo == 2015 ? Cut_2015 : 1. ) * 6 ) / 12.
				    + Kafu_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
			}
		}
		for( counter = SUM ; counter <= OLD_TURO ; counter++ )
		{
			Fuka[sotai_nendo][counter]
			 = Fuka_Nendomatu[sotai_nendo - 1][counter] * ( SHIHARAIOKURE + 6 ) / 12. + Fuka_Nendomatu[sotai_nendo][counter] * ( 6 - SHIHARAIOKURE ) / 12.;
		}
	}
}
