Distance and Bearing: Difference between revisions
m
→{{header|Fortran}}: added syntax highlighting
Bwana Pete (talk | contribs) m (Formatted the results) |
m (→{{header|Fortran}}: added syntax highlighting) |
||
(2 intermediate revisions by one other user not shown) | |||
Line 165:
</pre>
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
program code_translation
use iso_fortran_env , only:real64 , real32
Line 180 ⟶ 181:
integer :: ii , jj
real(real64) :: dist
location%latitude = (51.514669D0)!Rosetta co-ords
location%longitude = (2.19858D0)
!Nearest to latitude 51.51467,longitude 2.19858 degrees
!
!Open the file for reading
open(unit = 10 , file = 'airports.csv' , status = 'OLD' , &
&action = 'READ' , iostat = iostat , encoding = 'UTF-8')
Line 228:
write(* , *) 'Read complete, time taken = ' , (jj - ii) , &
& 'milliseconds' // char(10) // char(13)
call sortem(airports) ! Sort the airports out
write(*, '(/,2x,a,t14,a,t75,a,t95,a,t108,a,t117,a)') 'Num' , 'Name' , &
Line 240 ⟶ 239:
end do
stop 'Normal completion' // char(10) // char(13)
end program code_translation
module helpers
Line 247 ⟶ 245:
real(real64) , parameter :: radius_in_km = 6372.8D0
real(real64) , parameter :: kilos_to_nautical = 0.5399568D0
type whereme
real(real64) :: latitude , longitude
Line 265 ⟶ 262:
end type airport
contains ! We'll calculate them and store in each airport
!
! The given angles are in radians, and the result is in degrees in the range [0, 360).
function bearing(lat1 , lon1 , lat2 , lon2)
real(real64) , parameter :: toradians = acos( - 1.0D0)/180.0D0
real(real64) :: bearing
real(real64) , intent(in) :: lat1
Line 276 ⟶ 271:
real(real64) , intent(in) :: lat2
real(real64) , intent(in) :: lon2
real(real64) :: dlat
real(real64) :: dlon
Line 283 ⟶ 277:
real(real64) :: x
real(real64) :: y
!▼
dlat = (lat2 - lat1)*toradians
dlon = toradians*(lon2 - lon1)
Line 295 ⟶ 289:
bearing = mod(bearing + 360.0D0 , 360.0D0)
end function bearing
!
!▼
▲!
function to_radian(degree) result(rad)
real(real64) , parameter :: deg_to_rad = atan(1.0D0)/45.0D0 ! exploit intrinsic atan to generate pi/180 runtime constant
!
real(real64) :: rad
real(real64) , intent(in) :: degree
Line 331 ⟶ 299:
rad = degree*deg_to_rad
end function to_radian
!
function to_degrees(radians) result(deg)
real(real64) , parameter :: radian_to_degree = 180.0D0/acos( - 1.0D0)
!
real(real64) :: deg
real(real64) , intent(in) :: radians
Line 341 ⟶ 309:
!
function haversine(deglat1 , deglon1 , deglat2 , deglon2) result(dist)
real(real64) :: dist
real(real64) , intent(in) :: deglat1
Line 347 ⟶ 314:
real(real64) , intent(in) :: deglat2
real(real64) , intent(in) :: deglon2
real(real64) :: a
real(real64) :: c
Line 354 ⟶ 320:
real(real64) :: lat1
real(real64) :: lat2
! great circle distance
dlat = to_radian(deglat2 - deglat1)
Line 367 ⟶ 332:
subroutine sortem(airports)!Bubble sort them, nice and easy
type(airport) , intent(inout) , dimension(:) :: airports
integer :: i
integer :: k
logical :: swapped
type(airport) :: temp
swapped = .true.
k = size(airports%distance)
Line 386 ⟶ 349:
end do
end do
return
end subroutine sortem
!
subroutine tokenizes(input_string , tokens , num_tokens , delimiter)
character(*) , intent(in) :: input_string
character(*) , intent(out) , dimension(:) :: tokens
integer , intent(out) :: num_tokens
character(1) , intent(in) :: delimiter
integer :: end_idx
integer :: i
Line 404 ⟶ 364:
temp_string = trim(input_string)
start_idx = 1
do i = 1 , len_trim(temp_string)
if( (temp_string(i:i)==delimiter) )then
Line 416 ⟶ 375:
end if
end do
! Handle the last token
if( start_idx<=len_trim(temp_string) )then
Line 423 ⟶ 381:
& temp_string))
end if
end subroutine tokenizes
!
end module helpers
!
</syntaxhighlight>
{{out}}
<small>Scanning 7698 lines
Read complete, time taken = 93 milliseconds
Line 454 ⟶ 413:
8894 Rochester Airport United Kingdom EGTO 64.2 262
1399 Lille-Lesquin Airport France LFQQ 66.3 149
10747 Thurrock Airfield United Kingdom EGMT 68.4 272</small>
=={{header|J}}==
|