迭代 FORTRAN 字符数组

发布于 2024-11-14 07:55:55 字数 818 浏览 4 评论 0原文

好吧,我在使用以下 Fortran 90 代码时遇到了很多麻烦。程序测试人员应该创建一个名为 input 的字符数组,将所有条目初始化为空格字符,然后从用户那里获取一些字符串并将其存储在 input 中。 getLength 函数应该返回函数中最后一个非空格的索引;因此,如果用户输入字符串“Hello, how're you?”,则 getLength(input) 应返回 11。它的工作方式应该是从给定数组的末尾开始,并标记第一个非空格字符出现的位置。当我实际尝试运行它时,gfortran 说:“错误:(1) (REAL(4)/INTEGER(4)) 处函数 getLength 的返回类型不匹配。这是什么意思,我做错了什么,以及如何做我应该修复它吗?

    program tester
    implicit none
    character(len = 1000) :: input
    external getLength
    do i = 1, 1000
        input(i) = " " 
    end do
    read *, input
    print *, getLength(input)
    end program

    integer function getLength(array) result (length)
    character(len=1000) :: array
    integer :: lenTemp = 1000
    do while (array(lenTemp:lenTemp) == ' ')
        lenTemp = lenTemp - 1
    end do
    length = lenTemp
    end function

Ok, I'm having mucho trouble with the following Fortran 90 code. The program tester should create a character array called input, initialize all the entries to the space character, then get some string from the user and store it in input. The getLength function is supposed to return the last index in the function that isn't a space; so if the user entered the string "Hello, how're you?", then getLength(input) should return 11. It's supposed to work by starting at the end of the given array, and marking where the first non-space character occurs. When I actually try to run it, gfortran says: "Error: Return type mismatch of function getLength at (1) (REAL(4)/INTEGER(4)). What does that mean, what am I doing doing wrong, and how should I fix it? Thanks in advance!

    program tester
    implicit none
    character(len = 1000) :: input
    external getLength
    do i = 1, 1000
        input(i) = " " 
    end do
    read *, input
    print *, getLength(input)
    end program

    integer function getLength(array) result (length)
    character(len=1000) :: array
    integer :: lenTemp = 1000
    do while (array(lenTemp:lenTemp) == ' ')
        lenTemp = lenTemp - 1
    end do
    length = lenTemp
    end function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2024-11-21 07:55:55

看起来您没有声明外部函数 getLength 的类型,因此编译器假设 getLength 是默认类型 REAL(4)。考虑将 external getLength 替换为 INTEGER、EXTERNAL :: getLength 等类型。

此外,为了更加清晰起见,在函数定义中包含 IMPLICIT NONE 可能不会有什么坏处。

编辑:此外,根据 Fortran 处理字符串的方式,您应该简单地将变量输入初始化为空字符串或一个空格。当您没有完全填充字符串时,Fortran 总是会用空格填充字符串的其余部分。您确实应该执行诸如 CHARACTER(LEN=1000) :: input = '' 之类的操作来正确初始化 input

it doesn't look like you're declaring the type of your external function getLength so the compiler is assuming that getLength is of the default type REAL(4). Consider swapping external getLength for something with a type like INTEGER, EXTERNAL :: getLength.

Also, it probably wouldn't hurt to include an IMPLICIT NONE inside the function definition for extra clarity.

EDIT: Additionally, with the way Fortran handles character strings, you should simply initialize the variable input to either an empty string or one blank space. Fortran will always fill the remainder of a string with blanks when you don't fill it completely. You really should do something like CHARACTER(LEN=1000) :: input = '' to properly initialize input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文