/*************************************
*                                    *
*    外枠システム  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 ) {  

        seido = 1;
        readkyos_1( seido, KYOREQBK );
        simlkyos_1_kok() ;

    }
    else if ( KYOREQK == 3 ) {  

        seido = 1;
        readkyos_2( seido, KIJUN );
        readkyos_3( seido, KYOREQBK );
        simlkyos_2_kok() ;

    }


    if ( KYOREQR == 1 ) {  

        seido = 2;
        readkyos_1( seido, KYOREQBR );
        simlkyos_1_ren() ;
    }
    else if ( KYOREQR == 3 ) {  

        seido = 2;
        readkyos_2( seido, KIJUN );
        readkyos_3( seido, KYOREQBR );
        simlkyos_2_ren() ;

    }



    if ( KYOREQS == 1 ) {  

        seido = 3;
        readkyos_1( seido, KYOREQBS );
        seido = 3;
        simlkyos_1_sig() ;
    }
    else if ( KYOREQS == 2 ) {  

        seido = 3;
        readkyos_1( seido, KYOREQBS );
        readkyos_2( seido, KIJUN );
        readkyos_3( seido, KYOREQBS );
        simlkyos_2_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[1][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[1][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 ( KYOREQK == 1 ) {  
        for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
          for ( sei=1; sei<=2; sei++ ) {
            for ( nenrei=15; nenrei<=100; nenrei++ ) {
              if (( sojinko_j_c[KIJUN-STARTY][0] > 0.0 ) &&
                    ( stk_sojinko_c[nendo-STARTY][0] * sojinko_c[KIJUN-STARTY][0] > 0.0 )){
                kyosai[seido][nendo-STARTY][sei][nenrei] 
                  *= sojinko_c[nendo-STARTY][0] / stk_sojinko_c[nendo-STARTY][0] ;
              }
              else if (( stk_sojinko_c[KIJUN-STARTY][0] > 0.0 ) &&
                    ( stk_sojinko_c[nendo-STARTY][0] * sojinko_c[KIJUN-STARTY][0] > 0.0 )){
                kyosai[seido][nendo-STARTY][sei][nenrei] 
                  *= stk_sojinko_c[KIJUN-STARTY][0] / sojinko_c[KIJUN-STARTY][0]
                   * sojinko_c[nendo-STARTY][0] / stk_sojinko_c[nendo-STARTY][0] ;
              }
            }
          }
        }
      }


    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] ;
        }

        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 {
                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 ( KYOREQR == 1 ) {  
        for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
          for ( sei=1; sei<=2; sei++ ) {
            for ( nenrei=15; nenrei<=100; nenrei++ ) {

              if (( sojinko_j_c[KIJUN-STARTY][0] > 0.0 ) &&
                    ( str_sojinko_c[nendo-STARTY][0] * sojinko_c[KIJUN-STARTY][0] > 0.0 )){
                kyosai[seido][nendo-STARTY][sei][nenrei] 
                  *= sojinko_c[nendo-STARTY][0] / str_sojinko_c[nendo-STARTY][0] ;
              }
              else if (( str_sojinko_c[KIJUN-STARTY][0] > 0.0 ) &&
                    ( str_sojinko_c[nendo-STARTY][0] * sojinko_c[KIJUN-STARTY][0] > 0.0 )){
                kyosai[seido][nendo-STARTY][sei][nenrei] 
                  *= str_sojinko_c[KIJUN-STARTY][0] / sojinko_c[KIJUN-STARTY][0]
                   * sojinko_c[nendo-STARTY][0] / str_sojinko_c[nendo-STARTY][0] ;
              }

            }
          }
        }
      }



    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] ;
        }

        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 {
                kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
              }
            }
          }
        }
    }


}




void simlkyos_1_sig(void) { 

  int     TMS ;
  int     ii ;
  int     syubetu ;
  double  jigyoudan_ritu[5] ;
  double  TMQQ ;
  double  st_TMQQ ;
  char    filename[250]    ; 


      sprintf(filename, "/rev26/wakuc/data/kyos/siggakusyu26-%02d.csv", KYOREQBS ) ; 
      if ((fp_kyos = fopen(filename, "r")) == NULL) {
          fprintf(stderr, "私学共済の学種別被保険者ファイルを開けません!\n");
          exit (2);
      }
      karayomi( fp_kyos ) ;
      karayomi( fp_kyos ) ;

    for ( ii=2005; ii<=2110; ii++ ) { 
        read_csv( buffer, fp_kyos, &data_number );
        nendo = (int)buffer[1] ;
        for ( syubetu=1; syubetu<=11; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][1] = buffer[syubetu+2] ;
        }
        sig_akyos[0][nendo-STARTY][1] = buffer[2] ;
    }

    for ( ii=2005; ii<=2110; ii++ ) { 
        read_csv( buffer, fp_kyos, &data_number );
        nendo = (int)buffer[1] ;
        for ( syubetu=1; syubetu<=11; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][2] = buffer[syubetu+2] ;
        }
        sig_akyos[0][nendo-STARTY][2] = buffer[2] ;
    }

    fclose( fp_kyos );


    TMS = KIJUN ;
    KIJUN = 2024 ;

    for ( nendo=STARTY+1 ; nendo<=KE; nendo++ ) {

         for ( syubetu=0; syubetu<=10; syubetu++ ) {
           sig_gakurei[syubetu][nendo-STARTY] = 0.0 ;
           st_sig_gakurei[syubetu][nendo-STARTY] = 0.0 ;
         }

         for ( nenrei=18; nenrei<=21; nenrei++ ) {
           sig_gakurei[1][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[1][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=18; nenrei<=19; nenrei++ ) {
           sig_gakurei[2][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[2][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=19; nenrei++ ) {
           sig_gakurei[3][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[3][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=17; nenrei++ ) {
           sig_gakurei[4][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[4][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=12; nenrei<=14; nenrei++ ) {
           sig_gakurei[5][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[5][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=6; nenrei<=11; nenrei++ ) {
           sig_gakurei[6][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[6][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=3; nenrei<=5; nenrei++ ) {
           sig_gakurei[7][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[7][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=3; nenrei<=21; nenrei++ ) {
           sig_gakurei[8][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[8][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=19; nenrei++ ) {
           sig_gakurei[9][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[9][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=18; nenrei<=19; nenrei++ ) {
           sig_gakurei[10][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[10][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }

    }

    for ( sei=0; sei<=2; sei++ ) {
      jigyoudan_ritu[sei] = sig_akyos[11][KIJUN-STARTY][sei]
        / ( sig_akyos[0][KIJUN-STARTY][sei] - sig_akyos[11][KIJUN-STARTY][sei] ) ;
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {
        TMQQ = 0.0 ; st_TMQQ = 0.0 ; 
        for ( syubetu=1; syubetu<=10; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][sei] = sig_akyos[syubetu][KIJUN-STARTY][sei]
           * sig_gakurei[syubetu][nendo-STARTY] / sig_gakurei[syubetu][KIJUN-STARTY] ;
          TMQQ += sig_akyos[syubetu][nendo-STARTY][sei] ;
          st_sig_akyos[syubetu][nendo-STARTY][sei] = sig_akyos[syubetu][KIJUN-STARTY][sei]
           * st_sig_gakurei[syubetu][nendo-STARTY] / st_sig_gakurei[syubetu][KIJUN-STARTY] ;
          st_TMQQ += st_sig_akyos[syubetu][nendo-STARTY][sei] ;
        }
        sig_akyos[11][nendo-STARTY][sei] = TMQQ * jigyoudan_ritu[sei] ;
        st_sig_akyos[11][nendo-STARTY][sei] = st_TMQQ * jigyoudan_ritu[sei] ;
        sig_akyos[0][nendo-STARTY][sei] = TMQQ + sig_akyos[11][nendo-STARTY][sei] ;
        st_sig_akyos[0][nendo-STARTY][sei] = st_TMQQ + st_sig_akyos[11][nendo-STARTY][sei] ;
      }
      sig_akyos[0][nendo-STARTY][0]
        = sig_akyos[0][nendo-STARTY][1] + sig_akyos[0][nendo-STARTY][2] ;
      st_sig_akyos[0][nendo-STARTY][0]
        = st_sig_akyos[0][nendo-STARTY][1] + st_sig_akyos[0][nendo-STARTY][2] ;
    }

      for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            kyosai[3][nendo-STARTY][sei][nenrei]
              *= sig_akyos[0][nendo-STARTY][sei] / st_sig_akyos[0][nendo-STARTY][sei] ;
          }
        }
      }

    if ( KE > FJINKOY ) {
      for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            if ( sig_akyos[0][FJINKOY-STARTY][sei] > 0.0 ) {
              kyosai[3][nendo-STARTY][sei][nenrei]
                = kyosai[3][FJINKOY-STARTY][sei][nenrei]
                  * sig_akyos[0][nendo-STARTY][sei] / sig_akyos[0][FJINKOY-STARTY][sei] ;
            }
            else {
              kyosai[3][nendo-STARTY][sei][nenrei] = 0.0 ;
            }
          }
        }
      }
    }

      KIJUN = TMS ;


}


void simlkyos_2_kok(void) { 

  int  XEND ;
  double TMP ;
  double TMQ ;
  double TMR ;
  double seisan_wari[ENDY-STARTY+1] ; 


      if ( KYOREQK == 3 ) { 
        for ( nendo=KIJUN; nendo<=FJINKOY; nendo++ ) {
          if ( stk_sojinko_c[nendo-STARTY][0] > 0 ) {
              akyos[seido][nendo-STARTY][0] = st_akyos[seido][nendo-STARTY][0]
                * sojinko_c[nendo-STARTY][0] / stk_sojinko_c[nendo-STARTY][0] ;
              akyos[seido][nendo-STARTY][1] = akyos[seido][nendo-STARTY][0]
                * st_akyos[seido][nendo-STARTY][1] / st_akyos[seido][nendo-STARTY][0] ;
              akyos[seido][nendo-STARTY][2] = akyos[seido][nendo-STARTY][0]
                * st_akyos[seido][nendo-STARTY][2] / st_akyos[seido][nendo-STARTY][0] ;
          }
        }
      }


    if ( ( KYOREQK == 3 ) && ( akyos[seido][FJINKOY-1-STARTY][0] * sojinko_c[FJINKOY-1-STARTY][0] 
                               * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
        TMR = 0.0; 
        seisan_wari[FJINKOY-1-STARTY] = akyos[seido][FJINKOY-1-STARTY][0] / sojinko_c[FJINKOY-1-STARTY][0] ;
        seisan_wari[FJINKOY-STARTY] = akyos[seido][FJINKOY-STARTY][0] / sojinko_c[FJINKOY-STARTY][0] ;
        TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
    }

    if ( KE >= FJINKOY ) {
      for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
        seisan_wari[nendo-STARTY] = seisan_wari[nendo-1-STARTY] * TMR ;
        for ( sei=0; sei<=2; sei++ ) {

          if ( ( KYOREQK == 3 ) && 
              ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
              akyos[seido][nendo-STARTY][sei]
                = akyos[seido][FJINKOY-STARTY][sei]
                  * ( seisan_wari[nendo-STARTY] * sojinko_c[nendo-STARTY][0] )
                  / ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] ) ;
          }

        }
      }
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {
        TMP = 0.0;
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          dattai[seido][nendo-STARTY][sei][nenrei] = dattai[seido][nendo-1-STARTY][sei][nenrei] ;
          bup[seido][nendo-STARTY][sei][nenrei] = bup[seido][nendo-1-STARTY][sei][nenrei] ;
          TMP += bup[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( TMP > 0.0 ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            bup[seido][nendo-STARTY][sei][nenrei] *= 1.0 / TMP ;
          }
        }
      }
    }


    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {

      XEND = xend[seido][nendo-STARTY] ;  

      TMP = 0 ; TMR = 0 ; 

      for ( sei=1; sei<=2; sei++ ) {
        for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] 
             = kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * ( 1.0 - dattai[seido][nendo-STARTY][sei][nenrei] ) ;
            TMP += zanzon[seido][nendo-STARTY][sei][nenrei] ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * dattai[seido][nendo-STARTY][sei][nenrei]  ;
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] = 0 ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1] ;
        }
      }

      sinki[seido][nendo-STARTY][0] = akyos[seido][nendo-STARTY][0] - TMP ;
      TMQ = sinki[seido][nendo-STARTY][sei] ;
      sinki[seido][nendo-STARTY][1] = sinki[seido][nendo-STARTY][0] * 0.60 ; 
      sinki[seido][nendo-STARTY][2] = sinki[seido][nendo-STARTY][0] * 0.40 ; 

      for ( sei=1; sei<=2; sei++ ) {

        if ( TMQ < 0.0 ) { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  * akyos[seido][nendo-STARTY][sei] / TMP ;
            }
        }
        else { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  + sinki[seido][nendo-STARTY][sei] * bup[seido][nendo-STARTY][sei][nenrei] ;
            }
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
        }

        for ( nenrei=15; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][0][nenrei] = 
              kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
        }

      }

    }

    printf("共済%dのsimlkyos_2終了\n", seido);

}



void simlkyos_2_ren(void) { 

  int  XEND ;
  double TMP ;
  double TMQ ;
  double TMR ;
  double seisan_wari[ENDY-STARTY+1] ; 


      if ( KYOREQR == 3 ) { 
        for ( nendo=KIJUN; nendo<=FJINKOY; nendo++ ) {
          if ( str_sojinko_c[nendo-STARTY][0] > 0 ) {
              akyos[seido][nendo-STARTY][0] = st_akyos[seido][nendo-STARTY][0]
                * sojinko_c[nendo-STARTY][0] / str_sojinko_c[nendo-STARTY][0] ;
              akyos[seido][nendo-STARTY][1] = akyos[seido][nendo-STARTY][0]
                * st_akyos[seido][nendo-STARTY][1] / st_akyos[seido][nendo-STARTY][0] ;
              akyos[seido][nendo-STARTY][2] = akyos[seido][nendo-STARTY][0]
                * st_akyos[seido][nendo-STARTY][2] / st_akyos[seido][nendo-STARTY][0] ;
          }
        }
      }

    if ( ( KYOREQR == 3 ) && ( akyos[seido][FJINKOY-1-STARTY][0] * sojinko_c[FJINKOY-1-STARTY][0] 
                               * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
        TMR = 0.0; 
        seisan_wari[FJINKOY-1-STARTY] = akyos[seido][FJINKOY-1-STARTY][0] / sojinko_c[FJINKOY-1-STARTY][0] ;
        seisan_wari[FJINKOY-STARTY] = akyos[seido][FJINKOY-STARTY][0] / sojinko_c[FJINKOY-STARTY][0] ;
        TMR = seisan_wari[FJINKOY-STARTY] / seisan_wari[FJINKOY-1-STARTY] ;
    }

    if ( KE >= FJINKOY ) {
      for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
        seisan_wari[nendo-STARTY] = seisan_wari[nendo-1-STARTY] * TMR ;
        for ( sei=0; sei<=2; sei++ ) {

          if ( ( KYOREQR == 3 ) && 
              ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] > 0.0 )) {
              akyos[seido][nendo-STARTY][sei]
                = akyos[seido][FJINKOY-STARTY][sei]
                  * ( seisan_wari[nendo-STARTY] * sojinko_c[nendo-STARTY][0] )
                  / ( seisan_wari[FJINKOY-STARTY] * sojinko_c[FJINKOY-STARTY][0] ) ;
          }

        }
      }
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {
        TMP = 0.0;
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          dattai[seido][nendo-STARTY][sei][nenrei] = dattai[seido][nendo-1-STARTY][sei][nenrei] ;
          bup[seido][nendo-STARTY][sei][nenrei] = bup[seido][nendo-1-STARTY][sei][nenrei] ;
          TMP += bup[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( TMP > 0.0 ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            bup[seido][nendo-STARTY][sei][nenrei] *= 1.0 / TMP ;
          }
        }
      }
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {

      XEND = xend[seido][nendo-STARTY] ;  

      TMP = 0 ; TMR = 0 ; 

      for ( sei=1; sei<=2; sei++ ) {
        for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] 
             = kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * exp( - dattai[seido][nendo-STARTY][sei][nenrei] ) ;
            TMP += zanzon[seido][nendo-STARTY][sei][nenrei] ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * ( 1.0 - exp( - dattai[seido][nendo-STARTY][sei][nenrei] ) ) ;
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] = 0 ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1] ;
        }
      }

      sinki[seido][nendo-STARTY][0] = akyos[seido][nendo-STARTY][0] - TMP ;
      TMQ = sinki[seido][nendo-STARTY][sei] ;
      sinki[seido][nendo-STARTY][1] = sinki[seido][nendo-STARTY][0] * 0.5349 ; 
      sinki[seido][nendo-STARTY][2] = sinki[seido][nendo-STARTY][0] * 0.4651 ; 

      for ( sei=1; sei<=2; sei++ ) {

        if ( TMQ < 0.0 ) { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  * akyos[seido][nendo-STARTY][sei] / TMP ;
            }
        }
        else { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  + sinki[seido][nendo-STARTY][sei] * bup[seido][nendo-STARTY][sei][nenrei] ;
            }
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
        }

        for ( nenrei=15; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][0][nenrei] = 
              kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
        }

      }

    }

    printf("共済%dのsimlkyos_2終了\n", seido);

}



void simlkyos_2_sig(void) { 

  int     TMS ;
  int     XEND ;
  int     ii ;
  int     syubetu ;
  double  jigyoudan_ritu[5] ;
  double  TMP ;
  double  TMR ;
  double  TMQ ;
  double  TMQQ ;
  double  st_TMQQ ;
  char    filename[250]    ; 


      sprintf(filename, "/rev26/wakuc/data/kyos/siggakusyu26-%02d.csv", JIN ) ;
      if ((fp_kyos = fopen(filename, "r")) == NULL) {
          fprintf(stderr, "私学共済の学種別被保険者ファイルを開けません!\n");
          exit (2);
      }

      karayomi( fp_kyos ) ;
      karayomi( fp_kyos ) ;

    for ( ii=2005; ii<=2110; ii++ ) { 
        read_csv( buffer, fp_kyos, &data_number );
        nendo = (int)buffer[1] ;
        for ( syubetu=1; syubetu<=11; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][1] = buffer[syubetu+2] ;
        }
        sig_akyos[0][nendo-STARTY][1] = buffer[2] ;
    }

    for ( ii=2005; ii<=2110; ii++ ) { 
        read_csv( buffer, fp_kyos, &data_number );
        nendo = (int)buffer[1] ;
        for ( syubetu=1; syubetu<=11; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][2] = buffer[syubetu+2] ;
        }
        sig_akyos[0][nendo-STARTY][2] = buffer[2] ;
    }

    fclose( fp_kyos );



    TMS = KIJUN ;
    KIJUN = 2024 ;

    for ( nendo=KIJUN; nendo<=KE; nendo++ ) {

         for ( syubetu=1; syubetu<=10; syubetu++ ) {
           sig_gakurei[syubetu][nendo-STARTY] = 0.0 ;
           st_sig_gakurei[syubetu][nendo-STARTY] = 0.0 ;
         }

         for ( nenrei=18; nenrei<=21; nenrei++ ) {
           sig_gakurei[1][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[1][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=18; nenrei<=19; nenrei++ ) {
           sig_gakurei[2][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[2][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=19; nenrei++ ) {
           sig_gakurei[3][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[3][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=17; nenrei++ ) {
           sig_gakurei[4][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[4][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=12; nenrei<=14; nenrei++ ) {
           sig_gakurei[5][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[5][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=6; nenrei<=11; nenrei++ ) {
           sig_gakurei[6][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[6][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=3; nenrei<=5; nenrei++ ) {
           sig_gakurei[7][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[7][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=3; nenrei<=21; nenrei++ ) {
           sig_gakurei[8][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[8][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=15; nenrei<=19; nenrei++ ) {
           sig_gakurei[9][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[9][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }
         for ( nenrei=18; nenrei<=19; nenrei++ ) {
           sig_gakurei[10][nendo-STARTY] += jinko_c[nendo-STARTY][0][nenrei] ;
           st_sig_gakurei[10][nendo-STARTY] += sts_jinko_c[nendo-STARTY][0][nenrei] ;
         }

    }

    for ( sei=0; sei<=2; sei++ ) {
      jigyoudan_ritu[sei] = sig_akyos[11][KIJUN-STARTY][sei]
        / ( sig_akyos[0][KIJUN-STARTY][sei] - sig_akyos[11][KIJUN-STARTY][sei] ) ;
    }

    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {
        TMQQ = 0.0 ; st_TMQQ = 0.0 ; 
        for ( syubetu=1; syubetu<=10; syubetu++ ) {
          sig_akyos[syubetu][nendo-STARTY][sei] = sig_akyos[syubetu][KIJUN-STARTY][sei]
           * sig_gakurei[syubetu][nendo-STARTY] / sig_gakurei[syubetu][KIJUN-STARTY] ;
          TMQQ += sig_akyos[syubetu][nendo-STARTY][sei] ;
          st_sig_akyos[syubetu][nendo-STARTY][sei] = sig_akyos[syubetu][KIJUN-STARTY][sei]
           * st_sig_gakurei[syubetu][nendo-STARTY] / st_sig_gakurei[syubetu][KIJUN-STARTY] ;
          st_TMQQ += st_sig_akyos[syubetu][nendo-STARTY][sei] ;
        }
        sig_akyos[11][nendo-STARTY][sei] = TMQQ * jigyoudan_ritu[sei] ;
        st_sig_akyos[11][nendo-STARTY][sei] = st_TMQQ * jigyoudan_ritu[sei] ;
        sig_akyos[0][nendo-STARTY][sei] = TMQQ + sig_akyos[11][nendo-STARTY][sei] ;
        st_sig_akyos[0][nendo-STARTY][sei] = st_TMQQ + st_sig_akyos[11][nendo-STARTY][sei] ;
      }
      sig_akyos[0][nendo-STARTY][0]
        = sig_akyos[0][nendo-STARTY][1] + sig_akyos[0][nendo-STARTY][2] ;
      st_sig_akyos[0][nendo-STARTY][0]
        = st_sig_akyos[0][nendo-STARTY][1] + st_sig_akyos[0][nendo-STARTY][2] ;
    }


    for ( nendo=TMS+1; nendo<=KE; nendo++ ) { 
      for ( sei=1; sei<=2; sei++ ) {
        TMP = 0.0;
        for ( nenrei=15; nenrei<=100; nenrei++ ) {
          dattai[seido][nendo-STARTY][sei][nenrei] = dattai[seido][nendo-1-STARTY][sei][nenrei] ;
          bup[seido][nendo-STARTY][sei][nenrei] = bup[seido][nendo-1-STARTY][sei][nenrei] ;
          TMP += bup[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( TMP > 0.0 ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            bup[seido][nendo-STARTY][sei][nenrei] *= 1.0 / TMP ;
          }
        }
      }
    }


    for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {

        XEND = xend[seido][nendo-STARTY] ;  

        TMP = 0 ; TMR = 0 ; 
        for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] 
             = kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * exp( - dattai[seido][nendo-STARTY][sei][nenrei] ) ;
            TMP += zanzon[seido][nendo-STARTY][sei][nenrei] ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * ( 1.0 - exp( - dattai[seido][nendo-STARTY][sei][nenrei] ) ) ;
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] = 0 ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1] ;
        }

        sinki[seido][nendo-STARTY][sei] = sig_akyos[0][nendo-STARTY][sei] - TMP ;
        TMQ = sinki[seido][nendo-STARTY][sei] ;

        if ( TMQ < 0.0 ) { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  * sig_akyos[0][nendo-STARTY][sei] / TMP ;
            }
        }
        else { 
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  + sinki[seido][nendo-STARTY][sei] * bup[seido][nendo-STARTY][sei][nenrei] ;
            }
        }

        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
        }

      }

      for ( nenrei=15; nenrei<=110; nenrei++ ) {
          kyosai[seido][nendo-STARTY][0][nenrei] = 
            kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
      }

    }


    if ( KE > FJINKOY ) {
      for ( nendo=FJINKOY+1; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
            if ( sig_akyos[0][FJINKOY-STARTY][sei] > 0.0 ) {
              kyosai[3][nendo-STARTY][sei][nenrei]
                = kyosai[3][FJINKOY-STARTY][sei][nenrei]
                  * sig_akyos[0][nendo-STARTY][sei] / sig_akyos[0][FJINKOY-STARTY][sei] ;
            }
            else {
              kyosai[3][nendo-STARTY][sei][nenrei] = 0.0 ;
            }
          }
        }
      }
    }

    KIJUN = TMS ;

    printf("共済%dのsimlkyos_2終了\n", seido);

}





