Fortran打印格式化输出

时间:2020-11-03 20:34:55

标签: arrays fortran

我正在尝试在所有其他内容的结尾打印程序的这一部分。但是我不知道如何格式化并继续这样做。我认为这与我的!Print格式化输出部分有关。请帮忙。它也不显示最佳距离或排列数

65 San Diego to Phoenix -- 350 miles
66 Phoenix to Dallas -- 604 miles
67 Dallas to Denver -- 389 miles
68 Denver to San Diego -- 900 miles
69
70 Best distance is: 2243
71 Number of permutations: 6

但这是我的程序输出的内容

Enter filename
data.txt
 Number of cities:            4
 SanDiego            Phoenix                       350
 Phoenix              Denver                       560
 Denver               Dallas                       389
 Dallas               SanDiego                    1100
 Distance is         2399

 SanDiego            Phoenix                       350
 Phoenix              Dallas                       604
 Dallas               Denver                       389
 Denver               SanDiego                     950
 Distance is         2293

 SanDiego            Denver                        950
 Denver               Phoenix                      560
 Phoenix              Dallas                       604
 Dallas               SanDiego                    1100
 Distance is         3214

 SanDiego            Denver                        950
 Denver               Dallas                       389
 Dallas               Phoenix                      604
 Phoenix              SanDiego                     350
 Distance is         2293

 SanDiego            Dallas                       1100
 Dallas               Denver                       389
 Denver               Phoenix                      560
 Phoenix              SanDiego                     350
 Distance is         2399

 SanDiego            Dallas                       1100
 Dallas               Phoenix                      604
 Phoenix              Denver                       560
 Denver               SanDiego                     950
 Distance is         3214


           1
           2
           3
           4
 SanDiego
 Phoenix
 Denver
 Dallas
           0
         350
         950
        1100
         350
           0
         560
         604
         950
         560
           0
         389
        1100
         604
         389
           0

这是我的完整代码

Program P4

IMPLICIT NONE

!Variable Declarations
INTEGER :: count, i, j, ios, distance=0, permutations=0, best_distance=999999
CHARACTER(50) :: filename
TYPE city
CHARACTER(20) :: name
END TYPE
TYPE(city), ALLOCATABLE, DIMENSION(:) :: city_list
INTEGER, ALLOCATABLE,  DIMENSION(:,:) :: d_table
INTEGER, ALLOCATABLE, DIMENSION(:) :: path, best_path

PRINT *, "Enter filename"
READ *, filename

!Open the file and read number of cities
OPEN(UNIT = 15, FILE = filename, FORM="FORMATTED", ACTION="READ", STATUS="OLD", IOSTAT=ios)
        IF(ios /= 0) THEN
                PRINT *, "ERROR, could not open file.", TRIM(filename), "Error code: ", ios
                STOP
        END IF

READ (UNIT=15, FMT=100) count
PRINT *, "Number of cities: ", count


!Allocate memory for all needed arrays
ALLOCATE(city_list(1:count), d_table(1:count,1:count), best_path(1:count), path(1:count), STAT=ios)

        IF(ios /= 0) THEN
                PRINT *, "ERROR, could not allocate memory."
                STOP
        END IF

!Fill in arrays from data file
DO i=1, count
        path(i) = i
        READ(UNIT=15, FMT=200) city_list(i)
                IF(ios < 0) THEN
                        EXIT
                END IF

         DO j=1, 4
                READ(UNIT=15, FMT=100) d_table(i,j)
         END DO
END DO


!Use recursion to find minimal distance
CALL permute(2, count)

!Print formatted output
PRINT *
DO i=1, count
        PRINT *, path(i)
END DO
DO i=1, count
        PRINT *, (city_list(i))
END DO

DO i=1, count
        DO j=1, count
                PRINT *, d_table(i,j)
        END DO
END DO


100 FORMAT (I6)
200 FORMAT (A)


CONTAINS
!Permute function
RECURSIVE SUBROUTINE permute(first, last)
!Declare intent of parameter variables
IMPLICIT NONE
INTEGER, INTENT(in) :: first, last
INTEGER :: i, temp
        IF(first == last) THEN
                distance = d_table(1,path(2))
                PRINT *, city_list(1)%name, city_list(path(2))%name, " ", d_table(1, path(2))
       DO i=2, last-1
                distance = distance + d_table(path(i),path(i+1))
                print *, city_list(path(i))%name, " ", city_list(path(i+1))%name,  d_table(path(i),path(i+1))
            END DO
            distance = distance + d_table(path(last),path(1))
            PRINT *, city_list(path(last))%name," ",city_list(path(1))%name, d_table(path(last),path(1))
            PRINT *, "Distance is ",distance
            PRINT *
            permutations = permutations + 1
            IF(distance < best_distance) THEN
                best_distance = distance
                DO i=2, count
                    best_path(i) = path(i)
                END DO
            END IF

        ELSE
            DO i=first, last
                temp = path(first)
                path(first) = path(i)
                path(i) = temp

                call permute(first+1,last)

                temp = path(first)
                path(first) = path(i)
                path(i) = temp
            END DO
        END IF
END SUBROUTINE permute

END PROGRAM P4

0 个答案:

没有答案
相关问题