<Example Program 1>
Module material_type_module
Type material
Character(5) :: name
Real :: young, poisson
End Type material
End Module
Program type_definition_example
Use material_type_module
Type (material) steel, concrete
steel%name = 'A36'
steel%young = 200.
steel%poisson = 0.3
concrete = material( 'A36',200.,0.3 )
Print *, steel
Print *, concrete
End Program
Execution Results
A36 2.0000000E+02 0.3000000 A36 2.0000000E+02 0.3000000
<Example Program 2>
Module point_type_module
Type point(k)
Integer, Kind :: k = Kind(0.0)
Real(k) :: x,y,z
End Type
End Module
Program example_2
Use point_type_module
Type(point) :: spx = point(1.0,2.0,3.0)
Type(point(Kind(0d0)))
Print *, spx
dpy = point(Kind(0d0))(Sqrt(1d0), Sqrt(2d0), Sqrt(3d0))
Print *, dpy
End Program
Execution Results
1.0000000 2.0000000 3.0000000 1.0000000000000000 1.4142135623730951 1.7320508075688772
<Example Program 3>
Module vector_type_module
Type vector(n)
Integer, Len :: n
Real :: v(n)
Contains
Procedure :: dotp
Generic :: Operator(*) => dotp
End Type
Contains
Real Function dotp(a,b)
Use Ieee_Arithmetic
Class(vector(*)),Intent(In) :: a,b
If (a%n/=b%n) Then
If (.Not.Ieee_Support_Nan(dotp) .Or..Not.Ieee_Support_Flag(Ieee_Invalid)) &
Error Stop 'dotp: inconsistent vector lengths'
Call Ieee_Set_Flag(Ieee_Invalid,.True.)
dotp = Ieee_Value(dotp,Ieee_Quiet_NaN)
Else
dotp = Dot_Product(a%v,b%v)
End If
End Function
End Module
Program example_3
Use vector_type_module
Type(vector(3)) :: x = vector(3)([1.0,2.0,3.0])
Type(vector(3)) :: y = vector(3)([-4.0,-4.0,-4.0])
Print *, 'x =', x
Print *, 'y =', y
Print *, 'x*y =', x*y
End Program
Execution Results
x = 1.0000000 2.0000000 3.0000000 y = -4.0000000 -4.0000000 -4.0000000 x*y = -24.0000000