PROGRAM taxcalc
  IMPLICIT NONE

  DOUBLE PRECISION, PARAMETER :: e1 = 22d-2
  DOUBLE PRECISION, PARAMETER :: e2 = 29d-2
  DOUBLE PRECISION, PARAMETER :: e3 = 37d-2
  DOUBLE PRECISION, PARAMETER :: e4 = 45d-2

  DOUBLE PRECISION, PARAMETER :: k1 = 0d0
  DOUBLE PRECISION, PARAMETER :: k2 = 2d4
  DOUBLE PRECISION, PARAMETER :: k3 = 3d4
  DOUBLE PRECISION, PARAMETER :: k4 = 4d4

  DOUBLE PRECISION :: poso, foros, d, t1, t2, t3, t4

  PRINT *, "Dwse to eisodima"
  READ *, poso
  
  IF (poso < k1) THEN
     PRINT *, "Lathos eisodima!"
     STOP
  END IF

  ! t1 to poso poy tha forologhthei sthn prwth klimaka,
  ! t2 to poso sth deyterh, klp.
  
  d = k2-k1
  IF (poso < d) THEN
     t1 = poso
  ELSE
     t1 = d
  END IF
  poso = poso - t1
  
  d = k3-k2
  IF (poso < d) THEN
     t2 = poso
  ELSE
     t2 = d
  END IF
  poso = poso - t2
  
  d = k4-k3
  IF (poso < d) THEN
     t3 = poso
  ELSE
     t3 = d
  END IF
  poso = poso - t3
  
  t4 = poso
  
  foros = e1 * t1 + e2 * t2 + e3 * t3 + e4 * t4

  PRINT *, "o foros einai ", foros
END PROGRAM taxcalc
