[Fortran] 纯文本查看 复制代码
program main
implicit none
write(*,*)EqualClass(1,2)
write(*,*)EqualClass(1,1)
write(*,*)EqualClass(1.0,1.001)
write(*,*)EqualClass(1.0,1.0)
write(*,*)EqualClass('abc','abcd')
write(*,*)EqualClass('abc','abc')
write(*,*)EqualClass(.true.,.false.)
write(*,*)EqualClass(.true.,.true.)
contains
pure logical function EqualClass(self, other)
class(*), intent(in) :: self
class(*), intent(in) :: other
EqualClass=.false. !初值为假
select type(self)
type is(Integer)
select type(other)
type is(Integer)
EqualClass=(self==other)
end select
type is(Real)
select type(other)
type is(Real)
EqualClass=(abs(self-other)<1.0e-5)
end select
type is(Character(*))
select type(other)
type is(Character(*))
EqualClass=(self==other)
end select
type is(Logical)
select type(other)
type is(Logical)
EqualClass=(self.eqv.other)
end select
end select
end function
end program