Fortran Coder

查看: 6514|回复: 4
打印 上一主题 下一主题

[讨论] 如何判断两个无限多态变量相等

[复制链接]

128

帖子

36

主题

1

精华

大师

F 币
1159 元
贡献
594 点
跳转到指定楼层
楼主
发表于 2019-9-26 20:36:03 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 weixing1531 于 2019-9-27 10:40 编辑

这种方法好像很麻烦
[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
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-15 01:32

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表