方式Fortran处理空的空间 [英] way Fortran handles empty spaces

查看:188
本文介绍了方式Fortran处理空的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想澄清一下Fortran处理字符串中空字符的方式。
让我们假设我们有这种情况:

$ $ $ $ $ $ $ $ $
$ $ b test('AB')
end program

其中


$ b $

 函数测试(名称)
隐式无
字符(10)::名称
字符(3):: cutname

写(*,*)' - '// name //' - '!给出输出-AB -
!空格已添加到最后
cutname(1:3)= name(1:3)
write(*,*)'1 - '// cutname //' - '!给出输出-AB -
!看起来有一个空间,然后在最后
! cutname

write(*,*)(cutname(1:2)=='AB')!给出输出T(真)
写(*,*)(cutname(3:3)=='')!给出输出F(假)
写(*,*)(cutname =='AB')!给出输出F(false)

结束函数

我很好奇这种情况发生了什么。
在此先感谢。

解决方案

Fortran中的标准字符串是固定长度的。如果你不使用整个字符串,它们会以空格/空格结尾。



我改变了你的示例程序来通过gfortran和ifort的编译器检查。你的函数没有回报,所以比一个子程序更好。编译器注意到实际参数和伪参数的长度不一致 - 因为我将过程放入模块中,使用编辑它,以便编译器可以检查参数一致性。他们抱怨将长度为2的字符串传递给长度为10的字符串。剩下的字符应该如何定义?

  module test_mod 

包含

子程序测试(名称)
implicit none
character(10):: name
character(3):: cutname

write(*,*)' - '// name //' - '!给出输出-AB -
!空格已添加到最后
cutname(1:3)= name(1:3)
write(*,*)'1 - '// cutname //' - '!给出输出-AB -
!看起来有一个空间,然后在最后
! cutname

write(*,*)(cutname(1:2)=='AB')!给出输出T(真)
写(*,*)(cutname(3:3)=='')!给出输出F(假)
写(*,*)(cutname =='AB')!给出输出F(假)

结束子程序测试

结束模块test_mod



程序主
使用test_mod
隐式无

调用测试('AB')
结束程序

当我运行这个版本时,输出是T,T和T,这就是我所期望的。编辑:我建议使用full编译器的警告和错误检查选项。这就是我很快发现这个例子的问题。使用gfortran: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsprising -Waliasing -Wimplicit-interface -Wunused-parameter -fwhole-file -fcheck = all -std = f2008 - pedantic -fbacktrace

字符串赋值语句不要求双方具有相同的长度。如果RHS比LHS上的字符串变量短,它将在空白处结束填充。在这里,论据应该一致,包括长度。

I would like to have some clarifications about the way Fortran handles "empty" characters in strings. Let us assume we have this situation:

program main
implicit none

test('AB')
end program

where

function test(name)
implicit none
character(10) :: name
character(3)  :: cutname

write(*,*) '-'//name//'-'             ! Gives output "-AB        -" 
                                      ! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-'           ! Gives output "-AB -"
                                        ! It seems there is a space then at the end
                                        ! of cutname

write(*,*)  (cutname(1:2) == 'AB')      ! Gives output  T (true)
write(*,*)  (cutname(3:3) == ' ')       ! Gives output  F (false)
write(*,*)  (cutname  == 'AB ')         ! Gives output  F (false)

end function

I am pretty curious about what is happening in this case. Thanks in advance.

解决方案

Standard strings in Fortran are fixed length. If you don't use the entire string, they are padded on the end with blanks/spaces.

I altered your example program to pass compiler checks of gfortran and ifort. Your function had no return, so better as a subroutine. The compilers noticed the inconsistency between the lengths of the actual and dummy argument -- because I put the procedure into a module and useed it so that the compiler could check argument consistency. They complain about passing a length 2 string to a length 10 string. How are the remaining characters supposed to be defined?

module test_mod

contains

subroutine test(name)
implicit none
character(10) :: name
character(3)  :: cutname

write(*,*) '-'//name//'-'             ! Gives output "-AB        -"
                                      ! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-'           ! Gives output "-AB -"
                                        ! It seems there is a space then at the end
                                        ! of cutname

write(*,*)  (cutname(1:2) == 'AB')      ! Gives output  T (true)
write(*,*)  (cutname(3:3) == ' ')       ! Gives output  F (false)
write(*,*)  (cutname  == 'AB ')         ! Gives output  F (false)

end subroutine test

end module test_mod



program main
use test_mod
implicit none

call test('AB          ')
end program

When I run this version, the outputs are T, T and T, which is what I expect.

EDIT: I suggest using full warning and error checking options of your compiler. That is how I quickly found the issues with the example. With gfortran: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsurprising -Waliasing -Wimplicit-interface -Wunused-parameter -fwhole-file -fcheck=all -std=f2008 -pedantic -fbacktrace.

A string assignment statement doesn't require the two sides to have the same lengths. If the RHS is shorter than the string variable on the LHS, it will get padded on the end with blanks. Here, the arguments should be consistent, including in length.

这篇关于方式Fortran处理空的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆