module angle_class !------------------------------------------------------------------------------ !Program: Angle Class !Author: Chris Harper !Date: 3/30/2008 !------------------------------------------------------------------------------ implicit none type angle private integer :: degrees, minutes, seconds end type !relational operators interface operator (==) module procedure op_equality end interface interface operator (<) module procedure op_lessthan end interface !arithmetic operators interface operator (+) module procedure op_addition end interface interface operator (-) module procedure op_subtraction end interface contains !conversion functions function dms_to_dec (dms) type(angle), intent(IN) :: dms real :: dms_to_dec integer :: total_secs total_secs = (dms%minutes * 60) + dms%seconds dms_to_dec = dms%degrees + (total_secs / 3600.0) end function function dec_to_dms (dec) type(angle) :: dec_to_dms real, intent(IN) :: dec real :: fPart dec_to_dms%degrees = aint(dec) fpart = dec - dec_to_dms%degrees fpart = fpart * 60 dec_to_dms%minutes = aint(fpart) fpart = fPart - dec_to_dms%minutes dec_to_dms%seconds = nint(fpart * 60) end function !relational functions function op_equality (a, b) logical :: op_equality type(angle), intent(IN) :: a, b op_equality = dms_to_dec(a) == dms_to_dec(b) end function function op_lessthan (a, b) logical :: op_lessthan type(angle), intent(IN) :: a, b op_lessthan = dms_to_dec(a) < dms_to_dec(b) end function !arithmetic functions function op_addition (a, b) type(angle) :: op_addition type(angle), intent(IN) :: a, b op_addition = dec_to_dms(dms_to_dec(a) + dms_to_dec(b)) end function function op_subtraction (a, b) type(angle) :: op_subtraction type(angle), intent(IN) :: a, b op_subtraction = dec_to_dms(dms_to_dec(a) - dms_to_dec(b)) end function !constructor function new_angle(d, m, s) type(angle) :: new_angle integer :: d, m, s new_angle%degrees = d new_angle%minutes = m new_angle%seconds = s end function !I/O !this function is also a constructor function input () type(angle) :: input print *, "Enter an angle in degrees, minutes, and seconds:" read *, input%degrees, input%minutes, input%seconds end function !prints formatted class members subroutine output (a) type(angle), intent(IN) :: a print "(I4, 1X, I3, '''', 1X, I3, '""')", a%degrees, a%minutes, a%seconds !print *, a%degrees, a%minutes, a%seconds end subroutine end module