假定字符串长度输入到Fortran函数中 [英] Assumed string length input into a Fortran function

查看:1812
本文介绍了假定字符串长度输入到Fortran函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写下面的简单例程:

  program scratch 
character * 4 :: word
字='hell'
print *,concat(字)
结束程序暂存

函数concat(x)
字符*(*)x
concat = x //'plus stuff'
end function concat

程序应该是将字符串'hell'连接到字符串'plus stuff'。我希望这个函数能够接受任意长度的字符串(我打算使用'heaven'这个词),并将它连接到字符串'plus stuff'。



目前,当我在Visual Studio 2012上运行此代码时,出现以下错误:


错误1错误#6303:操作或二元
表达式操作对于两个
操作数的数据类型无效。 D:\aboufira\Desktop\TEMP\Visual
Studio \test\logicalfunction\scratch.f90 9


此错误适用于以下行:

  concat = x //'加上东西'

我不明白为什么这两个操作数不兼容。我把它们都设置为字符串。为什么他们不连接?

解决方案

高性能Mark's 评论告诉你为什么编译器抱怨:隐式键入。



函数 concat 的结果是隐式类型的,因为您尚未声明其类型。尽管 x //'plus stuff'是连接字符变量的正确方式,但您试图将该新字符对象分配给(隐含的)实际函数结果。 / p>

这导致了一个问题:我如何将函数结果声明为一个字符?。答案:和其他任何字符变量一样:

  character(len = length)concat 


[请注意,我使用字符(len = ...)而不是字符* ... 。我将在后面详细介绍为什么,但我还要指出,根据当前的Fortran,字符* 4 的形式已经过时,并且最终可能会被完全删除。 ]

棘手的部分是:它应该被声明为什么长度?



当声明长度我们不知道的字符函数结果有两种 1 方法:


  • an自动字符对象;
  • 延期长度字符对象。



在此功能的情况下,我们知道结果的长度比输入长10。我们可以声明

 字符(len = LEN(x)+10)concat 

为此,我们不能使用字符*(LEN(x)+10)形式。



在一般情况下,延期长度:

$ $ $ $ $ $ len = :),allocatable :: concat!递延长度,将在分配时定义

其中

  concat = x //'加东西'!在内在赋值上使用自动分配

使用这些形式增加了 concat 在主程序中有明确的接口。在其他问题和资源中你会发现很多。提供一个明确的接口也将消除在主程序中 concat 隐含地有实际结果的问题。



强调:

 程序
隐式无
字符(len = [something])concat
print *,concat('hell')
结束程序

不起作用对于 concat 具有编译时未知的长度结果。理想情况下,函数将是内部函数或从模块访问的函数。






1 还有第三种:假定的长度函数结果。任何想了解此信息的人都可以阅读此单独的问题。其他人都应该假装这不存在。就像Fortran标准的作者一样。

I am writing the following simple routine:

program scratch
    character*4 :: word
    word = 'hell'
    print *, concat(word)
end program scratch

function concat(x)
    character*(*) x
    concat = x // 'plus stuff'
end function concat

The program should be taking the string 'hell' and concatenating to it the string 'plus stuff'. I would like the function to be able to take in any length string (I am planning to use the word 'heaven' as well) and concatenate to it the string 'plus stuff'.

Currently, when I run this on Visual Studio 2012 I get the following error:

Error 1 error #6303: The assignment operation or the binary expression operation is invalid for the data types of the two operands. D:\aboufira\Desktop\TEMP\Visual Studio\test\logicalfunction\scratch.f90 9

This error is for the following line:

concat = x // 'plus stuff'

It is not apparent to me why the two operands are not compatible. I have set them both to be strings. Why will they not concatenate?

解决方案

High Performance Mark's comment tells you about why the compiler complains: implicit typing.

The result of the function concat is implicitly typed because you haven't declared its type otherwise. Although x // 'plus stuff' is the correct way to concatenate character variables, you're attempting to assign that new character object to a (implictly) real function result.

Which leads to the question: "just how do I declare the function result to be a character?". Answer: much as you would any other character variable:

character(len=length) concat

[note that I use character(len=...) rather than character*.... I'll come on to exactly why later, but I'll also point out that the form character*4 is obsolete according to current Fortran, and may eventually be deleted entirely.]

The tricky part is: what is the length it should be declared as?

When declaring the length of a character function result which we don't know ahead of time there are two1 approaches:

  • an automatic character object;
  • a deferred length character object.

In the case of this function, we know that the length of the result is 10 longer than the input. We can declare

character(len=LEN(x)+10) concat

To do this we cannot use the form character*(LEN(x)+10).

In a more general case, deferred length:

character(len=:), allocatable :: concat  ! Deferred length, will be defined on allocation

where later

concat = x//'plus stuff'  ! Using automatic allocation on intrinsic assignment

Using these forms adds the requirement that the function concat has an explicit interface in the main program. You'll find much about that in other questions and resources. Providing an explicit interface will also remove the problem that, in the main program, concat also implicitly has a real result.

To stress:

program
  implicit none
  character(len=[something]) concat
  print *, concat('hell')
end program

will not work for concat having result of the "length unknown at compile time" forms. Ideally the function will be an internal one, or one accessed from a module.


1 There is a third: assumed length function result. Anyone who wants to know about this could read this separate question. Everyone else should pretend this doesn't exist. Just like the writers of the Fortran standard.

这篇关于假定字符串长度输入到Fortran函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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