!********o*********o*********o*********o*********o*********o*********o**
! This program adds two angles, in degrees, minutes, and seconds.  The
! Angles are given in dddDmm'ss" format, where ddd is the three digit
! angle, mm is the two digit minutes, and ss is the two digit seconds.
! D, ', and " are required unit indicators.
!---------------------
! Inputs (see declarations for meaning)
!   deg, min, sec, deg2, min2, sec2, bigD, tick, ttick
!
! Outputs (see declarations for meaning)
!   sumD, sumM, sumS
!---------------------
! Author:   Bob Harris
! Class:    CMPSC 201F
! Section:  1234
! Date Due: 22 Oct 2003
!********o*********o*********o*********o*********o*********o*********o**

program anglosaxon
implicit none

integer   :: deg, min, sec     ! the first angle
integer   :: deg2, min2, sec2  ! the second angle
integer   :: sumD, sumM, sumS  ! the sum of the angles
character :: bigD, tick, ttick ! unit indicators

!----------
! fetch and validate the first angle
!----------

! fetch the first angle from the user

print *,"Please enter the first angular measurement, in dddDmm'ss"" format:"
read  '(I3,A1,I2,A1,I2,A1)', deg,bigD, min,tick, sec,ttick

! validate first angle

if ((bigD /= 'D') .or. (tick /= '''') .or. (ttick /= '"')) then
    print *,"illegal input, missing unit indicator(s)"
    stop
end if

if ((deg < 0) .or. (deg >= 360)) then
    print *,"illegal input, degrees out of range"
    stop
end if

if ((min < 0) .or. (min >= 60)) then
    print *,"illegal input, minutes out of range"
    stop
end if

if ((sec < 0) .or. (sec >= 60)) then
    print *,"illegal input, seconds out of range"
    stop
end if

!----------
! fetch and validate the second angle
!----------

! fetch the second angle from the user

print *,"Please enter the first angular measurement, in dddDmm'ss"" format:"
read  '(I3,A1,I2,A1,I2,A1)', deg2,bigD, min2,tick, sec2,ttick

! validate second angle

if ((bigD /= 'D') .or. (tick /= '''') .or. (ttick /= '"')) then
    print *,"illegal input, missing unit indicator(s)"
    stop
end if

if ((deg2 < 0) .or. (deg2 >= 360)) then
    print *,"illegal input, degrees out of range"
    stop
end if

if ((min2 < 0) .or. (min2 >= 60)) then
    print *,"illegal input, minutes out of range"
    stop
end if

if ((sec2 < 0) .or. (sec2 >= 60)) then
    print *,"illegal input, seconds out of range"
    stop
end if

!----------
! add the angles
!----------

! do the basic add

sumD = deg + deg2
sumM = min + min2
sumS = sec + sec2

! carry any excess out of the seconds into minutes

do
    if (sumS < 60) exit
    sumS = sumS - 60
    sumM = sumM + 1
end do

! carry any excess out of the minutes into degrees

do
    if (sumM < 60) exit
    sumM = sumM - 60
    sumD = sumD + 1
end do

! throw out any excess from degrees, in whole revolutions
! note that the problem statement doesn't specify what to do in this case,
! so you can choose any reasonable way to handle it, but you should state your
! assumption and indicate how you are handling it

do
    if (sumD < 360) exit
    sumD = sumD - 360
end do

!----------
! show the resulting sum
!----------

100 format (A3,I3.3,A1,I2.2,A1,I2.2,A1)
print 100, "   ", deg, "D", min, "'", sec, """"
print 100, " + ", deg2,"D", min2,"'", sec2,""""
print *,   " -----------"
print 100, "   ", sumD,"D", sumM,"'", sumS,""""

end program anglosaxon
