/*************************************
*                                    *
*    外枠システム  simlichisan.c     *
*  （第１号、第３号被保険者数の推計）*
*                                    *
*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "set.h"
#include "cntl.h"


void simlichisan(void) {

  double  TMP  ;
  double  TMQ  ;
  int     ii   ;
  int     XEND ;


    for ( nendo=STARTY+1; nendo<=KIJUN; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {

        ichigouip[nendo-STARTY][3][nenrei]
          = ichigouip[nendo-STARTY][2][nenrei] * ichiyuhaigr[nenrei] ;
        ichigouip[nendo-STARTY][4][nenrei]
          = ichigouip[nendo-STARTY][2][nenrei] * ( 1.0 - ichiyuhaigr[nenrei] ) ;
        ichigouni[nendo-STARTY][3][nenrei]
          = ichigouni[nendo-STARTY][2][nenrei] * ichiyuhaigr[nenrei] ;
        ichigouni[nendo-STARTY][4][nenrei]
          = ichigouni[nendo-STARTY][2][nenrei] * ( 1.0 - ichiyuhaigr[nenrei] ) ;

        for ( sei=0; sei<=4; sei++ ) {
          ichigou[nendo-STARTY][sei][nenrei] = 
            ichigouip[nendo-STARTY][sei][nenrei] + ichigouni[nendo-STARTY][sei][nenrei] ;
        }

        for ( sei=0; sei<=4; sei++ ) {
          mika_soto1[nendo-STARTY][sei][nenrei]
            = jinko_m[nendo-STARTY][sei][nenrei] - ichigou[nendo-STARTY][sei][nenrei]
                - nigou[nendo-STARTY][sei][nenrei] - sangou[nendo-STARTY][sei][nenrei] ; 
          mika_soto2[nendo-STARTY][sei][nenrei]
            = max( 0.0 , mika_soto1[nendo-STARTY][sei][nenrei] ) ;
          r_mika_soto[nendo-STARTY][sei][nenrei]
            = mika_soto2[nendo-STARTY][sei][nenrei] / jinko_m[nendo-STARTY][sei][nenrei] ; 
        }

      }
    }


    for ( sei=1; sei<=2; sei++ ) {
      for ( nenrei=20; nenrei<=69; nenrei++ ) {
        if ( nenrei<=59 ) { 
          r_ichigouni[KIJUN-STARTY][sei][nenrei]
            = ichigouni[KIJUN-STARTY][sei][nenrei] / jinko_m[KIJUN-STARTY][sei][nenrei] ;
        }
        else { 
          r_ichigouni[KIJUN-STARTY][sei][nenrei] = ichigouni[KIJUN-STARTY][sei][nenrei]
             / ( jinko_m[KIJUN-STARTY][sei][nenrei] - nigou[KIJUN-STARTY][sei][nenrei] ) ;
        }
      }
    }

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

        for ( sei=1; sei<=2; sei++ ) {
          if ( nenrei<=59 ) {
            ichigouni[nendo-STARTY][sei][nenrei]
              = jinko_m[nendo-STARTY][sei][nenrei] * r_ichigouni[KIJUN-STARTY][sei][nenrei] ;
          }
          else {
            ichigouni[nendo-STARTY][sei][nenrei]
              = ( jinko_m[nendo-STARTY][sei][nenrei] - nigou[nendo-STARTY][sei][nenrei] )
                  * r_ichigouni[KIJUN-STARTY][sei][nenrei] ;
          }
        }

        ichigouni[nendo-STARTY][3][nenrei]
          = ichigouni[nendo-STARTY][2][nenrei] * yuhaigr_m[nendo-STARTY][nenrei] ;
        ichigouni[nendo-STARTY][4][nenrei]
          = ichigouni[nendo-STARTY][2][nenrei] * ( 1.0 - yuhaigr_m[nendo-STARTY][nenrei] ) ;

        ichigouni[nendo-STARTY][0][nenrei]
          = ichigouni[nendo-STARTY][1][nenrei] + ichigouni[nendo-STARTY][2][nenrei] ;

      }
    }


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

      for ( sei=1; sei<=2; sei++ ) {
        if ( nendo >= 2035 ) {
          r_mika_soto[nendo-STARTY][sei][nenrei] = 0.0 ;
        }
        else if ( nenrei<=59 ) {
          r_mika_soto[nendo-STARTY][sei][nenrei] 
           = r_mika_soto[KIJUN-STARTY][sei][nenrei] 
              * (double)( 2035 - nendo ) / (double)( 2035 - KIJUN ) ;
        }
        else {
          r_mika_soto[nendo-STARTY][sei][nenrei] = r_mika_soto[nendo-STARTY][sei][59] ;
        }
        mika_soto1[nendo-STARTY][sei][nenrei] 
          = r_mika_soto[nendo-STARTY][sei][nenrei] * jinko_m[nendo-STARTY][sei][nenrei] ;

      }

      mika_soto1[nendo-STARTY][3][nenrei] 
        = mika_soto1[nendo-STARTY][2][nenrei] * yuhaigr_m[nendo-STARTY][nenrei] ;
      mika_soto1[nendo-STARTY][4][nenrei] 
        = mika_soto1[nendo-STARTY][2][nenrei] * ( 1.0 - yuhaigr_m[nendo-STARTY][nenrei] ) ;
      mika_soto1[nendo-STARTY][0][nenrei] 
        = mika_soto1[nendo-STARTY][1][nenrei] + mika_soto1[nendo-STARTY][2][nenrei] ;

    }
  }



  for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
    XEND = xend[8][nendo-STARTY] ; 
    if ( XEND < 60 ) { XEND = 60; }
    for ( nenrei=20; nenrei<=XEND-1; nenrei++ ) {

      if ( kounen[KIJUN-STARTY][3][nenrei] > 0.0 ) {
        kounen3[nendo-STARTY][1][nenrei] = kounen[nendo-STARTY][3][nenrei]
          * kounen3[KIJUN-STARTY][1][nenrei] / kounen[KIJUN-STARTY][3][nenrei] ;
      }
      else { 
        kounen3[nendo-STARTY][1][nenrei]
          = ( kounen[nendo-STARTY][3][nenrei-1] + kounen[nendo-STARTY][3][nenrei+1] ) 
            * kounen3[KIJUN-STARTY][1][nenrei]
            / ( kounen[KIJUN-STARTY][3][nenrei-1] + kounen[KIJUN-STARTY][3][nenrei+1] ) ;
      }
      kou3syu3[nendo-STARTY][1][nenrei] = 0.0 ; 
      kou12syu3[nendo-STARTY][1][nenrei] = kounen3[nendo-STARTY][1][nenrei] ;
      qkou12syu3[nendo-STARTY][1][nenrei] = kounen3[nendo-STARTY][1][nenrei] ;
      for ( seido=4; seido<=7; seido++ ) {
        if ( kounen3[KIJUN-STARTY][1][nenrei] > 0.0 ) {
          kyosai3[seido][nendo-STARTY][1][nenrei] = kounen3[nendo-STARTY][1][nenrei]
            * kyosai3[seido][KIJUN-STARTY][1][nenrei] / kounen3[KIJUN-STARTY][1][nenrei] ;
        }
        else { 
          kyosai3[seido][nendo-STARTY][1][nenrei] = 0.0 ; 
        }
        qkou12syu3[nendo-STARTY][1][nenrei] -= kyosai3[seido][nendo-STARTY][1][nenrei] ;
      }
      qkounen3[nendo-STARTY][1][nenrei] = qkou12syu3[nendo-STARTY][1][nenrei] ;

      for ( seido=1; seido<=3; seido++ ) {
        if ( kyosai[seido][KIJUN-STARTY][3][nenrei] > 0.0 ) {
          kyosai3[seido][nendo-STARTY][1][nenrei] = kyosai[seido][nendo-STARTY][3][nenrei]
            * kyosai3[seido][KIJUN-STARTY][1][nenrei] / kyosai[seido][KIJUN-STARTY][3][nenrei] ;
        }
        else { 
          kyosai3[seido][nendo-STARTY][1][nenrei]
            = ( kyosai[seido][nendo-STARTY][3][nenrei-1] + kyosai[seido][nendo-STARTY][3][nenrei+1] )
              * kyosai3[seido][KIJUN-STARTY][1][nenrei]
              / ( kyosai[seido][KIJUN-STARTY][3][nenrei-1] + kyosai[seido][KIJUN-STARTY][3][nenrei+1] ) ;
        }
      }

      sangou[nendo-STARTY][1][nenrei] = kounen3[nendo-STARTY][1][nenrei] ;
      for ( seido=1; seido<=3; seido++ ) {
        sangou[nendo-STARTY][1][nenrei] += kyosai3[seido][nendo-STARTY][1][nenrei] ;
      }

    }
  }

  for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
    XEND = xend[8][nendo-STARTY] ; 
    if ( XEND < 60 ) { XEND = 60; }
    for ( nenrei=20; nenrei<=XEND-1; nenrei++ ) {

      TMP = jinko_m[KIJUN-STARTY][3][nenrei] - nigou[KIJUN-STARTY][3][nenrei] ;
      TMQ = jinko_m[nendo-STARTY][3][nenrei] - nigou[nendo-STARTY][3][nenrei] ;
      if (( TMP > 0.0 ) && ( TMQ > 0.0 )) {
        if ( MODESAN == 1 ) { 
          sangou[nendo-STARTY][3][nenrei] = TMQ * sangou[KIJUN-STARTY][3][nenrei] / TMP 
            * ( nigou[nendo-STARTY][1][nenrei] / jinko_m[nendo-STARTY][1][nenrei] )
            / ( nigou[KIJUN-STARTY][1][nenrei] / jinko_m[KIJUN-STARTY][1][nenrei] ) ;
        }
      }
      else { 
        sangou[nendo-STARTY][3][nenrei] = 0.0 ;
      }
      sangou[nendo-STARTY][4][nenrei] = 0.0 ;
      sangou[nendo-STARTY][2][nenrei] = sangou[nendo-STARTY][3][nenrei] ;

      kounen3[nendo-STARTY][3][nenrei] = sangou[nendo-STARTY][3][nenrei]
        * kounen[nendo-STARTY][1][nenrei] / nigou[nendo-STARTY][1][nenrei] ; 
      kounen3[nendo-STARTY][4][nenrei] = 0.0 ;
      kounen3[nendo-STARTY][2][nenrei] = kounen3[nendo-STARTY][3][nenrei] ;
      kou3syu3[nendo-STARTY][3][nenrei] = sangou[nendo-STARTY][3][nenrei]
        * kou3syu[nendo-STARTY][1][nenrei] / nigou[nendo-STARTY][1][nenrei] ; 
      kou3syu3[nendo-STARTY][4][nenrei] = 0.0 ;
      kou3syu3[nendo-STARTY][2][nenrei] = kou3syu3[nendo-STARTY][3][nenrei] ;
      for ( sei=2; sei<=4; sei++ ) { 
        kou12syu3[nendo-STARTY][sei][nenrei]
          = kounen3[nendo-STARTY][sei][nenrei] - kou3syu3[nendo-STARTY][sei][nenrei] ;
      }
      qkou12syu3[nendo-STARTY][3][nenrei] = kou12syu3[nendo-STARTY][3][nenrei] ;
      qkounen3[nendo-STARTY][3][nenrei] = kounen3[nendo-STARTY][3][nenrei] ;
      for ( seido=4; seido<=7; seido++ ) {
        if ( kou12syu3[KIJUN-STARTY][3][nenrei] > 0.0 ) {
          kyosai3[seido][nendo-STARTY][3][nenrei] = kou12syu3[nendo-STARTY][3][nenrei]
            * kyosai3[seido][KIJUN-STARTY][3][nenrei] / kou12syu3[KIJUN-STARTY][3][nenrei] ;
        }
        else { 
          kyosai3[seido][nendo-STARTY][3][nenrei] = 0.0 ;
        }
        kyosai3[seido][nendo-STARTY][4][nenrei] = 0.0 ;
        kyosai3[seido][nendo-STARTY][2][nenrei] = kyosai3[seido][nendo-STARTY][3][nenrei] ;
        qkou12syu3[nendo-STARTY][3][nenrei] -= kyosai3[seido][nendo-STARTY][3][nenrei] ;
        qkounen3[nendo-STARTY][3][nenrei] -= kyosai3[seido][nendo-STARTY][3][nenrei] ;
      }
      qkou12syu3[nendo-STARTY][4][nenrei] = 0.0 ;
      qkou12syu3[nendo-STARTY][2][nenrei] = qkou12syu3[nendo-STARTY][3][nenrei] ;
      qkounen3[nendo-STARTY][4][nenrei] = 0.0 ;
      qkounen3[nendo-STARTY][2][nenrei] = qkounen3[nendo-STARTY][3][nenrei] ;

      for ( seido=1; seido<=3; seido++ ) {
        kyosai3[seido][nendo-STARTY][3][nenrei] = sangou[nendo-STARTY][3][nenrei]
          * kyosai[seido][nendo-STARTY][1][nenrei] / nigou[nendo-STARTY][1][nenrei] ;
        kyosai3[seido][nendo-STARTY][4][nenrei] = 0.0 ;
        kyosai3[seido][nendo-STARTY][2][nenrei] = kyosai3[seido][nendo-STARTY][3][nenrei] ;
      }

      qkou12syu3[nendo-STARTY][0][nenrei]
        = qkou12syu3[nendo-STARTY][1][nenrei] + qkou12syu3[nendo-STARTY][2][nenrei] ;
      qkounen3[nendo-STARTY][0][nenrei]
        = qkounen3[nendo-STARTY][1][nenrei] + qkounen3[nendo-STARTY][2][nenrei] ;
      kou3syu3[nendo-STARTY][0][nenrei]
        = kou3syu3[nendo-STARTY][1][nenrei] + kou3syu3[nendo-STARTY][2][nenrei] ;
      kou12syu3[nendo-STARTY][0][nenrei]
        = kou12syu3[nendo-STARTY][1][nenrei] + kou12syu3[nendo-STARTY][2][nenrei] ;
      kounen3[nendo-STARTY][0][nenrei]
        = kounen3[nendo-STARTY][1][nenrei] + kounen3[nendo-STARTY][2][nenrei] ;
      for ( seido=1; seido<=7; seido++ ) {
        kyosai3[seido][nendo-STARTY][0][nenrei]
          = kyosai3[seido][nendo-STARTY][1][nenrei] + kyosai3[seido][nendo-STARTY][2][nenrei] ;
      }
      sangou[nendo-STARTY][0][nenrei]
        = sangou[nendo-STARTY][1][nenrei] + sangou[nendo-STARTY][2][nenrei] ;

    }
  }


  for ( nendo=KIJUN+1; nendo<=KE; nendo++ ) {
    XEND = xend[8][nendo-STARTY] ; 
    if ( XEND < 60 ) { XEND = 60; }

    for ( nenrei=20; nenrei<=XEND-1; nenrei++ ) {
      for ( sei=1; sei<=4; sei++ ) {
        TMP = jinko_m[nendo-STARTY][sei][nenrei] - mika_soto1[nendo-STARTY][sei][nenrei]
                - nigou[nendo-STARTY][sei][nenrei] - sangou[nendo-STARTY][sei][nenrei]
                - ichigouni[nendo-STARTY][sei][nenrei] ;
        if ( TMP < 0.0 ) { 
            fprintf(stderr, "%d年度の性%d年齢%dの１号一般\n", nendo, sei, nenrei );
            ichigouip[nendo-STARTY][sei][nenrei] = 0.0 ;
        }
        else {
          ichigouip[nendo-STARTY][sei][nenrei] = TMP ;
        }
      }

      ichigouip[nendo-STARTY][2][nenrei]
        = ichigouip[nendo-STARTY][3][nenrei] + ichigouip[nendo-STARTY][4][nenrei] ;
      ichigouip[nendo-STARTY][0][nenrei]
        = ichigouip[nendo-STARTY][1][nenrei] + ichigouip[nendo-STARTY][2][nenrei] ;
    }

    for ( sei=0; sei<=4; sei++ ) {
      for ( nenrei=20; nenrei<=69; nenrei++ ) {
        ichigou[nendo-STARTY][sei][nenrei]
          = ichigouip[nendo-STARTY][sei][nenrei] + ichigouni[nendo-STARTY][sei][nenrei] ;
      }
    }

  }

  for ( nendo=STARTY+1; nendo<=KE; nendo++ ) {
    XEND = xend[8][nendo-STARTY] ; 
    if ( XEND < 60 ) { XEND = 60; }
    for ( sei=0; sei<=4; sei++ ) {
      for ( nenrei=0; nenrei<=19; nenrei++ ) {
        mika_soto1[nendo-STARTY][sei][nenrei] = 0.0 ;
      }
      for ( nenrei=XEND; nenrei<=100; nenrei++ ) {
        mika_soto1[nendo-STARTY][sei][nenrei] = 0.0 ;
      }
    }
  }

  XEND = xend[8][KIJUN-STARTY] ; 
  if ( XEND < 60 ) { XEND = 60; }
  for ( sei=0; sei<=4; sei++ ) {
    for ( nenrei=XEND; nenrei<=100; nenrei++ ) {
      sangou[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      kounen3[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      qkounen3[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      kou12syu3[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      qkou12syu3[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      kou3syu3[KIJUN-STARTY][sei][nenrei] = 0.0 ;
      for ( ii=1; ii<=7; ii++ ) {
        kyosai3[ii][KIJUN-STARTY][sei][nenrei] = 0.0 ;
      }
    }
  }


}




