Fortran:如何读取文本文件每一行的第一个字符?
这是我第一次尝试用 Fortran 编程。我正在尝试编写一个程序,打印 斐波那契数列 的前 1476 项,然后检查每一项的第一个数字并存储数组中出现的 1、2、3、...、9 的数量。
我似乎无法弄清楚的问题是如何读取每个术语的第一个数字。我已经尝试过几种方法,但由于我对 Fortran 技术的了解有限,我遇到了困难。我将这些术语写入一个文本文件,其想法是读取每行的第一个数字并在数组中累积相应的数字。有人对如何做到这一点有任何建议吗?
这是到目前为止我的代码:(
编辑:我包含了用于读取文件的代码。现在它只打印出 3.60772951994415996E-313, 这看起来像是某种地址,因为它不是斐波那契数列之一。另外,它是唯一打印的内容,我希望它会打印出文件的每一行...)
(编辑编辑:考虑到这一点后,也许有一种方法可以将写入格式设置为有没有办法将实数的有效位数设置为 1?
subroutine writeFib(n)
integer :: i
real*8 :: prev, current, newFib
prev = 0
current = 1
do i = 1, n
newFib = prev + current
prev = current
current = newFib
write(7,*) newFib
end do
return
end subroutine
subroutine recordFirstDigits(a)
integer :: openStat, inputStat
real*8 :: fibNum
open(7, file = "fort.7", iostat = openStat)
if (openStat > 0) stop "*** Cannot open the file ***"
do
read(7, *, iostat = inputStat) fibNum
print *,fibNum
if (inputStat > 0) stop "*** input error ***"
if (inputStat < 0) exit ! end of file
end do
close(7)
end subroutine
program test
integer :: k, a(9)
k = 1476
call writeFib(k)
call recordFirstDigits(a)
end program
this is my first time trying to program in Fortran. I'm trying to write a program that prints the first 1476 terms of the Fibonacci sequence, then examines the first digit of each term and stores the number of 1s, 2s, 3s, ..., 9s that occur in an array.
The problem that I can't seem to figure out is how to read the first digit of each term. I've tried several things but am having difficulty with my limited knowledge of Fortran techniques. I write the terms to a text file and the idea is to read the first digit of each line and accumulate the respective number in the array. Does anyone have any suggestions of how to do this?
Here is my code so far:
(edit: I included the code I have for reading the file. Right now it just prints out 3.60772951994415996E-313,
which seems like an address of some sort, because it's not one of the Fibonacci numbers. Also, it is the only thing printed, I expected that it would print out every line of the file...)
(edit edit: After considering this, perhaps there's a way to format the writing to the text file to just the first digit. Is there a way to set the number of significant digits of a real number to one? :P)
subroutine writeFib(n)
integer :: i
real*8 :: prev, current, newFib
prev = 0
current = 1
do i = 1, n
newFib = prev + current
prev = current
current = newFib
write(7,*) newFib
end do
return
end subroutine
subroutine recordFirstDigits(a)
integer :: openStat, inputStat
real*8 :: fibNum
open(7, file = "fort.7", iostat = openStat)
if (openStat > 0) stop "*** Cannot open the file ***"
do
read(7, *, iostat = inputStat) fibNum
print *,fibNum
if (inputStat > 0) stop "*** input error ***"
if (inputStat < 0) exit ! end of file
end do
close(7)
end subroutine
program test
integer :: k, a(9)
k = 1476
call writeFib(k)
call recordFirstDigits(a)
end program
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
虽然建议已经到位,但也有一些被遗忘的事情。 REAL 类型的范围,以及一些格式问题。
无论如何,这是一个修补后的解决方案,已编译并可运行,因此请尝试看看这是否适合您。我冒昧地选择了自己的斐波那契数计算方法。
噢,...我刚刚读到您需要将位数存储到数组中。虽然我只是数了一下。
哦,好吧,“留给读者作为练习......”:-)
Although the suggestions were in place, there were also several things that were forgotten. Range of the REAL kind, and some formatting problems.
Anyways, here's one patched up solution, compiled and working, so try to see if this will work for you. I've took the liberty of choosing my own method for fibonacci numbers calculation.
Aw, ... I just read you need the number of digits stored in to an array. While I just counted them.
Oh well, ... "left as an exercise for the reader ..." :-)
你能用 FORMAT(A1) 阅读吗? 20 年过去了,我已经不记得具体的语法了。
Can you read with a FORMAT(A1)? It's been 20 years so I don't remember the exact syntax.
我想知道为什么当文件 7 尚未关闭时 open 语句会成功。我认为您在写入和读取之间需要一个 endfile 语句和/或一个 rewind 语句。
Paul Tomblin 发布了在解决了让读取工作的问题之后您必须做的事情。
I wonder why the open statement succeeds when file 7 hasn't been closed. I think you need an endfile statement and/or a rewind statement in between writing and reading.
Paul Tomblin posted what you have to do after you solve your problem in getting reads to work in the first place.
您没有显示
!代码在这里阅读...
这使得很难猜测你做错了什么:-)也许你需要一个循环来读取每一行,然后在有时跳出循环到 continue 语句没有更多的线路。
像这样的东西:
更好 - 看看 此 revcomp 计划中使用的更现代的风格。
You don't show the
! code to read here...
which makes it kind of difficult to guess what you are doing wrong :-)Perhaps you need a loop to read each line and then jump out of the loop to a continue statement when there are no more lines.
Something like this:
Better still - look at the more modern style used in this revcomp program.
这里有一些提示:
这个问题的文件 I/O 少得多
(除非你忘记声明
必须创建文件)。
地板(10^(frac(log_10(7214989861293412))))
(将其放入 Wolfram Alpha 中看看它的作用。)
Fortran 语言很简单
循环内的算术
构造——至少是解决方案的第一遍。
去吧。这个建议甚至适用于你的性格驱动方法。 (这个问题非常适合
提出了一个可爱的索引
计划为您的统计数据,但有些
人们讨厌可爱的计划
编程。如果你不害怕
可爱……那么你就可以有联想
Fortran 中的数组只要你
键是整数;-)
问题是你将要使用的数据类型
使用来计算你的答案。为了
例如, 这是您输入的最后一个数字
必须打印。
干杯,--贾里德
here are some hints:
much less file i/o for this problem
(unless you forgot to state that a
file must be created).
floor(10^(frac(log_10(7214989861293412))))
(Put this in Wolfram Alpha to see what it does.)
well in Fortran with simple
arithmetic inside of looping
constructs--at least for a first pass at the solution.
go. This advice would even apply to your character-driven approach. (This problem is ideally suited
for coming up with a cute indexing
scheme for your statistics, but some
people hate cute schemes in
programming. If you don't fear
cuteness ... then you can have associative
arrays in Fortran as long as your
keys are integers ;-)
problem is the data type you will
use to calculate your answers. For
example, here's the last number you
will have to print.
Cheers, --Jared