马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
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> |