[Fortran] 纯文本查看 复制代码
program main
implicit none
integer::i,c(3,4),a_1(3,2),a(6),b_1(3,2),b(6)
real amean,bmean,sfenzi,sfenmu_1,sfenmu_2,coefficient
open(12,file=1.txt')
read(12,*)c
a_1=c(:,1:2)
b_1=c(:,3:4)
a=reshape(a_1,(/6/))
b=reshape(b_1,(/6/))
amean=real(sum(a))/real(size(a))
bmean=real(sum(b))/real(size(b))
write(*,*)"a=",a
write(*,*)"b=",b
write(*,*)"amean=",amean
write(*,*)"bmean=",bmean
sfenzi=0.0
sfenmu_1=0.0
sfenmu_2=0.0
do i=1,size(a)
sfenzi=sfenzi+(real(a(i))-amean)*(real(b(i))-bmean)
sfenmu_1=sfenmu_1+(real(a(i))-amean)**2
sfenmu_2=sfenmu_2+(real(b(i))-bmean)**2
end do
coefficient=sfenzi/sqrt(sfenmu_1*sfenmu_2)
write(*,*)"coefficient=",coefficient
stop
end program
[Fortran] 纯文本查看 复制代码
Program main
Implicit None
Integer , parameter :: N = 6
Real :: a(N) , b(N)
Real :: amean , bmean , sfenzi = 0.0 , sfenmu_1 = 0.0 ,sfenmu_2 = 0.0 , coefficient
Integer :: i
Open(12,file='1.txt')
Read(12,*) a , b
Close(12)
amean= sum(a) / N
bmean= sum(b) / N
write(*,*)"a=",a
write(*,*)"b=",b
write(*,*)"amean=",amean
write(*,*)"bmean=",bmean
Do i = 1 , N
sfenzi = sfenzi + ( a(i)-amean ) * ( b(i)-bmean )
sfenmu_1 = sfenmu_1 + (a(i)-amean)**2
sfenmu_2 = sfenmu_2 + (b(i)-bmean)**2
End Do
coefficient=sfenzi/sqrt(sfenmu_1*sfenmu_2)
write(*,*)"coefficient=",coefficient
End Program main