IDENTIFICATION DIVISION.
PROGRAM-ID. 168C.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INP        PIC X(100).
01 A          PIC 9(04).
01 B          PIC 9(04).
01 H          PIC 9(02).
01 M          PIC 9(02).
01 W          PIC 9(08)       COMP.
01 AK         PIC 9(04)V9     COMP.
01 BK         PIC 9(04)       COMP.
01 SK         PIC 9(04)V9     COMP.
01 CNT        PIC 9(05)V9(10) COMP.
01 OUT        PIC ZZZZ9.9(10).
*>
PROCEDURE DIVISION.
  ACCEPT INP.
  UNSTRING INP DELIMITED BY ' '
      INTO A B H M.
*>
  COMPUTE AK = H * 30 + M * 0.5.
  COMPUTE BK = M * 6.
  COMPUTE SK = AK - BK.
  IF (SK > 180)
    COMPUTE SK = 360 - SK
  END-IF.
*>
  EVALUATE TRUE
    WHEN SK = 0
      COMPUTE CNT = A - B
    WHEN SK = 60
      COMPUTE CNT = ((A ** 2 + B ** 2) - (A * B)) ** 0.5
    WHEN SK = 90
      COMPUTE CNT = (A ** 2 + B ** 2) ** 0.5
    WHEN SK = 120
      COMPUTE CNT = ((A ** 2 + B ** 2) + (A * B)) ** 0.5
    WHEN SK = 180
      COMPUTE CNT = A + B
    WHEN OTHER
      COMPUTE CNT  = ((A ** 2 + B ** 2) - (2 * A * B * FUNCTION COS(SK * 3.1415926536 / 180))) ** 0.5
  END-EVALUATE
*>
  MOVE CNT TO OUT.
  DISPLAY FUNCTION TRIM(OUT).
  STOP RUN.
 