我想重排各行的位置。重排标准是,先按每行中的第一数据,由小到达排列。如果有两行,或者多行的第一个数据相等,则按第二个数据由小到大重排这些行的位置。如果有两行,或者多行第二个数据也相等,则按第三个数据重排各行位置。
这是我的代码。[Fortran] 纯文本查看 复制代码
PROGRAM COMPAIMPLICIT NONE
INTEGER, PARAMETER :: dp = SELECTED_REAL_KIND(15,14)
INTEGER :: i, j, k, l
REAL (KIND=dp) :: ap(116,6), lr(3,3), pl(3), ns(2,116)
OPEN (UNIT=3, FILE='data.dat', STATUS='OLD')
READ (UNIT=3, FMT=*)
READ (UNIT=3, FMT=*)
DO i = 1, 3, 1
READ (UNIT=3, FMT=*) pl
lr(i,:) = pl
END DO
READ (UNIT=3, FMT=*)
DO i = 1, 116, 1
READ (UNIT=3, FMT=*) pl
ap(i,4:6) = pl
ns(:,i) = i
END DO
DO i = 1, 116, 1
ap(i,1) = ap(i,4)*lr(1,1)+ap(i,5)*lr(2,1)+ap(i,6)*lr(3,1)
ap(i,2) = ap(i,4)*lr(1,2)+ap(i,5)*lr(2,2)+ap(i,6)*lr(3,2)
ap(i,3) = ap(i,4)*lr(1,3)+ap(i,5)*lr(2,3)+ap(i,6)*lr(3,3)
END DO
DO i = 2, 116, 1
DO j = 1, i-1, 1
IF ((ap(i,1) < ap(j,1)) .OR. &
((ap(i,1) == ap(j,1)) .AND. (ap(i,2) < ap(j,2))) .OR. &
((ap(i,1) < ap(j,1)) .AND. (ap(i,2) == ap(j,2)) .AND. &
(ap(i,3) < ap(j,3)))) THEN
k = ns(1,i)
DO l = j, i-1, 1
ns(1,l+1) = ns(1,l)
END DO
ns(1,j) = k
END IF
END DO
END DO
OPEN (UNIT=4, FILE='compare.dat', STATUS='UNKNOWN')
DO i = 1, 116, 1
DO j = 1, 116, 1
IF (ns(1,j) == i) WRITE (UNIT=4, FMT=*) ns(:,j), ap(j,1:3)
END DO
END DO
CLOSE (UNIT=3)
CLOSE (UNIT=4)
STOP
END PROGRAM COMPA