!      Programmer: Chris Dilbeck
!      Course: CS 430
!      Date Due: 9/12/07
!      Language: FORTRAN
!      Compiler: FORCE 2.0
!      Source: PA2P2.f
!      Exe: PA2P2.exe
!      Professor: Dr. Adams

!      Purpose: To convert decimal numbers into their binary
!      representations.

!      Input: A decimal number up to five places long, any more
!      can cause inaccuracy.

!      Output: A labelled echo of the input and a labelled echo of the
!      binary representation of the input.

!      Comments: This program uses a modified version of the algorithm
!      used for part 1 of this assignment, it just starts at different
!      values and does not care if a zero is printed before a one.

!      Syntax: CHARACTER NAME * X means to declare a string NAME
!      with X characters in it.

!      .LT. means < and .GT. means >

!      GOTO X sends the program execution to the given label X

!      .OR. is the boolean or operator

!      .EQ. is an equality comparison operator.

!      STRING(X:Y) is a substring notation. It means only reference the
!      locations in the string up to and including the locations X and Y
!      starting at 1, NOT ZERO, like Java or C++.

!      ** is the exponentation operator.

!      IF (CONDITION) THEN
!      EXPRESSIONS
!      ENDIF is a block conditional.

!      // means concatentation of strings

       CHARACTER BINARY * 16
       CHARACTER ONE*1
       CHARACTER ZERO*1

       ONE = '1'
       ZERO = '0'
       BINARY = '.'
       LENBINARY = 1

!      A real representation of the input
  20   REALINPUT = 0.0
  
!      The modified power of two to start on and end on
       INITIALPOWER = -1
       LASTPOWER = -6


       CALL PRINTLN('Enter a decimal number')

       READ(5,10) REALINPUT
  10   FORMAT(F6.5)
  
!      Check for bad input
       IF (REALINPUT .GT. 1.0 .OR. REALINPUT .LE. 0) THEN
       GOTO 20
       ENDIF
       
       CALL PRINTLN("Input was:")

       WRITE(6, 30) REALINPUT
  30   FORMAT(1X, F6.5)

       DO 11 I = INITIALPOWER, LASTPOWER, -1

       SLOTVALUE = 2.0 ** I

!      If the input is less than the slotvalue, put a zero
       IF (REALINPUT .LT. SLOTVALUE) THEN

!      Sub-string notation adapted from
!      http://www.ibiblio.org/pub/languages/fortran/ch2-13.html

!      BINARY(1:LENBINARY + 1) means to replace the area between and
!      including 1 and the position in the string after the last
!      actual digit, which is what LENBINARY tracks.
       BINARY(1:LENBINARY + 1) = BINARY(1:LENBINARY) // ZERO
       LENBINARY = LENBINARY + 1



       ENDIF

!      If the input is greater than the slot value, subtract the slot
!      value and add a one to the binary.
       IF (REALINPUT .GE. SLOTVALUE) THEN

       REALINPUT = REALINPUT - SLOTVALUE

       BINARY(1:LENBINARY + 1) = BINARY(1:LENBINARY) // ONE

       LENBINARY = LENBINARY + 1



       ENDIF


  11   CONTINUE

       CALL BLANKLINE
       CALL PRINTLN("Binary representation:")
       CALL PRINTLN(BINARY)


       STOP
       END



!      Advances the screen one line
       SUBROUTINE BLANKLINE
       CHARACTER LN
       LN = ' '
       WRITE(6, 29) LN
  29   FORMAT(1X, A)
       RETURN
       END

!      Prints a string
!      Technique adapted from http://aspire.cs.uah.edu/aspire/summer/manuals/fortran/subroutines.html
       SUBROUTINE PRINTLN(LN)
       CHARACTER(*) LN
       WRITE(6,30) LN
  30   FORMAT(1X, A)
       RETURN
       END
