PROGRAM CALC
!     Last change:  X    16 Nov 2004    2:07 am
!-----------------------------------------------------------------------------
!                                                           Date: 11-16-04
! Names: Jordon Herzog, Kelly Harris, John Hall, and Alex Beck
!
! Program to calculate the input into the second program
!
! Variables use are:
! x       : the value of a root
! rootR   : root to the right of the root x
! rootL   : root to the left of the root x
! dist1   : distance between x and rootL
! dist2   : distance between x and rootR
! a, b    : dist1 or dist2 multiplied by 0.1 and 0.3 respectively
! chopper : placeholder variable for truncating the root
! x0      : value of the root minus its value a
! x1      : value of the root plus its value b
! temp    : placeholder variable for truncating the root
! i       : loop counter
! answer  : whether or not the root has values on both sides
! ans     : whether or not to run the program again
!
! Input  : a root and it's neighboring root(s)
! Output : values of x-a and x+b for the root
!-----------------------------------------------------------------------------
IMPLICIT NONE

REAL (KIND=8) :: x, rootR, rootL, dist1, dist2, a, b, chopper, x0, x1
INTEGER :: temp, i
CHARACTER(1) :: answer, ans

WRITE (*,*) "Range Calculations for the Coordinate Finder Program:"

DO
  WRITE (*,*)
  WRITE (*,*) "Choose a root from the list."
  WRITE (*,*) "Does this root have roots on both sides of it? (y/n) "
  READ (*,*) answer

  IF (answer == "n" .OR. answer == "N") THEN
     DO i = 1,1,1
        WRITE (*,*) "Enter the root, and the root value next to it: "
        READ (*,*) x, rootL

        !!! rounds each variable to 4 places after the decimal
        temp = x*10000.0D0
        chopper = (x - REAL(temp)/10000.0D0)
        x = x - chopper

        temp = rootL*10000.0D0
        chopper = rootL - REAL(temp)/10000.0D0
        rootL = rootL - chopper
     END DO

     dist1 = ABS(x-rootL)
     a = dist1 * 0.1D0
     b = dist1 * 0.3D0
     x0 = x - a
     x1 = x + b
     WRITE (*,*) "The value of x-a is: ", x0
     WRITE (*,*) "the value of x+b is: ", x1

     ELSE

     WRITE(*,*) "Enter a root x, the root on the left, and the root on the right: "
     READ(*,*) x, rootL, rootR

     ! rounds each variable to 4 places after the decimol
     temp = x*10000.0D0
     chopper = x - REAL(temp)/10000.0D0
     x = x - chopper

     temp = rootL*10000.0D0
     chopper = rootL - REAL(temp)/10000.0D0
     rootL = rootL - chopper

     temp = rootR*10000.0D0
     chopper = rootR - REAL(temp)/10000.0D0
     rootR = rootR - chopper

     ! Determines distance to use
     dist1 = ABS(x - rootL)
     dist2 = ABS(rootR - x)
        IF (dist1 < dist2) THEN
           a = 0.1D0 * dist1
           b = 0.3D0 * dist1
        ELSE
           a = 0.1D0 * dist2
           b = 0.3D0 * dist2
        END IF

     x0 = x - a
     x1 = x + b
     WRITE(*,*) "The value for x-a is: ", x0
     WRITE(*,*) "The value for x+b is: ", x1
     END IF

  WRITE (*,*)
  WRITE (*,*) "Need another set of ranges? "
  READ (*,*) ans

  IF (ans == 'n' .OR. ans == 'N') EXIT

  WRITE (*,*)
  WRITE (*,*) "Now choose another root."
END DO

WRITE (*,*) "Now move on to the Coordinate Calculation program with your data."

STOP
END PROGRAM CALC
