声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2250|回复: 2

[Fortran] F90 重载“+”“-”操作符

[复制链接]
发表于 2005-10-19 09:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
<DIV class=quote>!! F90 重载“+”“-”操作符<BR>
<P><BR><BR>module class_vector3<BR>  implicit none</P>
<P>  type vector3<BR>     integer :: idx<BR>     real :: x<BR>     real :: y<BR>     real :: z<BR>  end type vector3</P>
<P>  interface operator(+)<BR>     module procedure vector_add<BR>  end interface</P>
<P>  interface operator(-)<BR>     module procedure vector_sub<BR>  end interface</P>
<P>contains</P>
<P>  function vector_add(v1, v2) result(v3)<BR>    implicit none<BR>    type(vector3), intent(in) :: v1, v2<BR>    type(vector3) :: v3<BR>    v3%idx = v1%idx + v2%idx<BR>    v3%x = v1%x + v2%x<BR>    v3%y = v1%y + v2%y<BR>    v3%z = v1%z + v2%z<BR>  end function vector_add</P>
<P>  function vector_sub(v1, v2) result(v3)<BR>    implicit none<BR>    type(vector3), intent(in) :: v1, v2<BR>    type(vector3) :: v3<BR>    v3%idx = v1%idx - v2%idx<BR>    v3%x = v1%x - v2%x<BR>    v3%y = v1%y - v2%y<BR>    v3%z = v1%z - v2%z<BR>  end function vector_sub</P>
<P>end module class_vector3</P>
<P><BR>program main<BR>  use class_vector3<BR>  implicit none<BR>  <BR>  type(vector3) :: x1, x2, x3</P>
<P>  x1 = vector3(1, 5.1, 0.2, -4.2)<BR>  x2 = vector3(10, -4.1, 3.1003, 5.129)</P>
<P>  x3 = x1 - x2</P>
<P>  print *, x3</P>
<P>  x3 = x1 + x2</P>
<P>  print *, x3</P>
<P>end program main<BR></P></DIV>
回复
分享到:

使用道具 举报

发表于 2005-10-19 09:15 | 显示全部楼层

回复:(FSI)F90 重载“+”“-”操作符

<P>昨天就见发了,今天重新发?</P>
 楼主| 发表于 2005-10-19 11:16 | 显示全部楼层
昨天发错地方了,[em04]   那个删除了
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-4-27 22:35 , Processed in 0.169436 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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