[Fortran] 纯文本查看 复制代码
module typedef
type sm
real(kind=4) v,n1
end type
end module
program main
use typedef
implicit none
type(sm)::s1(100),s2(100),s3(100),s4(100),s5(100),s6(100),s7(100),s8(100),s9(100),s10(100),s11(100),s12(100),s13(100)
character::file1="length.txt",file2="length(2).txt",file3="length(3).txt",file4="length(4).txt",file5="length(5).txt",file6="length(6).txt"
integer::i,status=0
real::d1(50),d2(50),d3(50),d4(50),d5(50),d6(50)
forall(i=1:50)
d1(i)=0
d2(i)=0
d3(i)=0
d4(i)=0
d5(i)=0
d6(i)=0
end forall
open(11,file="length1.txt",status="old")
do i=1,50
read(11,*,iostat=status) s1(i)%v,s1(i)%n1
if(status/=0) exit
d1(s1(i)%v)=s1(i)%n1
write(*,*) s1(i)%v,s1(i)%n1
end do
open(12,file="length(2).txt",status="old")
do i=1,50
read(12,*,iostat=status) s2(i)%v,s2(i)%n1
d2(s2(i)%v)=s2(i)%n1
if(status/=0) exit
end do
open(13,file="length(3).txt",status="old")
do i=1,50
read(13,*,iostat=status) s3(i)%v,s3(i)%n1
d3(s3(i)%v)=s3(i)%n1
if(status/=0) exit
end do
open(14,file="length(4).txt",status="old")
do i=1,50
read(14,*,iostat=status) s4(i)%v,s4(i)%n1
d4(s4(i)%v)=s4(i)%n1
if(status/=0) exit
end do
open(15,file="length(5).txt",status="old")
do i=1,50
read(15,*,iostat=status) s5(i)%v,s5(i)%n1
d5(s5(i)%v)=s5(i)%n1
if(status/=0) exit
end do
open(16,file="length(6).txt",status="old")
do i=1,50
read(16,*,iostat=status) s6(i)%v,s6(i)%n1
d6(s6(i)%v)=s6(i)%n1
if(status/=0) exit
end do
open(17,file="length7.txt")
do i=1,50
write(17,*) i, real(d1(i)+d2(i)+d3(i)+d4(i)+d5(i)+d6(i))/real(6)
end do
end program
[Fortran] 纯文本查看 复制代码
module typedef
type sm
real(kind=4) :: v !// 值
integer :: n !// 出现次数
end type
end module typedef
program www_fcode_cn
use typedef
implicit none
Integer , parameter :: N_File = 6 !// 6 个文件
Integer , parameter :: N_Rec = 50 !// 最多 50 个记录
character( len = 30 ) ::file( N_File + 1 ) = (/"length1.txt","length(2).txt","length(3).txt","length(4).txt","length(5).txt","length(6).txt","length(7).txt"/)
!// 上面是 6 个输入文件。加上一个输出文件 length(7).txt
Type ( sm ) :: d( N_Rec )
integer :: i , j , ierr
real :: rv
d( : )%v = 0.0 !// 所有值一开始为 0.0
d( : )%n = 0 !// 所有出现次数一开始为 0
Do i = 1 , N_File !// 循环读取 6 个文件
Open( 12 , File = Trim(file(i)) , status="old" )
Do !// 循环读取每个文件的每行
Read( 12 , * , ioStat = ierr ) j , rv
if( ierr /= 0 ) exit
d(j)%v = d(j)%v + rv !// 值叠加上去
d(j)%n = d(j)%n + 1 !// 出现次数加1
End Do
Close( 12 )
End Do
open(17,file=Trim(file(N_File+1)))
Do i = 1 , N_Rec
if ( d(i)%n == 0 ) then !// 如果从未出现
rv = 0.0 !// 平均值取 0.0
else
rv = d(i)%v / d(i)%n !// 否则,叠加值除以出现次数
end if
write(17,*) i , rv ! , d(i)%n !// 可以输出 d(i)%n 出现次数确认没有问题
End do
end program www_fcode_cn