/*************************************
*                                    *
*    外枠システム  simlkyos.c        *
*  （共済２号被保険者数の推計）      *
*                                    *
*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <math.h>
#include "set.h"
#include "cntl.h"
#include "kyos.h"
#include "file_open.h"


void simlkyos(void) { /* 現行３共済の推計 */

  for ( seido=0; seido<=7; seido++ ){
    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      if (( seido==0 ) || ( seido==3 ) || ( seido>=4 ) ) {  
          if ( nendo<= 2001 ) {
              xend[seido][nendo-STARTY] = 65 ;   
              if ( seido==7 ) {
                  xend[seido][nendo-STARTY] = 75 ;  
              }
          }
          else {
              xend[seido][nendo-STARTY] = 70 ; 
          }
      }
      else {
          if ( nendo >= 2015 ) {  
              xend[seido][nendo-STARTY] = 70 ; 
          }
          else {
              xend[seido][nendo-STARTY] = 75 ; 
          }
      }
    }
  }



/* 共済データの処理（国共済） */

    if ( ( KYOREQK == 1 ) || ( KYOREQK == 2 ) ) { 


        seido = 1;
        readkyos_1( seido, KYOREQBK );

        simlkyos_1_kok() ;

    }



/* 共済データの処理（地共済） */

    if ( ( KYOREQR == 1 ) || ( KYOREQR == 2 ) ) {  

        seido = 2;
        readkyos_1( seido, KYOREQBR );
        simlkyos_1_ren() ;
    }



/* 共済データの処理（私学共済） */

    if ( KYOREQS == 1 ) { 

        seido = 3;
        readkyos_1( seido, KYOREQBS );

        seido = 3;
        simlkyos_1_sig() ;
    }



    for ( seido=1; seido<=3; seido++ ) {
      for ( nendo=2000; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          if (( seido == 3 ) && ( nendo >= 2005 )) { 
            for ( nenrei=70; nenrei<=100; nenrei++ ) {
              kyosai[seido][nendo-STARTY][sei][69] += kyosai[seido][nendo-STARTY][sei][nenrei] ;
              kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
            }
          }
          else if ( nendo >= 2015 ) { 
            for ( nenrei=70; nenrei<=100; nenrei++ ) {
              kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
            }
          }
        }
      }
    }



  for ( seido=1; seido<=3; seido++ ) {
    for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {

        if ( koyouj_c[1][nendo-STARTY][2][nenrei] > 0.0 ) {
          kyosai[seido][nendo-STARTY][3][nenrei]
            = kyosai[seido][nendo-STARTY][2][nenrei]
               * (koyouj_c[1][nendo-STARTY][3][nenrei]+koyouj_c[2][nendo-STARTY][3][nenrei]) / (koyouj_c[1][nendo-STARTY][2][nenrei]+koyouj_c[2][nendo-STARTY][2][nenrei]) ;
          kyosai[seido][nendo-STARTY][4][nenrei]
            = kyosai[seido][nendo-STARTY][2][nenrei]
               * (koyouj_c[1][nendo-STARTY][4][nenrei]+koyouj_c[2][nendo-STARTY][4][nenrei]) / (koyouj_c[1][nendo-STARTY][2][nenrei]+koyouj_c[2][nendo-STARTY][2][nenrei]) ;
        }
        /* 男女計 */
        kyosai[seido][nendo-STARTY][0][nenrei]
          = kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
      }
    }
  }



}


void simlkyos_1_kok(void) { /* 国共済 */

  /*  変数定義  */
  double TMP ;
  double TMQ ;
  double TMR ;
  double seisan_wari[ENDY-STARTY+1] ;



    if ( KE >= FJINKOY ) {
        /*  変数ゼロクリア  */
        TMP = 0.0 ; TMQ = 0.0 ; TMR = 0.0 ;
        for ( nendo=FJINKOY-1; nendo<=KE+1; nendo++ ) {
          seisan_wari[nendo-STARTY] = 0.0 ;
        }
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            TMP += kyosai[seido][FJINKOY-1-STARTY][sei][nenrei] ;
            TMQ += kyosai[seido][FJINKOY-STARTY][sei][nenrei] ;
          }
        }

        if ( ( KYOREQK == 1 ) && 
             ( TMP * sojinko_c[FJINKOY-1-STARTY][0] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )){
            seisan_wari[FJINKOY-1-STARTY] = TMP / sojinko_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / sojinko_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }
        else if ( ( KYOREQK == 2 ) && 
             ( TMP * seisanj_c[FJINKOY-1-STARTY][0] * seisanj_c[FJINKOY-STARTY][0] > 0.0 ) ) {
            seisan_wari[FJINKOY-1-STARTY] = TMP / seisanj_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / seisanj_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
            seisan_wari[nendo-STARTY] = seisan_wari[nendo-1-STARTY] * TMR ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
          for ( sei=1; sei<=2; sei++ ) {
            for ( nenrei=15; nenrei<=100; nenrei++ ) {

              if ( ( KYOREQK == 1 )  && 
                  ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * sojinko_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] ) ;
              }
              else if ( ( KYOREQK == 2 )  && 
                  ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] > 0.0 ) ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * seisanj_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] ) ;
              }
              else {
                if (( sei==1 ) && ( nenrei==15 )) {
                  printf("共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                  fprintf(stderr, "共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                }
                kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
              }
            }
          }
        }
    }


}



void simlkyos_1_ren(void) { /* 地共済 */

  /*  変数定義  */
  double TMP ;
  double TMQ ;
  double TMR ;
  double seisan_wari[ENDY-STARTY+1] ;


    if ( KE > FJINKOY ) {
        TMP = 0.0 ; TMQ = 0.0 ; TMR = 0.0 ;
        for ( nendo=FJINKOY-1; nendo<=KE+1; nendo++ ) {
          seisan_wari[nendo-STARTY] = 0.0 ;
        }
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            TMP += kyosai[seido][FJINKOY-1-STARTY][sei][nenrei] ;
            TMQ += kyosai[seido][FJINKOY-STARTY][sei][nenrei] ;
          }
        }

        if ( ( KYOREQR == 1 )  && 
             ( TMP * sojinko_c[FJINKOY-1-STARTY][0] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )){
            seisan_wari[FJINKOY-1-STARTY] = TMP / sojinko_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / sojinko_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }
        else if ( ( KYOREQR == 2 ) && 
             ( TMP * seisanj_c[FJINKOY-1-STARTY][0] * seisanj_c[FJINKOY-STARTY][0] > 0.0 ) ) {
            seisan_wari[FJINKOY-1-STARTY] = TMP / seisanj_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / seisanj_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
            seisan_wari[nendo-STARTY] = seisan_wari[nendo-1-STARTY] * TMR ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
          for ( sei=1; sei<=2; sei++ ) {
            for ( nenrei=15; nenrei<=100; nenrei++ ) {

              if ( ( KYOREQR == 1 ) && 
                   ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * sojinko_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] ) ;
              }
              else if ( ( KYOREQR == 2 ) && 
                   ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] > 0.0 )) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * seisanj_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] ) ;
              }
              else {
                if (( sei==1 ) && ( nenrei==15 )) {
                  printf("共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                  fprintf(stderr, "共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                }
                kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
              }
            }
          }
        }
    }


}




void simlkyos_1_sig(void) { /* 私学共済 */

  /*  変数定義  */
  double TMP ;
  double TMQ ;
  double TMR ;
  double seisan_wari[ENDY-STARTY+1] ;



    /* ファイル名セット＆開く */
   if ( KE > FJINKOY ) {
        /*  変数ゼロクリア  */
        TMP = 0.0 ; TMQ = 0.0 ; TMR = 0.0 ;
        for ( nendo=FJINKOY-1; nendo<=KE+1; nendo++ ) {
          seisan_wari[nendo-STARTY] = 0.0 ;
        }
        /*  総組合員数  */
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            TMP += kyosai[seido][FJINKOY-1-STARTY][sei][nenrei] ;
            TMQ += kyosai[seido][FJINKOY-STARTY][sei][nenrei] ;
          }
        }

        if ( ( KYOREQR == 1 )  && 
             ( TMP * sojinko_c[FJINKOY-1-STARTY][0] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )){
            seisan_wari[FJINKOY-1-STARTY] = TMP / sojinko_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / sojinko_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }
        else if ( ( KYOREQR == 2 ) && 
             ( TMP * seisanj_c[FJINKOY-1-STARTY][0] * seisanj_c[FJINKOY-STARTY][0] > 0.0 ) ) {
            seisan_wari[FJINKOY-1-STARTY] = TMP / seisanj_c[FJINKOY-1-STARTY][0] ;
            seisan_wari[FJINKOY-STARTY] = TMQ / seisanj_c[FJINKOY-STARTY][0] ;
            TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
            seisan_wari[nendo-STARTY] = seisan_wari[nendo-1-STARTY] * TMR ;
        }

        for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
          for ( sei=1; sei<=2; sei++ ) {
            for ( nenrei=15; nenrei<=100; nenrei++ ) {

              if ( ( KYOREQR == 1 ) && 
                   ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * sojinko_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] ) ;
              }
              else if ( ( KYOREQR == 2 ) && 
                   ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] > 0.0 )) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                  = kyosai[seido][FJINKOY-STARTY][sei][nenrei]
                    * ( seisan_wari[nendo-STARTY] * seisanj_c[nendo-STARTY][0] )
                    / ( seisan_wari[FJINKOY-STARTY] * seisanj_c[FJINKOY-STARTY][0] ) ;
              }
              else {
                if (( sei==1 ) && ( nenrei==15 )) {
                  printf("共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                  fprintf(stderr, "共済 %d の %04d 年度の補正がゼロです\n", seido, nendo );
                }
                kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
              }
            }
          }
        }
    }


}




