FSI 发表于 2005-10-19 09:03

F90 重载“+”“-”操作符

<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>

FSI 发表于 2005-10-19 11:16

昨天发错地方了,   那个删除了
页: [1]
查看完整版本: F90 重载“+”“-”操作符