如何将连续列的数对应的最小值提取出
本人刚刚接触Fortran代码,想将图片红框中所有1的那一部分对应E的最小值获得,然后所有2对应E的最小值获得,3对应E的最小值……以此类推,然后将对应最小值那一行输出到文件中,比如:3个红色框的部分,将它们最小值获得后写入文件中变成:Step number39scan point 1E=-571.251770020
Step number 5scan point 2E=-571.252807617
Step number 4scan point 3E=-571.253234863
……
这个代码是我自己尝试写的,不过问题还没解决,只解决了红框中那一列中有多少个数。还望各位有什么思路或者代码修改不吝惜赐教,谢谢。
program takeminenergy
implicit none
integer i,j,num,num1,num2,lines,k,num4
integer,allocatable::x(:),y(:)
real,allocatable::z(:),minz(:),minumz(:)
open(12,file='result.txt')!输入文件
open(13,file='a.txt') !输出文件
open(14,file='b.txt')!输出文件
num1=1
num2=0
write(*,*) 'How many lines:' !读取文件有多少行
read(*,*) lines
allocate (x(lines))
allocate (y(lines))
allocate (minz(lines))
allocate (minumz(lines))
do i=1,lines
read(12,"(12x,I4,12x,I6)") x(i),y(i)
if (y(i)== num1) then
num2=num2+1
else if (y(i) /= num1) then
y(num1)=num2
num1=num1+1
num2=1
end if
end do
write(*,*) num1
do j=1,num1
write(13,"(I6)") y(j) !获取数字相同那一列有多少个,比如全是数字1的共有39个
end do
close(13)
close(12)
open(13,file='a.txt')
open(12,file='result.txt')
open(15,file='c.txt') !输出文件
do j=1,num1
read(13,"(I6)") num4
write(*,*) num4
do k=1,num4
read(12,"(38x,f16.9)") z(k)
write(15,*)z(k)
end do
minz=minval(z)
write(14,"(f16.9)") minz
deallocate(z)
end do
end
本帖最后由 楚香饭 于 2015-11-12 09:44 编辑
你想得太复杂了,其实问题可以很简单。
Program www_fcode_cn
Implicit None
real(kind=kind(0.0d0)) :: rE , rMinE
integer :: ierr , iRepeat , iPoint , pointerlast , iMinRepeat
character(len=12) :: c(5)
Open( 12 , File = 'result.txt' )
Open( 13 , File = 'out.txt' )
pointerlast = 1
rMinE= 1.0e30
Do
Read( 12 , * , ioStat=ierr ) c(1) , c(2) , iRepeat , c(3) , c(4) , iPoint , c(5) , rE
if ( ( ierr== 0 ) .and. (iPoint == pointerlast) ) then !// 同一个Point
if( rE <= rMinE ) then
rMinE = rE
iMinRepeat = iRepeat
end if
else !//不同Point
write(13,'(a,1x,a,1x,i4,2x,a,1x,a,i6,2x,a,f16.9)') trim(c(1)) , trim(c(2)) , iMinRepeat , &
trim(c(3)) , trim(c(4)) , pointerlast , trim(c(5)) , rMinE
pointerlast = iPoint
iMinRepeat= iRepeat
rMinE = rE
end if
if ( ierr /= 0 ) Exit
End Do
Close( 12 )
Close( 13 )
End Program www_fcode_cn
群主太无敌了,唉,我该如何才能达到你的这种境界。目前我也是一边看彭老师的书,一边练习编程,也关注论坛和qq群,但是到真正自己编程的时候发现思想总是很复杂,想问问群主,为什么你的编程思想可以这么简单,我看这代码里也没有什么特别特殊的东西。 这个没有别的方法,多写多练吧。
很多时候同一个问题是可能有不同的思路的,你把这个代码看懂了,就理解他的思路了。以后遇到同类问题也不妨换个思路看看。 懂了,多谢多练,思想自然就有了,再次谢谢群主。
页:
[1]