Fortran 创建要在消息日志记录中使用的重载函数

Fortran 创建要在消息日志记录中使用的重载函数,fortran,fortran90,Fortran,Fortran90,我正在尝试创建一个重载函数num2str(x),它将整数或实数作为输入并返回字符串值。我这样做的目的是在编写日志文件时使用它 基于我在上一篇文章()中给出的建议 我已经创建了一个子例程message(msglevel,string),我正在使用它来编写日志文件。现在我只能向这个函数发送一个字符串,我正在尝试使用num2str(x)轻松创建一个字符串 有人能解释一下我应该把代码放在哪里(在子程序中,在模块中),这样我就可以从任何地方访问它了。我看到了一个例子,但它在主程序中使用它,我不能这样做 请

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

基于我在上一篇文章()中给出的建议 我已经创建了一个子例程
message(msglevel,string)
,我正在使用它来编写日志文件。现在我只能向这个函数发送一个字符串,我正在尝试使用
num2str(x)
轻松创建一个字符串

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

请告诉我这种方法是否正确。我还想知道我是否可以修改
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

我肯定会选择一个模块:

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

谢谢大家的帮助。下面是我编写的代码,它的工作原理应该类似于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

您是否尝试在模块中定义并使用它,如果是,问题是什么?谢谢您的解释。我试图运行上述代码,但收到错误消息:
error 1 error\5082:语法错误,在位置
module procedure::num2str\u int
中应为:
之一时发现“:”。不知道怎么了。啊,我曾经遇到过一次。。。只需删除双冒号:
模块过程num2str\u int
,和
模块过程num2str\u real
。根据Fortran 2008标准(第12.4.3.2章),它们是可选的,但是
ifort
似乎有问题……我遇到了一个奇怪的错误。我正在读取密度值并使用
num2str()
打印它。由于某种原因,
write(tmp,”(F6.4)”number
没有将存储在number中的值写入tmp。请参阅下面的我的代码和错误消息:代码:
function num2str_real(number)implicit none real,intent(in)::number character(len=10)::tmp character(len=10)::num2str_real write(tmp),(F6.4);“number=”,number write(,)“tmp=,tmp num2str real=adjustl(trim(tmp))返回结束函数
RUN:
number=1025.000 tmp=**********密度=*********
由于格式说明符溢出而导致:
F6.4
由于短时间无法保存您的号码。您需要扩展说明符(和字符串长度)。谢谢。这修正了我的一些错误。我正在改进num2str()以显示数组,但我用于显示数组的代码没有显示任何输出。你能看一下吗<代码>函数num2str_数组(number,col)隐式非整数::i,col real,dimension(col)::数字字符(len=32)::tmp字符(len=33*col)::num2str_数组num2str_数组=''do i=1,col Write(tmp),(E)')number(i)num2str数组=num2str数组//adjustl(trim(tmp))/“,“Write(*,*)“num2str数组=”,num2str_数组end do return end函数需要更多帮助。当前发布的程序
num2str(x)
返回一个未修剪的字符串。因此,我需要使用
trim(num2str(x))
。有没有办法解决这个问题?有两个选项:a)在函数本身的规范部分确定并指定结果的确切长度,这样就没有尾随空格,或者b)使用延迟长度可分配字符(添加到Fortran 2003标准中的语言中)并在功能内部进行修剪。请给出您提到的选项
(b)
的示例。我认为选项
(a)
对我的情况不可行。