[Fortran] 纯文本查看 复制代码
module TEST
contains
subroutine mean(A,N,O,P,Q,R)
implicit none
real :: O,P,Q,R
integer :: N,i
real,intent(in) :: A(:,:)
!记录ABCD的平均数,分别对应OPQR
O=0.0
P=0.0
Q=0.0
R=0.0
do i=1,N
O=O+A(i,1)
P=P+A(i,2)
Q=Q+A(i,3)
R=R+A(i,4)
enddo
O=O/real(N)
P=P/real(N)
Q=Q/real(N)
R=R/real(N)
return
end subroutine mean
subroutine FANGCHA(A,N,O,P,Q,R,K,L,M,H)
implicit none
real,intent(in) :: A(:,:)
integer :: i,N
real :: O,P,Q,R,K,L,M,H
real :: B(N),C(N),D(N),E(N)
K=0.0
L=0.0
M=0.0
H=0.0
do i=1,N
B(i)=(A(i,1)-O)**2
C(i)=(A(i,2)-P)**2
D(i)=(A(i,3)-Q)**2
E(i)=(A(i,4)-R)**2
enddo
do i=1,N
K=K+B(i)
L=L+C(i)
M=M+D(i)
H=H+E(i)
enddo
K=(K)/real(N)
L=(L)/real(N)
M=(M)/real(N)
H=(H)/real(N)
return
end subroutine FANGCHA
subroutine BIAOZHUNCHA(K,L,M,H,bzc1,bzc2,bzc3,bzc4)
implicit none
real :: K,L,M,H,bzc1,bzc2,bzc3,bzc4
bzc1=sqrt(K)
bzc2=sqrt(L)
bzc3=sqrt(M)
bzc4=sqrt(H)
return
end subroutine BIAOZHUNCHA
subroutine BZCS(bzc1,bzc2,bzc3,bzc4,bzc5,bzc6,bzc7,bzc8,bzc9,bzc10,bzc11,bzc12)
implicit none
real :: bzc1,bzc2,bzc3,bzc4,bzc5,bzc6,bzc7,bzc8,bzc9,bzc10,bzc11,bzc12
bzc5=bzc1/3.0
bzc6=bzc2/3.0
bzc7=bzc3/3.0
bzc8=bzc4/3.0
bzc9=bzc1/4.0
bzc10=bzc2/4.0
bzc11=bzc3/4.0
bzc12=bzc4/4.0
return
end subroutine BZCS
end module TEST
program main
use TEST
implicit none
real,allocatable :: A(:,:)
integer :: i,j,N
real :: O,P,Q,R,K,L,M,H,bzc1,bzc2,bzc3,bzc4
real :: bzc5,bzc6,bzc7,bzc8,bzc9,bzc10,bzc11,bzc12
write(*,*) "输入ABCD每列元素个数(注意元素个数必须相等):"
read(*,*) N
j=4
open(1,file='TEST.txt',status='old')
open(2,file='TEST01.txt')
allocate (A(N,j))
read(1,*)
write(2,*) "A,B,C,D分别为:"
write(2,*)
do i=1,N
read(1,*) (A(i,j),j=1,4)
write(2,*) (A(i,j),j=1,4)
enddo
write(2,*)
!计算平均值
call mean(A,N,O,P,Q,R)
!计算方差
call FANGCHA(A,N,O,P,Q,R,K,L,M,H)
!计算标准差
call BIAOZHUNCHA(K,L,M,H,bzc1,bzc2,bzc3,bzc4)
!标准差再计算
call BZCS(bzc1,bzc2,bzc3,bzc4,bzc5,bzc6,bzc7,bzc8,bzc9,bzc10,bzc11,bzc12)
write(2,*) "A,B,C,D的平均数分别为:"
write(2,*)
write(2,*) O,P,Q,R
write(2,*)
write(2,*) "A,B,C,D的方差分别为:"
write(2,*)
write(2,*) K,L,M,H
write(2,*)
write(2,*) "A,B,C,D的标准差为:"
write(2,*)
write(2,*) bzc1,bzc2,bzc3,bzc4
write(2,*)
write(2,*) "A,B,C,D标准差的三分之一:"
write(2,*)
write(2,*) bzc5,bzc6,bzc7,bzc8
write(2,*)
write(2,*) "A,B,C,D标准差的四分之一:"
write(2,*)
write(2,*) bzc9,bzc10,bzc11,bzc12
DEALLOCATE(A)
pause
end