迭代 FORTRAN 字符数组
好吧,我在使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您没有声明外部函数
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 thatgetLength
is of the default typeREAL(4)
. Consider swappingexternal getLength
for something with a type likeINTEGER, 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 initializeinput
.