2013-11-01 18 views
2

我想创建一个重载函数num2str(x)将于整数或实值作为输入,并返回一个字符串值中使用。我这样做的目的是在写日志文件时使用它。创建一个重载函数在消息记录

根据我以前的文章(creating log file)给出的建议 我创建了一个子程序message(msglevel, string),我正在使用它来编写我的日志文件。现在我只能发送一个字符串到这个函数,我正在试图使用num2str(x)来创建一个字符串变得很容易。

有人能解释我,我应该在哪里把这个代码(在子程序,模块中),所以我可以从任何地方访问它。我看到了这个example,但它在主程序中使用它,我不能这样做。

请让我知道,如果这种做法是correct.I也想知道我是否可以修改num2str(x)为数组变量返回的字符串。

!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 
+0

你有没有尝试在模块中定义它并从那里使用它,如果是的话,是什么问题? – milancurcic

回答

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

谢谢你的解释。我试图运行上面的代码,但得到错误信息:'错误错误#5082:语法错误,发现“::”期待当一个:在位置'模块程序:: num2str_int' '。不知道什么是错的。 – Amitava

+0

啊,我遇到过那个......只要删除双冒号:'module procedure num2str_int'和'module procedure num2str_real'。根据Fortran 2008标准(第12.4.3.2节),它们是可选的,但是'ifort'似乎对它们有问题... –

+0

我从代码中删除了冒号... –

0

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

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

需要更多帮助。当前发布的程序'num2str(x)'返回一个未被修剪的字符串。因此,我需要使用'trim(num2str(x))'。有没有什么办法解决这一问题? – Amitava

+0

两种选择:a)确定并指定函数本身的规范部分中的结果的确切长度,以便不存在尾随空白或b)使用延迟长度可分配字符(添加到Fortran 2003标准中的语言中)并在功能内部进行修剪。 – IanH

+0

您能否举一个例子来说明您提到的(b)选项。我不认为选项'(a)'对我的情况是可行的。 – Amitava