2

I am trying to create an overloaded function num2str(x) which will take integer or real values as input and return a string value. My purpose of doing this is to use it when writing log file.

Based on suggestions given in my previous post (creating log file) I have created a subroutine message(msglevel, string) which I am using to write my log file. Now I can only send a string to this function and I am trying to make it easy to create a string using num2str(x).

Could someone explain me where should I place this code (In a subroutine, in a module) so I can access it from everywhere. I saw an example of this, but it uses it in the main program, which I can't do.

Please let me know if this approach is correct.I would also like to know if I can modify num2str(x) to return string for array variables.

!GLOBAL FUNCTIONS
interface num2str
    function num2str_int(number)
        integer,intent(in)::number
        character(len=*)::num2str_int
    end function
    character function num2str_real(number)
        real::number
        character(len=*)::num2str_real
    end function
end interface 
function num2str_int(number)
    implicit none
    integer,intent(in)::number
    character(len=*)::num2str_int
    write(num2str_int,'(I)')number
    return
end function
character function num2str_real(number)
    implicit none
    real,intent(in)::number
    character(len=*)::num2str_real
    write(num2str_real,'(F6.4)')number
    return
end function
4

2 回答 2

3

我肯定会选择一个模块:

module strings

  ! GLOBAL FUNCTIONS
  public :: num2str

  ! Everything else is private
  private

  interface num2str
    module procedure num2str_int
    module procedure num2str_real
  end interface 

contains

  function num2str_int(number)
      implicit none
      integer,intent(in) :: number
      character(len=6)   :: num2str_int
      character(len=6)   :: tmp

      write(tmp,'(I6)')number
      num2str_int = tmp
  end function

  function num2str_real(number)
      implicit none
      real,intent(in)    :: number
      character(len=6)   :: num2str_real
      character(len=6)   :: tmp

      write(tmp,'(F6.4)')number
      num2str_real = tmp
  end function
end module

program test_strings
  use strings

  write(*,*) num2str(1)//' '//num2str(1.23)  
end program
于 2013-11-01T20:47:33.103 回答
0

谢谢大家的帮助。这是我编写的代码,它应该类似于num2str()在 MATLAB 中工作。它还具有显示数组和矩阵的附加功能。

module module_common_functions
 !GLOBAL FUNCTIONS
  public :: num2str

  interface num2str
      module procedure num2str_int
      module procedure num2str_real
      module procedure num2str_array
      module procedure num2str_matrix
  end interface

contains

function num2str_int(number)
    implicit none
    integer,intent(in)::number
    character(len=10)::num2str_int
    character(len=10):: tmp
    write(tmp,'(I6)')number
    num2str_int=trim(adjustl(tmp))
    return
end function

function num2str_real(number)
    implicit none
    real,intent(in)::number
    character(len=32):: tmp
    ! Deferred length allocatable character result.
    character(len=:), allocatable ::num2str_real
    ! Format specifier changed to use full length.
    write(tmp,'(F32.4)')number
    ! Reallocation on assignment means length of result will 
    ! be set to match length of right hand side expression.
    ! Note ordering of trim and adjustl.
    num2str_real=trim(adjustl(tmp))
end function

function num2str_array(number,col)
    implicit none
    integer::i,col
    real, dimension(col)::number
    character(len=32):: tmp
    character(len=33*col)::num2str_array
    num2str_array =''
    do i=1,col
        write(tmp,'(F12.4)') number(i)
        num2str_array = trim(num2str_array)//adjustl(trim(tmp))//','
    end do
    return
end function 

function num2str_matrix(number,row,col)
    implicit none
    integer::i,j,row,col
    real,dimension(row,col)::number
     character(len=32):: tmp
    character(len=33*row*col)::num2str_matrix
    num2str_matrix=''

    do i=1,row
        do j=1,col
            write(tmp,'(F12.4)') number(i,j)
            num2str_matrix= trim(num2str_matrix)//adjustl(trim(tmp))//','
        end do
    end do
    return
end function


end module
于 2013-11-04T17:25:06.500 回答