主题:现在fortran可否实现typealias功能
有的子程序 实数,整数,和复数 都是一样的,
只是程序的变量类型不一样,是否有类似typealias的功能,
如何实现?
下面是typealias的例子,但编译器还没实现这个功能,是否有代替的功能?
1 subroutine sort(IOa, INMax)
2 type(atype), dimension(:), intent(inout):: IOa
3 integer, intent(in):: INMax
4 type(atype):: temp
5 integer:: ii, jj, mini
6
7 do ii = 1, INmax - 1
8 mini = ii
9 do jj = ii + 1, INMax
10 if (IOa(mini) .gt. IOa(jj)) mini = jj
11 end do
12
13 if (mini .ne. ii) then
14 temp = IOa(ii)
15 IOa(ii) = IOa(mini)
16 IOa(mini) = temp
17 end if
18 end do
19 return
20 end subroutine
1 module ModReal
2 typealias:: atype=>real
3 contains
4 include 'sorta.inc'
5 end module
6
7 module ModInt
8 typealias:: atype=>integer
9 contains
10 include 'sorta.inc'
11 end module
12
13 program main
14 use ModReal, only: sortreal=>sort
15 use ModInt, only: sortint=>sort
16 integer, parameter:: MAXAA = 10
17 real, dimension(MAXAA):: ar
18 integer, dimension (MAXAA):: ai
19 integer:: ii
20
21 ar = (/ 7., 4., 1., 8., 5., 2., 9., 6., 3., 0. /)
22 ai = (/ 3, 6, 9, 2, 5, 8, 1, 4, 7, 0/)
23
24 print '(A,10I7)', 'before', (ai(ii), ii = 1, MAXAA)
25 call sortint(ai, MAXAA)
26 print '(A,10I7)', 'after ', (ai(ii), ii = 1,
27 MAXAA)
28 print '(A,10F7.2)', 'before', (ar(ii), ii
29 = 1, MAXAA)
30 call sortreal(ar, MAXAA)
31 print '(A,10F7.2)', 'after ', (ar(ii), ii = 1, MAXAA)
32 stop
33 end
只是程序的变量类型不一样,是否有类似typealias的功能,
如何实现?
下面是typealias的例子,但编译器还没实现这个功能,是否有代替的功能?
1 subroutine sort(IOa, INMax)
2 type(atype), dimension(:), intent(inout):: IOa
3 integer, intent(in):: INMax
4 type(atype):: temp
5 integer:: ii, jj, mini
6
7 do ii = 1, INmax - 1
8 mini = ii
9 do jj = ii + 1, INMax
10 if (IOa(mini) .gt. IOa(jj)) mini = jj
11 end do
12
13 if (mini .ne. ii) then
14 temp = IOa(ii)
15 IOa(ii) = IOa(mini)
16 IOa(mini) = temp
17 end if
18 end do
19 return
20 end subroutine
1 module ModReal
2 typealias:: atype=>real
3 contains
4 include 'sorta.inc'
5 end module
6
7 module ModInt
8 typealias:: atype=>integer
9 contains
10 include 'sorta.inc'
11 end module
12
13 program main
14 use ModReal, only: sortreal=>sort
15 use ModInt, only: sortint=>sort
16 integer, parameter:: MAXAA = 10
17 real, dimension(MAXAA):: ar
18 integer, dimension (MAXAA):: ai
19 integer:: ii
20
21 ar = (/ 7., 4., 1., 8., 5., 2., 9., 6., 3., 0. /)
22 ai = (/ 3, 6, 9, 2, 5, 8, 1, 4, 7, 0/)
23
24 print '(A,10I7)', 'before', (ai(ii), ii = 1, MAXAA)
25 call sortint(ai, MAXAA)
26 print '(A,10I7)', 'after ', (ai(ii), ii = 1,
27 MAXAA)
28 print '(A,10F7.2)', 'before', (ar(ii), ii
29 = 1, MAXAA)
30 call sortreal(ar, MAXAA)
31 print '(A,10F7.2)', 'after ', (ar(ii), ii = 1, MAXAA)
32 stop
33 end