fortran OOP(3) class_Rational.f90

module class_Rational                ! filename: class_Rational.f90
implicit none
 
! public, everything but following private subprograms
 
private :: gcd, reduce
   
type Rational
     
private ! numerator and denominator
     
integer :: num, den ; end type Rational

          
! overloaded operators interfaces
   
interface assignment (=)
     
module procedure equal_Integer ; end interface
   interface operator 
(+)          ! add unary versions & (-) later
     
module procedure add_Rational ; end interface
   interface operator 
(*)          ! add integer_mult_Rational, etc 
     
module procedure mult_Rational ; end interface
   interface operator 
(==)
     
module procedure is_equal_to ; end interface
contains                      
! inherited operational functionality

 
function add_Rational (a, b) result (c)       ! to overload +
  
type (Rational), intent(in) :: a, b          ! left + right
  
type (Rational)             :: c     
   c % num = a % num*b % den + a % den*b % num
   c % den = a % den*b % den 
   
call reduce (c) ; end function add_Rational 
  
 
function convert (nameresult (value! rational to real
  
type (Rational), intent(in) :: name
  real                        
:: value  ! decimal form
   
value float(name % num)/name % den ; end function convert 

 
function copy_Rational (nameresult (new)
  
type (Rational), intent(in) :: name
  type 
(Rational)             :: new 
   new % num = 
name % num
   new % den = 
name % den ; end function copy_Rational 

 
subroutine delete_Rational (name)     ! deallocate allocated items
  
type (Rational), intent(inout) :: name      ! simply zero it here
   
name = Rational (0, 1) ; end subroutine delete_Rational 

 
subroutine equal_Integer (new, I) ! overload =, with integer
  
type (Rational), intent(out) :: new  ! left  side of operator
  
integer,         intent(in)  :: I    ! right side of operator
   
new % num = I ; new % den = 1 ; end subroutine equal_Integer  

 
recursive function gcd (j, k) result (g) ! Greatest Common Divisor
  
integerintent(in) :: j, k ! numerator, denominator
  
integer             :: g
   
if ( k == 0 ) then ; g = j
   
else ; g = gcd ( k, modulo(j,k) )           ! recursive call
   
end if end function gcd

 
function  get_Denominator (nameresult (n)   ! an access function
  
type (Rational), intent(in) :: name
  integer                     
:: n             ! denominator
   
n = name % den ; end function  get_Denominator

 
function  get_Numerator (nameresult (n)     ! an access function
  
type (Rational), intent(in) :: name
  integer                     
:: n             ! numerator
   
n = name % num ; end function  get_Numerator 

 
subroutine  invert (name)         ! rational to rational inversion
  
type (Rational), intent(inout) :: name
  integer                        
:: temp
   temp       = 
name % num 
   
name % num = name % den 
   
name % den = temp ; end subroutine invert 

 
function is_equal_to (a_given, b_given) result (t_f)      ! for ==
  
type (Rational), intent(in) :: a_given, b_given  ! left == right
  
type (Rational)             :: a, b              ! reduced copies 
  
logical                     :: t_f
   a = copy_Rational (a_given) ; b = copy_Rational (b_given)
   
call reduce(a) ; call reduce(b)        ! reduced to lowest terms
   
t_f = (a%num == b%num) .and. (a%den == b%den) ; end function 
  
 subroutine 
list(name)                 ! as a pretty print fraction
  
type (Rational), intent(in) :: name
   print 
*, name % num, "/"name % den ; end subroutine list

 
function make_Rational (numerator, denominator) result (name)
 
!        Optional Constructor for a rational type
  
integeroptionalintent(in) :: numerator, denominator         
  
type (Rational)               :: name 
   name 
= Rational(0, 1)                            ! set defaults
   
if present(numerator)  ) name % num = numerator 
   
if present(denominator)) name % den = denominator
   
if name % den == 0     ) name % den = 1        ! now simplify 
   
call reduce (name) ; end function make_Rational

 
function  mult_Rational (a, b) result (c)          ! to overload *
  
type (Rational), intent(in) :: a, b
  
type (Rational)             :: c 
   c % num = a % num * b % num 
   c % den = a % den * b % den 
   
call reduce (c) ; end function mult_Rational 
  
 
function Rational_ (numerator, denominator) result (name)
 
!        Public Constructor for a rational type
  
integeroptionalintent(in) :: numerator, denominator         
  
type (Rational)               :: name
   if 
( denominator == 0 ) then name = Rational (numerator, 1)
   
else name = Rational (numerator, denominator) ; end if
 end function 
Rational_

 
subroutine reduce (name)               ! to simplest rational form
  
type (Rational), intent(inout) :: name
  integer                        
:: g     ! greatest common divisor
   
g          = gcd (name % num, name % den)
   
name % num = name % num/g
   
name % den = name % den/g ; end subroutine reduce
end module class_Rational
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yueliang2100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值