!Complex.Java Fortran90 Conversion !Module containing complex methods and type definitions module complex_module !Define complex type type public_complex real :: re, im end type public_complex !Add the functions for complex mathematics contains !Add two complex numbers function add(a,b) implicit none type (public_complex), intent(in) :: a,b type (public_complex) :: add add%re = a%re + b%re add%im = a%im + b%im end function add !Subtract b from a complex numbers function subtract(a,b) implicit none type (public_complex), intent(in) :: a,b type (public_complex) :: subtract subtract%re = a%re - b%re subtract%im = a%im - b%im end function subtract !Multiply two complex numbers function multiply(a,b) implicit none type (public_complex), intent(in) :: a,b type (public_complex) :: multiply multiply%re = a%re * b%re - a%im * b%im multiply%im = a%re * b%im + a%im * b%re end function multiply !Divide two complex numbers function divide(a,b) implicit none type (public_complex), intent(in) :: a,b type (public_complex) :: divide divide%re = (a%re * b%re + a%im * b%im)/(b%re**2 + b%im**2) divide%im = (a%im * b%re - a%re * b%im)/(b%re**2 + b%im**2) end function divide end module complex_module !Begin main program program main_program use complex_module implicit none type(public_complex) :: a,b,c character ch a%re=1. a%im=2. b%re=3. b%im=4. if(a%im>=0.0) then ch = '+' else if(a%im<0.0) then ch = '-' end if write(*,*) 'a = ', a%re, ch, abs(a%im), 'i' if(b%im>=0.0) then ch = '+' else if(b%im<0.0) then ch = '-' end if write(*,*) 'b = ', b%re, ch, abs(b%im), 'i' c = add(a,b) if(c%im>=0.0) then ch = '+' else if(c%im<0.0) then ch = '-' end if write(*,*) 'a + b = ', c%re, ch, abs(c%im),'i' c = subtract(a,b) if(c%im>=0.0) then ch = '+' else if(c%im<0.0) then ch = '-' end if write(*,*) 'a - b = ', c%re, ch, abs(c%im),'i' c = multiply(a,b) if(c%im>=0.0) then ch = '+' else if(c%im<0.0) then ch = '-' end if write(*,*) 'a * b = ', c%re, ch, abs(c%im),'i' if(b%re==0. .AND. b%im==0.) then write(*,*) 'Error: division by zero ',b%re,'+',b%im,'i' else c = divide(a,b) if(c%im>=0.0) then ch = '+' else if(c%im<0.0) then ch = '-' end if write(*,*) 'a / b = ', c%re, ch, abs(c%im),'i' end if end program main_program